当前位置:网站首页>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]);
}
}
}
边栏推荐
- Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
- Pytest (1) case collection rules
- Thread hierarchy in CUDA
- Sentinel rules persist to Nacos
- Fe - use of weex development weex UI components and configuration use
- Solution to the black screen of win computer screenshot
- js数组的常用的原型方法
- Huawei mindspire open source internship machine test questions
- The default Google browser cannot open the link (clicking the hyperlink does not respond)
- ctf三计
猜你喜欢
AWD学习
js中map和forEach的用法
Pytest (1) case collection rules
The intern left a big hole when he ran away and made two online problems, which made me miserable
Linux MySQL 5.6.51 Community Generic 安装教程
Pytest (2) mark function
How to try catch statements that return promise objects in JS
Idea announced a new default UI, which is too refreshing (including the application link)
Vscode installation, latex environment, parameter configuration, common problem solving
Blog directory of zzq -- updated on 20210601
随机推荐
Deployment API_ automation_ Problems encountered during test
2020-9-23 use of QT timer qtimer class.
JS modification element attribute flipping commonly used in selenium's Web Automation
pytest(2) mark功能
Sentinel rules persist to Nacos
Redis - hot key issues
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
FE - Eggjs 结合 Typeorm 出现连接不了数据库
Selenium+msedgedriver+edge browser installation driver pit
selenium+msedgedriver+edge浏览器安装驱动的坑
table 组件指定列合并行方法
The use of regular expressions in JS
Idea announced a new default UI, which is too refreshing (including the application link)
Build learning tensorflow
Apt command reports certificate error certificate verification failed: the certificate is not trusted
Sublime text configuring PHP compilation environment
Name six schemes to realize delayed messages at one go
Eggjs -typeorm treeenity practice
Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
自学table au