当前位置:网站首页>栈的概念和操作
栈的概念和操作
2022-04-22 12:20:00 【Henry Jekyll】
一、什么是栈
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。
特点:后进先出

二、Java虚拟机栈
(1)栈帧:调用函数是为其开辟的内存叫栈帧。

三、Java基本库中栈的基本使用
public static void main(String[] args) {
Stack<Integer> stack= new Stack<Integer>();
//入栈
stack.push(3);//栈底
stack.push(4);
stack.push(5);
stack.push(7);//栈顶
//出栈:弹出栈顶元素
System.out.println(stack.pop());//7
//再弹一次,此时栈顶元素为5了,如下。
System.out.println(stack.pop());
//获取栈顶元素但不删除,这时的栈顶元素以及是4了
System.out.println(stack.peek());
//判断栈顶元素是否为空
System.out.println(stack.empty());
Stack<Integer> stack1=new Stack<>();
System.out.println(stack1.empty());
//获取栈中的元素的位置,栈顶为1号,此时stack中有3,4两个元素,所以4元素的位置为1号
System.out.println(stack.search(4));
//使用父类的方法,stack继承自Vector
System.out.println(stack.isEmpty());
}
四、中缀表达式、后缀表达式、前缀表达式的转化。
(1)、后缀表达式求值:

逆波兰表达式求值:
Stack<Integer> stack=new Stack<>();
public int evalRPN(String[] tokens) {
for(int i=0;i<tokens.length;i++){
String val=tokens[i];
if(isOperation(val)==false){
stack.push(Integer.parseInt(val))//通过Integer的方法转成数字;
}else {
int num2=stack.pop();
int num1=stack.pop();
switch(val){
case "+":
stack.push(num1+ num2);
break;
case "-":
stack.push(num1- num2);
break;
case "*":
stack.push(num1 * num2);
break;
case "/":
stack.push(num1/ num2);
break;
default:
}
}
}
return stack.pop();
}
private boolean isOperation(String x){
if(x.equals("+")||x.equals("-")||x.equals("*")||x.equals("/")){
return true;
}
return false;
}
五、用顺序表实现一个栈
//利用顺序表实现一个栈
class MyStack{
private int[] array=new int[5];//假设这是存放5个元素的栈。
private int size=0;
public void push(int value){
this.array[size]=value;
size++;
}
public int pop(){
int ret=this.array[size-1];
size--;
return ret;
}
public int peek(){
int ret=this.array[size-1];
return ret;
}
public boolean isEmpty(){
if (size==0){
return true;
}
return false;
}
public int getSize(){
return size;
}
}
public class Demo3 {
public static void main(String[] args) {
MyStack myStack=new MyStack();
//入栈
myStack.push(5);
myStack.push(6);
myStack.push(8);
myStack.push(3);
myStack.push(2);
//计算此时栈的长度
System.out.println(myStack.getSize());
//弹出栈顶元素2
System.out.println(myStack.pop());
//弹出栈顶元素3
System.out.println(myStack.pop());
//弹出栈顶元素8,但不删除
System.out.println(myStack.peek());
//判断栈是否为空
System.out.println(myStack.isEmpty());
//新创一个栈,不放元素
MyStack myStack1=new MyStack();
System.out.println(myStack1.isEmpty());
}
}
六、括号匹配问题

public class Demo4 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String str=scanner.nextLine();
System.out.println(isValid(str));
}
public static boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch=='('||ch=='['||ch=='{'){
stack.push(ch);
}else{
if(stack.empty()){
return false;
}
char top=stack.peek();
if(top=='('&&ch==')'||top=='{'&&ch=='}'||top=='['&&ch==']'){
stack.pop();
}else{
return false;
}
}
}
if(!stack.empty()){
return false;
}
return true;
}
}
七、判断某个数组是否是正确的出栈顺序。

public class Demo5 {
public static void main(String[] args) {
int[] A={
1,2,3,4,5};
int[] B={
4,5,3,2,1};
System.out.println(IsPopOrder(A, B));
}
public static boolean IsPopOrder(int [] pushA,int [] popA) {
Stack <Integer> stack=new Stack<>();
int j=0;
for(int i=0;i<pushA.length;i++){
stack.push(pushA[i]);
while(j<popA.length&&!stack.empty()&&stack.peek()==popA[j]){
stack.pop();
j++;
}
}
return stack.empty();
}
}
八、设计一个获取栈中最小元素的栈
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack() {
stack=new Stack<>();
minStack=new Stack<>();
}
public void push(int val) {
stack.push(val);
if(!minStack.empty()){
int top=minStack.peek();
if(val<=top){
minStack.push(val);
}
}else{
minStack.push(val);
}
}
public void pop() {
int popVal=stack.pop();
if(!minStack.empty()){
int top=minStack.peek();
if(top==popVal){
minStack.pop();
}
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
版权声明
本文为[Henry Jekyll]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_60391870/article/details/124308721
边栏推荐
- 【并发编程049】说说重排序的分类?
- Distributed transaction and lock
- Cvpr2022 𞓜 3D reconstruction of mobile end hand
- 【深入理解TcaplusDB技术】批量删除列表指定位置数据接口说明——[List表]
- Circuit experiment -- Experiment 4 Davinan theorem and Norton theorem
- 通俗易懂地给女朋友讲:线程池的内部原理
- Remember a study of Intranet penetration into the shooting range
- Electrician Lecture 2
- What role does RF chip play in mobile phone?
- "Stack overflow Chinese content" is finally here! Invite you to experience feedback and get a good gift
猜你喜欢
![【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
【深入理解TcaplusDB技术】删除列表所有数据接口说明——[List表]

【生活中的逻辑谬误】以暴制暴和压制理性

Redis新版本发布,你还认为Redis是单线程?

基于J2EE的房屋租赁系统的设计与实现.rar(论文+项目源码+数据库文件)
![[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]](/img/ed/cccd5dee09d2f0a3e6c788bd265b36.png)
[in depth understanding of tcallusdb technology] description of data interface of designated location in replacement list - [list table]

Set the sliding wheel in vscode to change the font size

【并发编程053】双重检查锁的变量为什么使用volatile变量
![[concurrent programming 050] type and description of memory barrier?](/img/2b/9035c20c45f3f33c97639341315f72.png)
[concurrent programming 050] type and description of memory barrier?

LeetCode 206、反转链表

低频(LF)RFID 智能终端
随机推荐
Difference between redis setex and set
Comparison of data protection modes between Oracle data guard and Jincang kingbasees cluster
LeetCode 617、合并二叉树
Low frequency (LF) RFID intelligent terminal
【keras入门】MNIST数据集分类
Ner brief overview
NLP dataset collation (updating)
STM32 中的0UL或1UL是什么意思?
LeetCode 206、反转链表
Set the sliding wheel in vscode to change the font size
带你详细入门华为云会议【玩转华为云】
Canvas series tutorial 01 - line, triangle, polygon, rectangle, palette
通俗易懂地给女朋友讲:线程池的内部原理
Heap overflow of kernel PWN basic tutorial
射频芯片在手机上起到什么作用?
Setting policy of thread pool size
【分布式】分布式事务与锁
NLP数据集整理(更新中)
Cas 4 - 1.7: transfert de fichiers (et recherche d'ensembles)
[deeply understand tcallusdb technology] delete all data interface descriptions in the list - [list table]