当前位置:网站首页>『牛客|每日一题』模板栈
『牛客|每日一题』模板栈
2022-07-26 06:34:00 【starry陆离】
作者简介:一位喜欢写作,计科专业大二菜鸟
个人主页:starry陆离
首发日期:2022年7月13日星期三
上期文章:『首期文章』
订阅专栏:『牛客刷题集锦』
如果文章有帮到你的话记得点赞+收藏支持一下哦

『牛客|每日一题』模板栈
1.每日一题:AB1 【模板】栈
2.测试示例
输入:
6
push 1
pop
top
push 2
push 3
pop
输出:
1
error
3
3.Stack类实现
先来个简单的用Java自带的Stack类来实现,这样我们就只需要处理输入和判空即可
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.数组实现栈
用自带的类写没挑战性???那就来自己写一个MyStack类用数组实现栈
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;//用数组实现栈
int maxSize;//栈的容量
int top=-1;//栈顶指针-栈中元素个数
//构造函数
public MyStack(int maxSize) {
this.maxSize=maxSize;//预先指定栈大小
this.data=new int[maxSize];//初始化栈空间
}
//入栈
public void push(int val) {
if(this.top==this.maxSize) {
//栈满-栈中的元素个数等于栈的容量
System.out.println("error");
}else {
//栈没满,移动栈顶指针,加入新元素
data[++this.top]=val;
}
}
//出栈操作
public void pop() {
//栈空-栈中的元素为0
if(this.isEmpty()) {
System.out.println("error");
}else {
//栈不空,打印栈顶元素,栈顶指针下移
System.out.println(data[this.top--]);
}
}
//打印栈顶元素
public void peek() {
//栈空-栈中的元素为0
if(this.isEmpty()) {
System.out.println("error");
}else {
//栈不空,打印栈顶元素
System.out.println(data[this.top]);
}
}
//栈的判空
public boolean isEmpty() {
if(this.top==-1) {
return true;
}else {
return false;
}
}
}

如果文章有帮到你的话记得点赞+收藏支持一下哦
边栏推荐
猜你喜欢

【Day_06 0423】不要二

Conda 虚拟环境envs目录为空

TPS Motion(CVPR2022)视频生成论文解读

BPG笔记(四)
![[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code](/img/9e/138e4b160fa9bd6486fac44a788d09.png)
[fault diagnosis] bearing fault diagnosis based on Bayesian optimization support vector machine with matlab code
![[pytorch] CNN practice - flower species identification](/img/af/81e2735ba385ba3d851e61a5fe2bef.png)
[pytorch] CNN practice - flower species identification
![[day05_0422] C language multiple choice questions](/img/cb/0019ec819480bd9f52055e726b323a.png)
[day05_0422] C language multiple choice questions

Go 的切片与数组

【Day05_0422】C语言选择题
![[pytorch] fine tuning technology](/img/d3/6d0f60fffd815f520f4b3880bd0ac7.png)
[pytorch] fine tuning technology
随机推荐
PG Vacuum 杂谈之 auto vacuum
『HarmonyOS』探索HarmonyOS应用
[day_060423] convert string to integer
English sentence pattern reference exclusive Edition - attributive clause
Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
【Day_05 0422】连续最大和
深度学习——CV、CNN、RNN
力扣——4. 寻找两个正序数组的中位数
The number of weeks of Oracle last year and this year, with the start time and end time
【C语言】文件操作
Go 的切片与数组
带你搞透IO多路复用原理(select、poll和epoll)
BigDecimal becomes negative
Gdown Access denied:Cannot retrieve the public link of the file.
Force buckle - 3. Longest substring without repeated characters
【Day_06 0423】不要二
Force buckle - 4. Find the median of two positive arrays
[day_050422] continuous maximum sum
信号处理系统综合设计-求解器函数的设计(连续和离散时间系统)
The real epidemic situation in the United States, do not easily "bottom" 2020-03-23

