06--栈【使用数组模拟栈数据结构】

栈:是一种先入后出的数据结构

栈底:栈中最下边的数据

栈顶:栈中最上边的数据

 

  •  使用数组模拟栈数据结构 -->
 1 //定义栈【里边使用数组模拟栈数据结构】,管理整个栈
 2 class Stack{
 3     private int maxSize; //数组中能放置的最大的数据的个数
 4     private int[] stack; //定义数组存放数据
 5     public Stack(int maxSize) {
 6         this.maxSize = maxSize;
 7         stack = new int[this.maxSize];
 8     }
 9     
10     //栈顶 【push一下栈顶 +1,pop一下栈顶 -1】
11     private int top = -1;
12     
13     //检查是否栈满
14     public boolean isFull() {
15         return top == maxSize - 1;
16     }
17     
18     //入栈
19     public void push(int n) {
20         if (isFull()) {
21             System.out.println("栈满,无法加入数据");
22             return;
23         }
24         top++;
25         stack[top] = n;
26     }
27     
28     //出栈
29     public int pop() {
30         if (top == -1) {
31             throw new RuntimeException("栈为空,无数据");
32         }
33         int tmp = stack[top];
34         top--;
35         return tmp;
36     }
37     
38     //展示栈【从上 --> 到下】
39     public void show() {
40         if (top == -1) {
41             throw new RuntimeException("栈为空,无数据");
42         }
43         for(int i = top; i >= 0; i--) {
44             System.out.printf("stack[%d] = %d\n",i, stack[i]);
45         }
46     }
47     
48 }
 1 import java.util.Scanner;
 2 
 3 //数组模拟栈数据结构
 4 public class ArrayStackDemo {
 5 
 6     public static void main(String[] args) {
 7         Stack arrayStack = new Stack(3);
 8         Scanner sc = new Scanner(System.in);
 9 
10         int command = 0;
11         boolean loop = true;
12         while(loop) {
13             System.out.println("*****************************");
14             System.out.println("***1、push***********2、pop***");
15             System.out.println("***3、show***********4、exit***");
16             System.out.println("请选择-->");
17             command = sc.nextInt();
18             switch(command) {
19             case 1:
20                 System.out.println("请输入您要压栈数据");
21                 int n = sc.nextInt();
22                 arrayStack.push(n);
23                 break;
24             case 2:
25                 try {
26                     int popNum = arrayStack.pop();
27                     System.out.println(popNum + "出栈");
28                 } catch (Exception e) {
29                     System.out.println(e.getMessage());
30                 }
31                 break;
32             case 3:
33                 try {
34                     arrayStack.show();
35                 } catch (Exception e) {
36                     System.out.println(e.getMessage());
37                 }
38                 break;
39             case 4:
40                 loop = false;
41                 sc.close();
42                 System.out.println("程序退出~~~");
43                 break;
44             default:
45                 System.out.println("不支持该选择,请重新输入~");
46                 break;
47             }
48         }
49     }
50 
51 }

 

 

 

 

 

posted @ 2022-08-13 00:30  羽梦齐飞  阅读(19)  评论(0编辑  收藏  举报