[Java] Java模拟队列的出队和进队 →→→→→进入此内容的聊天室

来自 , 2021-03-13, 写在 Java, 查看 146 次.
URL http://www.code666.cn/view/f5b2aa92
  1. package com.stackANDqueue;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5.  
  6. /*
  7.  * 循环队列的入队和出队
  8.  */
  9. public class Queue {
  10.         static int MAX = 20;
  11.         static String[] item = new String[MAX];
  12.         static int front, rear;
  13.        
  14.         public Queue() {
  15.                 // TODO Auto-generated constructor stub
  16.                 front = 0;
  17.                 rear = -1;
  18.         }
  19.        
  20.         /**
  21.          * 按任何一键立刻执行Fuction
  22.          */
  23.         public static void anyKey_f(){
  24.                 char tChar;
  25.                 System.out.println("   Press any key to contunue...");
  26.                 try {
  27.                         tChar = (char) System.in.read();
  28.                 } catch (IOException e) {
  29.                         // TODO Auto-generated catch block
  30.                         e.printStackTrace();
  31.                 }
  32.         }
  33.        
  34.         public static void enqueue_f(){//入队函数
  35.                 DataInputStream in = new DataInputStream(System.in);
  36.                 if(rear >= MAX-1) System.out.println("\n Queue is full ! \n");
  37.                 else{
  38.                         rear ++;
  39.                         System.out.println(" \n Please enter item to insert ! \n");
  40.                         System.out.flush();
  41.                         try {
  42.                                 item[rear] = in.readLine();
  43.                         } catch (IOException e) {
  44.                                 // TODO Auto-generated catch block
  45.                                 e.printStackTrace();
  46.                         }
  47.                 }
  48.                 System.out.println("");
  49.         }
  50.        
  51.         public static void dequeue_f(){//出队函数
  52.                 if(front > rear) System.out.println("\n No item ,Queue is empty ! \n");
  53.                 else{
  54.                         System.out.print("\n Item " + item[front] + " is deleted ! \n");
  55.                         front ++;
  56.                 }
  57.                 System.out.println("");
  58.         }
  59.        
  60.         public static void list_f(){
  61.                 DataInputStream in = new DataInputStream(System.in);
  62.                 int count=0, i=0;
  63.                 if(front > rear) System.out.print("\n  No item , Queue is empty ! \n\n");
  64.                 else{
  65.                         System.out.print("\n ITEM \n");
  66.                         System.out.flush();
  67.                         for(i = front; i<= rear; i++){
  68.                                 System.out.print("");
  69.                                 System.out.print(item[i] + "\n");
  70.                                 count ++;
  71.                                 if(count % 20 ==0) anyKey_f();
  72.                         }
  73.                         System.out.print(" --------------------\n");
  74.                         System.out.print(" Total item : "+count + "\n\n");
  75.                         anyKey_f();
  76.                         System.out.println("");
  77.                 }
  78.         }
  79.        
  80.         @SuppressWarnings("deprecation")
  81.         public static void main(String args[]){
  82.                 DataInputStream in = new DataInputStream(System.in);
  83.                 String op = "";
  84.                 int option = 0;
  85.                 Queue obj = new Queue();
  86.                 do{
  87.                         System.out.println("****************** Stack Program *******************");
  88.                         System.out.println("                                                    ");
  89.                         System.out.println("                 <1> Insert Node                    ");
  90.                         System.out.println("                 <2> Delete Node                    ");
  91.                         System.out.println("                 <3> List  Node                     ");
  92.                         System.out.println("                 <4> Exit !                         ");
  93.                         System.out.println("                                                    ");
  94.                         System.out.println("****************************************************");
  95.                         System.out.print("\n         Choice :  ");
  96.                         System.out.flush();
  97.                         op = "";
  98.                         try {
  99.                                 op = in.readLine();
  100.                         } catch (IOException e) {
  101.                                 // TODO Auto-generated catch block
  102.                                 e.printStackTrace();
  103.                         }
  104.                         option = 0;
  105.                         try{
  106.                                 option = Integer.valueOf(op).intValue();
  107.                         }catch(NumberFormatException e){
  108.                                 System.out.println("\n Please input (1,2,3,4).....");
  109.                                 System.out.println("\n\n\n");
  110.                         }
  111.                         switch(option){
  112.                                 case 1:
  113.                                         enqueue_f();
  114.                                         break;
  115.                                 case 2:
  116.                                         dequeue_f();
  117.                                         break;
  118.                                 case 3:
  119.                                         list_f();
  120.                                         break;
  121.                                 case 4:
  122.                                         System.exit(0);
  123.                         }
  124.                 }while(true);
  125.         }
  126.        
  127. }
  128. //java/1342

回复 "Java模拟队列的出队和进队 "

这儿你可以回复上面这条便签

captcha