[Java] Java模拟数据结构中栈的进队出队 →→→→→进入此内容的聊天室

来自 , 2020-06-23, 写在 Java, 查看 140 次.
URL http://www.code666.cn/view/a3fefe83
  1. package com.stackANDqueue;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5.  
  6. /*
  7.  * 这是一个栈的模拟
  8.  * 模拟的是入栈和出栈
  9.  */
  10. public class Stack {
  11.         static int MAX = 10;
  12.         static String[] item = new String[10];
  13.         static int top;
  14.        
  15.         Stack(){
  16.                 top = -1;
  17.         }
  18.        
  19.         /**
  20.          * 入栈函数
  21.          */
  22.         @SuppressWarnings("deprecation")
  23.         public static void push_f(){
  24.                 DataInputStream in = new DataInputStream(System.in);
  25.                 if(top >= MAX-1) System.out.print("\n   Stack is full ! \n"); //当栈已满,显示错误!
  26.                 else{
  27.                         top ++;
  28.                         System.out.print("\n  Please Enter item to insert ! \n");
  29.                         System.out.flush();
  30.                         try {
  31.                                 item[top] = in.readLine();
  32.                         } catch (IOException e) {
  33.                                 // TODO Auto-generated catch block
  34.                                 e.printStackTrace();
  35.                         }
  36.                 }
  37.                 System.out.println("");
  38.         }
  39.        
  40.         /**
  41.          * 出栈函数
  42.          */
  43.         public static void pop_f(){
  44.                 if(top < 0) System.out.print("\n  No item , stack is empty ! \n");//当栈没有数据存在,显示错误!
  45.                 else{
  46.                         System.out.print("\n  Item "+ item[top] + "  delete !  \n");
  47.                         top--;
  48.                 }
  49.                 System.out.println("");
  50.         }
  51.        
  52.         /**
  53.          * 输出函数
  54.          */
  55.         public static void list_f(){
  56.                 int count=0, i=0;
  57.                 if(top < 0) System.out.print("\n  No item , stack is empty ! \n");
  58.                 else{
  59.                         System.out.print("\n\n ITEM\n");
  60.                         System.out.print("-------------------\n");
  61.                         for(i=top;i>=0;i--){
  62.                                 System.out.print(" ");
  63.                                 System.out.println(item[i]);
  64.                                 count++;
  65.                         }
  66.                         System.out.print("-------------------\n");
  67.                         System.out.print("Total item :" + count +"\n\n");
  68.                 }
  69.                 System.out.println("");
  70.         }
  71.        
  72.        
  73.         @SuppressWarnings({ "deprecation", "unused" })
  74.         public static void main(String args[]){
  75.                 DataInputStream in = new DataInputStream(System.in);
  76.                 String op = "";
  77.                 int option = 0;
  78.                 Stack obj = new Stack();
  79.                 do{
  80.                         System.out.println("****************** Stack Program *******************");
  81.                         System.out.println("                                                    ");
  82.                         System.out.println("                 <1> Insert Node                    ");
  83.                         System.out.println("                 <2> Delete Node                    ");
  84.                         System.out.println("                 <3> List  Node                     ");
  85.                         System.out.println("                 <4> Exit !                         ");
  86.                         System.out.println("                                                    ");
  87.                         System.out.println("****************************************************");
  88.                         System.out.print("\n         Choice :  ");
  89.                         System.out.flush();
  90.                         op = "";
  91.                         try {
  92.                                 op = in.readLine();
  93.                         } catch (IOException e) {
  94.                                 // TODO Auto-generated catch block
  95.                                 e.printStackTrace();
  96.                         }
  97.                         option = 0;
  98.                         try{
  99.                                 option = Integer.valueOf(op).intValue();
  100.                         }catch(NumberFormatException e){
  101.                                 System.out.println("\n Please input (1,2,3,4).....");
  102.                                 System.out.println("\n\n\n");
  103.                         }
  104.                         switch(option){
  105.                                 case 1:
  106.                                         push_f();
  107.                                         break;
  108.                                 case 2:
  109.                                         pop_f();
  110.                                         break;
  111.                                 case 3:
  112.                                         list_f();
  113.                                         break;
  114.                                 case 4:
  115.                                         System.exit(0);
  116.                         }
  117.                 }while(true);
  118.         }
  119. }
  120. //java/1343

回复 "Java模拟数据结构中栈的进队出队"

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

captcha