当前位置:网站首页>"Niuke | daily question" template stack
"Niuke | daily question" template stack
2022-07-26 06:40:00 【Starry Luli】
Author's brief introduction : One likes to write , Sophomore rookie of planning major
Personal home page :starry Lu Li
First date :2022 year 7 month 13 Wednesday, Sunday
Last article :『 First issue article 』
Subscription column :『 Collection of Niuke brush questions 』
If the article helps you, remember to praise + Collect and support

『 Cattle guest | A daily topic 』 Template stack
1. A daily topic :AB1 【 Templates 】 Stack
Portal : A daily topic :AB1 【 Templates 】 Stack 
2. Test examples
Input :
6
push 1
pop
top
push 2
push 3
pop
Output :
1
error
3
3.Stack Class implementation
Let's start with a simple use Java Self contained Stack Class to achieve , In this way, we only need to process the input and judge the blank
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
String string;
Stack<Integer> stack = new Stack<Integer>();
while (n-- != 0) {
string = scanner.nextLine();
String cmp = string.substring(0, 3);
if (cmp.equals("pus")) {
int a = Integer.valueOf(string.substring(5, string.length()));
stack.push(a);
} else if (cmp.equals("pop")) {
if (!stack.isEmpty()) {
System.out.println(stack.pop());
} else {
System.out.println("error");
}
} else if (cmp.equals("top")) {
if (!stack.isEmpty()) {
System.out.println(stack.peek());
} else {
System.out.println("error");
}
}
}
}
}
4. Array implementation stack
It's not challenging to write with your own class ??? Then write one by yourself MyStack Class implements stack with array
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
scanner.nextLine();
String string;
MyStack stack=new MyStack(n+1);
while(n--!=0){
string=scanner.nextLine();
String cmp=string.substring(0,3);
if(cmp.equals("pus")){
int a=Integer.valueOf(string.substring(5,6));
stack.push(a);
}else if(cmp.equals("pop")){
stack.pop();
}else if(cmp.equals("top")){
stack.peek();
}
}
scanner.close();
}
}
class MyStack{
int[] data;// Stack with arrays
int maxSize;// Stack capacity
int top=-1;// Top pointer of stack - The number of elements in the stack
// Constructors
public MyStack(int maxSize) {
this.maxSize=maxSize;// Specify stack size in advance
this.data=new int[maxSize];// Initialize stack space
}
// Push
public void push(int val) {
if(this.top==this.maxSize) {
// Stack full - The number of elements in the stack is equal to the capacity of the stack
System.out.println("error");
}else {
// The stack is not full , Move the stack top pointer , Add new elements
data[++this.top]=val;
}
}
// The stack,
public void pop() {
// The stack is empty - The elements in the stack are 0
if(this.isEmpty()) {
System.out.println("error");
}else {
// The stack is not empty , Print top of stack elements , The top of the stack pointer moves down
System.out.println(data[this.top--]);
}
}
// Print top of stack elements
public void peek() {
// The stack is empty - The elements in the stack are 0
if(this.isEmpty()) {
System.out.println("error");
}else {
// The stack is not empty , Print top of stack elements
System.out.println(data[this.top]);
}
}
// Empty stack
public boolean isEmpty() {
if(this.top==-1) {
return true;
}else {
return false;
}
}
}

If the article helps you, remember to praise + Collect and support
边栏推荐
- Proxyman, a native high-performance packet capturing tool, is for you who love learning
- How does the national standard gb28181 protocol easygbs platform realize device video recording and set streaming IP?
- [C language] address book dynamic version and document version
- JVM class loading and GC garbage collection mechanism
- 【保姆级】包体积优化教程
- Why the server is stuck
- C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
- MySQL optimized index and index invalidation
- Alibaba cloud OSS binding custom domain name
- Map集合继承结构
猜你喜欢
![[pytorch] picture enlargement](/img/79/2eeefcb4623156f65957d78997e138.jpg)
[pytorch] picture enlargement

Overview of image classification of vision transformer must read series

Proxyman, a native high-performance packet capturing tool, is for you who love learning

Do it yourself smart home: intelligent air conditioning control

C# 可以利用反射给只读属性赋值吗?

快速排序(quick-sort)

UIToolkit中显示汉字

Sorting problem: bubble sort, select sort, insert sort

力扣5: 最长回文子串

Vim中删除^M
随机推荐
[pytorch] fine tuning technology
C language file operation
Slice and array of go
C# 可以利用反射给只读属性赋值吗?
If introduced according to the open source framework
带你搞透IO多路复用原理(select、poll和epoll)
Why the server is stuck
How to solve the crash when the easygbs platform edits the device management group?
Conda 虚拟环境envs目录为空
[pytorch] CNN practice - flower species identification
[day_070425] Fibonacci series
堆排序(heap-sort)
nuxt 配置主题切换
[C language] address book dynamic version and document version
Open includeexceptiondetailinfaults on the server (from servicebehaviorattribute or from & lt; servicedebug & gt; to configure behavior) to send exception information back
Map dictionary and constraints of go
mysql优化之索引及索引失效
[pytorch] picture enlargement
Force deduction 5: Longest palindrome substring
Show you the principle of IO multiplexing (select, poll and epoll)
