当前位置:网站首页>栈(线性结构)
栈(线性结构)
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]);
}
}
}
边栏推荐
- LeetCode 283. Move zero
- Deep learning classification network -- alexnet
- Mock simulate the background return data with mockjs
- Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
- keepalived安装使用与快速入门
- Eco express micro engine system has supported one click deployment to cloud hosting
- Detailed steps of JS foreground parsing of complex JSON data "case: I"
- Detailed notes of ES6
- Cookie plugin and localforce offline storage plugin
- BGP中的状态机
猜你喜欢

The official zero foundation introduction jetpack compose Chinese course is coming!

Community theory | kotlin flow's principle and design philosophy

AttributeError: ‘str‘ object has no attribute ‘decode‘

Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish

步骤详解 | 助您轻松提交 Google Play 数据安全表单

来自读者们的 I/O 观后感|有奖征集获奖名单

Classic literature reading -- deformable Detr

Sumo tutorial Hello World

Shenji Bailian 3.54-dichotomy of dyeing judgment

VLAN experiment of switching technology
随机推荐
Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
Lambda expressions and method references
Reading classic literature -- Suma++
The official zero foundation introduction jetpack compose Chinese course is coming!
神机百炼3.54-染色法判定二分图
Network related knowledge (Hardware Engineer)
神机百炼3.52-Prim
No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
LeetCode 283. 移动零
Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
Redis Key-Value数据库 【秒杀】
Frequently asked questions about jetpack compose and material you
Style modification of Mui bottom navigation
Generic classes and parameterized classes of SystemVerilog
492. Construction rectangle
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
LeetCode 47. Full arrangement II
Eco express micro engine system has supported one click deployment to cloud hosting
Compte à rebours de 3 jours pour l'inscription à l'accélérateur de démarrage Google Sea, Guide de démarrage collecté à l'avance!
LeetCode 77. 组合