首页 > 其他分享 >用数组实现栈

用数组实现栈

时间:2023-01-16 14:45:50浏览次数:42  
标签:实现 System break int 数组 ArrayStack println out

入栈:

 

 出栈:

 

 代码:

 

  1 import java.util.Scanner;
  2 
  3 public class ArrayStackTest {
  4     public static void main(String[] args) {
  5         ArrayStack stack = new ArrayStack(4);
  6         String key = "";
  7         //控制是否退出菜单
  8         boolean loop = true;
  9         Scanner scanner = new Scanner(System.in);
 10 
 11         while(loop) {
 12             System.out.println("s: 表示显示栈");
 13             System.out.println("e: 退出程序");
 14             System.out.println("a: 表示添加数据到栈(入栈)");
 15             System.out.println("p: 表示从栈取出数据(出栈)");
 16             System.out.println("l: 查看栈顶元素");
 17             System.out.println("请输入你的选择");
 18             key = scanner.next();
 19             switch (key) {
 20                 case "s":
 21                     System.out.println(stack.toString());
 22                     break;
 23                 case "l":
 24                     try {
 25                         int peek = stack.peek();
 26                         System.out.printf("栈顶元素:%d\n", peek);
 27                     } catch (Exception e) {
 28                         System.out.println(e.getMessage());
 29                     }
 30                     break;
 31                 case "a":
 32                     System.out.println("请输入一个数");
 33                     int value = scanner.nextInt();
 34                     stack.push(value);
 35                     break;
 36                 case "p":
 37                     try {
 38                         int res = stack.pop();
 39                         System.out.printf("出栈的数据是 %d\n", res);
 40                     } catch (Exception e) {
 41                         System.out.println(e.getMessage());
 42                     }
 43                     break;
 44                 case "e":
 45                     scanner.close();
 46                     loop = false;
 47                     break;
 48                 default:
 49                     break;
 50             }
 51         }
 52         System.out.println("程序退出~~~");
 53     }
 54 }
 55 
 56 /**
 57  * 数组实现栈
 58  */
 59 class ArrayStack {
 60 
 61     /**
 62      * 栈顶指针
 63      */
 64     private int top = -1;
 65 
 66     private int[] values;
 67 
 68     public ArrayStack(int size) {
 69         values = new int[size];
 70     }
 71 
 72     /**
 73      * 判断栈是否为空
 74      * @return
 75      */
 76     public boolean isEmpty() {
 77         return top <= -1;
 78     }
 79 
 80     /**
 81      * 判断栈是否为满
 82      * @return
 83      */
 84     public boolean isFull() {
 85         return top == values.length - 1;
 86     }
 87 
 88     /**
 89      * 添加元素
 90      * @param element
 91      * @return
 92      */
 93     public boolean push(int element) {
 94         if (isFull()) {
 95             System.out.println("栈已满,无法添加");
 96             return false;
 97         }
 98         top++;
 99         values[top] = element;
100         return true;
101     }
102 
103     /**
104      * 弹出元素
105      * @return
106      */
107     public int pop() throws Exception {
108         if (isEmpty()) {
109             throw new Exception("栈为空,无法弹出");
110         }
111         int result = values[top];
112         top--;
113         return result;
114     }
115 
116     /**
117      * 查看栈顶元素
118      * @return
119      */
120     public int peek() throws Exception {
121         if (isEmpty()) {
122             throw new Exception("栈已空");
123         }
124         return values[top];
125     }
126 
127     @Override
128     public String toString() {
129         if (isEmpty()) {
130             return "栈为空";
131         }
132         StringBuilder stringBuilder = new StringBuilder();
133         for (int i = top; i > -1; i--) {
134             stringBuilder.append(values[i] + "    ");
135         }
136         return "ArrayStack{" +
137                 "values=" + stringBuilder.toString() +
138                 '}';
139     }
140 }

 

标签:实现,System,break,int,数组,ArrayStack,println,out
From: https://www.cnblogs.com/xueseng/p/17055343.html

相关文章