当前位置:网站首页>"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
边栏推荐
- JVM class loading and GC garbage collection mechanism
- CS5801_HDMI转EDP优势替代LT6711A方案
- [C language] address book dynamic version and document version
- A tool for quickly switching local host files -- switchhosts
- IP day 10 notes - BGP
- Display Chinese characters in uitoolkit
- 『HarmonyOS』DevEco的下载安装与开发环境搭建
- 『牛客|每日一题』 栈的压入、弹出序列
- Go 的切片与数组
- Upgrade appium automation framework to the latest 2.0
猜你喜欢

IP day 10 notes - BGP

A tool for quickly switching local host files -- switchhosts

将一个正整数分解质因数,要求分解成尽可能小的多个的因数。

Merge_sort

力扣——3. 无重复字符的最长子串

归并排序(merge_sort)

Do you think you are a reliable test / development programmer? "Back to the pot"? Surface and reality

28. Implement strStr()实现 strStr()
![[C language] file operation](/img/19/5bfcbc0dc63d68f10155e16d99581c.png)
[C language] file operation

RNN recurrent neural network
随机推荐
Resume considerations
打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 &lt;serviceDebug&gt; 配置行为)以便将异常信息发送回
If introduced according to the open source framework
Merge_sort
[pytorch] fine tuning technology
Deep learning - CV, CNN, RNN
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
Gdown Access denied:Cannot retrieve the public link of the file.
What is KVM? What is KVM virtual machine?
Gdown Access denied:Cannot retrieve the public link of the file.
Tiktok web s_ v_ web_ Analysis and implementation of ID parameter generation
力扣——3. 无重复字符的最长子串
Children's programming electronic society graphical programming level examination scratch level 1 real problem analysis (multiple choice) June 2022
Conda 虚拟环境envs目录为空
[untitled]
性能测试包括哪些方面?分类及测试方法有哪些?
What is the concept and purpose of white box testing? And what are the main methods?
【Web3 系列开发教程——创建你的第一个 NFT(4)】NFTs 可以给你带来什么
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)
Use scanner to get multiple data types from the keyboard
