数组模拟栈

数组模拟栈

  1、用数组模拟栈的使用,由于栈是一种有序列表,当然可以使用数组的结构来存储栈的数据内容。

  2、实现思路分析,并画出示意图。

    

  3、代码实现

  1 public class ArrayStackDemo {
  2 
  3     public static void main(String[] args) {
  4         // 测试栈
  5         ArrayStack stack = new ArrayStack(4);
  6         String key = "";
  7         boolean loop = true; // 是否退出菜单
  8         Scanner sc = new Scanner(System.in);
  9 
 10         while (loop) {
 11             System.out.println("show:表示显示栈");
 12             System.out.println("exit:退出程序");
 13             System.out.println("push:表示添加数据到栈(入栈)");
 14             System.out.println("pop:表示从栈取出数据(出栈)");
 15             System.out.println("请输入你的选择");
 16             key = sc.next();
 17             switch (key) {
 18             case "show":
 19                 stack.list();
 20                 break;
 21             case "push":
 22                 System.out.println("请输入一个数据");
 23                 int value = sc.nextInt();
 24                 stack.push(value);
 25                 break;
 26             case "pop":
 27                 try {
 28                     int res = stack.pop();
 29                     System.out.printf("出栈的数据是%d
", res);
 30                 } catch (Exception e) {
 31                     System.out.println(e.getMessage());
 32                 }
 33                 break;
 34             case "exit":
 35                 sc.close();
 36                 loop = false;
 37                 break;
 38             default:
 39                 break;
 40             }
 41         }
 42 
 43         System.out.println("程序退出!");
 44     }
 45 
 46 }
 47 
 48 // 定义一个ArrayStack 表示栈
 49 class ArrayStack {
 50     private int maxSize; // 栈的大小
 51     private int[] stack; // 数组,数组模拟栈,数据就在该数组
 52     private int top = -1; // top 表示栈顶,初始化为 -1
 53 
 54     // 构造器
 55     public ArrayStack(int maxSize) {
 56         this.maxSize = maxSize;
 57         stack = new int[this.maxSize];
 58     }
 59 
 60     // 判断栈满
 61     public boolean isFull() {
 62         return top == maxSize - 1;
 63     }
 64 
 65     // 判断栈空
 66     public boolean isEmpty() {
 67         return top == -1;
 68     }
 69 
 70     // 入栈 - push
 71     public void push(int value) {
 72         // 先判断栈是否满
 73         if (isFull()) {
 74             System.out.println("栈满");
 75             return;
 76         }
 77         top++;
 78 
 79         stack[top] = value;
 80 
 81     }
 82 
 83     // 出栈 - pop,将栈顶的数据返回
 84     public int pop() {
 85         // 先判断栈是否空
 86         if (isEmpty()) {
 87             // 抛出异常来处理
 88             throw new RuntimeException("栈空,没有数据··");
 89         }
 90 
 91         int value = stack[top];
 92         top--;
 93         return value;
 94     }
 95 
 96     // 显示栈的情况[遍历栈],从栈顶往下显示数据
 97     public void list() {
 98         if (isEmpty()) {
 99             System.out.println("栈空,没有数据~~");
100             return;
101         }
102 
103         for (int i = top; i >= 0; i--) {
104             System.out.printf("stack[%d]=%d
", i, stack[i]);
105         }
106     }
107 
108 }
原文地址:https://www.cnblogs.com/niujifei/p/11589605.html