当前位置:网站首页>栈(线性结构)
栈(线性结构)
2022-07-02 06:09:00 【我觉得海星_98】
- 栈是一个先入后出(FILO-First In Last Out)的有序列表。
- 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
- 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除。
图解
代码
注释有思路讲解~
package com.atguigu.stack;
import java.util.Scanner;
//author qij
public class ArrayStackDemo {
public static void main(String[] args) {
//测试一下ArrayStack 是否正确
//先创建一个ArrayStack对象->表示栈
ArrayStack stack = new ArrayStack(4);
String key = "";
boolean loop = true; //控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while(loop) {
System.out.println("show: 表示显示栈");
System.out.println("exit: 退出程序");
System.out.println("push: 表示添加数据到栈(入栈)");
System.out.println("pop: 表示从栈取出数据(出栈)");
System.out.println("请输入你的选择");
key = scanner.next();
switch (key) {
case "show":
stack.list();
break;
case "push":
System.out.println("请输入一个数");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int res = stack.pop();
System.out.printf("出栈的数据是 %d\n", res);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
break;
case "exit":
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序退出~~~");
}
}
//定义一个 ArrayStack 表示栈
class ArrayStack {
private int maxSize; // 栈的大小
private int[] stack; // 数组,数组模拟栈,数据就放在该数组
private int top = -1;// top表示栈顶,初始化为-1
//构造器
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//栈满
public boolean isFull() {
return top == maxSize - 1;
}
//栈空
public boolean isEmpty() {
return top == -1;
}
//入栈-push
public void push(int value) {
//先判断栈是否满
if(isFull()) {
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈-pop, 将栈顶的数据返回
public int pop() {
//先判断栈是否空
if(isEmpty()) {
//抛出异常
throw new RuntimeException("栈空,没有数据~");
}
int value = stack[top];
top--;
return value;
}
//显示栈的情况[遍历栈], 遍历时,需要从栈顶开始显示数据
public void list() {
if(isEmpty()) {
System.out.println("栈空,没有数据~~");
return;
}
//需要从栈顶开始显示数据
for(int i = top; i >= 0 ; i--) {
System.out.printf("stack[%d]=%d\n", i, stack[i]);
}
}
}
边栏推荐
- Arduino Wire 库使用
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- Talking about MySQL database
- Format check JS
- [C language] simple implementation of mine sweeping game
- State machine in BGP
- Jetpack Compose 与 Material You 常见问题解答
- LeetCode 39. Combined sum
- 如何使用MITMPROXy
- 深入了解JUC并发(二)并发理论
猜你喜欢
From design delivery to development, easy and efficient!
步骤详解 | 助您轻松提交 Google Play 数据安全表单
ROS create workspace
Little bear sect manual query and ADC in-depth study
深入了解JUC并发(一)什么是JUC
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
让每一位开发者皆可使用机器学习技术
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
sudo提权
Shenji Bailian 3.54-dichotomy of dyeing judgment
随机推荐
Shenji Bailian 3.53-kruskal
Redis key value database [primary]
Ros2 --- lifecycle node summary
I/o multiplexing & event driven yyds dry inventory
LeetCode 47. Full arrangement II
Reading classic literature -- Suma++
495. Timo attack
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
来自读者们的 I/O 观后感|有奖征集获奖名单
ROS create workspace
Little bear sect manual query and ADC in-depth study
Classic literature reading -- deformable Detr
Arduino Wire 库使用
官方零基础入门 Jetpack Compose 的中文课程来啦!
LeetCode 47. 全排列 II
LeetCode 39. 组合总和
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
神机百炼3.54-染色法判定二分图
Bgp Routing preference Rules and notice Principles
复杂 json数据 js前台解析 详细步骤《案例:一》