当前位置:网站首页>Stack (linear structure)
Stack (linear structure)
2022-07-02 06:48:00 【I think starfish_ ninety-eight】
- A stack is a First in, then out (FILO-First In Last Out) Ordered list of .
- Stack (stack) Is to limit the insertion and deletion of elements in a linear table Only on the same end of the linear table A special linear table for carrying out . The side that allows insertion and deletion , For the end of change , be called To the top of the stack (Top), The other end is the fixed end , be called At the bottom of the stack (Bottom).
- According to the definition of stack , First put the elements in the stack at the bottom of the stack , The last element put in is at the top of the stack , Deleting elements is the opposite , The last element is deleted first , The first element to be placed is deleted .
The illustration

Code
Notes have ideas to explain ~
package com.atguigu.stack;
import java.util.Scanner;
//author qij
public class ArrayStackDemo {
public static void main(String[] args) {
// Test it ArrayStack Whether it is right
// So let's create one ArrayStack object -> Presentation stack
ArrayStack stack = new ArrayStack(4);
String key = "";
boolean loop = true; // Controls whether to exit the menu
Scanner scanner = new Scanner(System.in);
while(loop) {
System.out.println("show: Indicates the display stack ");
System.out.println("exit: Exit procedure ");
System.out.println("push: Indicates adding data to the stack ( Push )");
System.out.println("pop: Indicates fetching data from the stack ( Out of the stack )");
System.out.println(" Please enter your choice ");
key = scanner.next();
switch (key) {
case "show":
stack.list();
break;
case "push":
System.out.println(" Please enter a number ");
int value = scanner.nextInt();
stack.push(value);
break;
case "pop":
try {
int res = stack.pop();
System.out.printf(" The data out of the stack is %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(" Program exit ~~~");
}
}
// Define a ArrayStack Presentation stack
class ArrayStack {
private int maxSize; // The size of the stack
private int[] stack; // Array , Array simulation stack , The data is in this array
private int top = -1;// top Represents the top of the stack , Initialize to -1
// Constructors
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
// Stack full
public boolean isFull() {
return top == maxSize - 1;
}
// The stack is empty
public boolean isEmpty() {
return top == -1;
}
// Push -push
public void push(int value) {
// First, judge whether the stack is full
if(isFull()) {
System.out.println(" Stack full ");
return;
}
top++;
stack[top] = value;
}
// Out of the stack -pop, Return the data at the top of the stack to
public int pop() {
// First, judge whether the stack is empty
if(isEmpty()) {
// Throw an exception
throw new RuntimeException(" The stack is empty , No data ~");
}
int value = stack[top];
top--;
return value;
}
// Show the stack [ Traversal stack ], Ergodic time , You need to display data from the top of the stack
public void list() {
if(isEmpty()) {
System.out.println(" The stack is empty , No data ~~");
return;
}
// You need to display data from the top of the stack
for(int i = top; i >= 0 ; i--) {
System.out.printf("stack[%d]=%d\n", i, stack[i]);
}
}
}
边栏推荐
- sprintf_s的使用方法
- QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
- Sentinel Alibaba open source traffic protection component
- 部署api_automation_test过程中遇到的问题
- Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
- PgSQL learning notes
- unittest.TextTestRunner不生成txt测试报告
- Implement strstr() II
- 微信小程序基础
- Latex在VSCODE中编译中文,使用中文路径问题解决
猜你喜欢

Alibaba cloud MFA binding Chrome browser

There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)

Warp shuffle in CUDA

flex九宫格布局

PgSQL学习笔记

Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)

pytest(1) 用例收集规则

ZZQ的博客目录--更新于20210601

unittest.TextTestRunner不生成txt测试报告

qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
随机推荐
Error "list" object is not callable in Web automatic switching window
Tensorrt command line program
js判断数组中对象是否存在某个值
构建学习tensorflow
Functions of tensorrt
Implement strstr() II
Alibaba cloud MFA binding Chrome browser
Function execution space specifier in CUDA
查询GPU时无进程运行,但是显存却被占用了
提高用户体验 防御性编程
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Fe - eggjs combined with typeorm cannot connect to the database
ctf-web之练习赛
Overload global and member new/delete
js中map和forEach的用法
VSCODE 安装LATEX环境,参数配置,常见问题解决
CUDA user object
No process runs when querying GPU, but the video memory is occupied
pytest(1) 用例收集规则