一、栈的介绍
- 栈(stack)作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。
- 入栈:
- 出栈
二、应用场景
- 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,直到子程序执行完后再将地址取出,以回到原来的程序中。
- 处理递归调用:和子程序的调用类似,只是除了储存下一个指令的地址外,也将参数、区域变量等数据存入堆栈中。
- 表达式的转换[中缀表达式转后缀表达式]与求值(实际解决)。
- 二叉树的遍历。
- 图形的深度优先(depth一first)搜索法。
三、栈的模拟实现
1. 数组模拟栈
class ArrayStack{
private int maxsize;
private int[] stack;
private int top = -1 ;
public ArrayStack(int maxsize){
this.maxsize = maxsize;
stack = new int[maxsize];
}
public boolean isFull(){
return top == maxsize-1;
}
public boolean isEmply(){
return top == -1;
}
public void push(int data){
if(isFull()){
System.out.println("栈已经满了");
return;
}
top++;
stack[top] = data;
}
public void pop(){
if(isEmply()){
System.out.println("栈为空");
return;
}
System.out.println("出栈数据="+stack[top]);
top--;
}
public void showStack(){
for (int i = top ; i>= 0;i--){
System.out.println(stack[i]);
}
}
}
class LinkedListStack {
private Person top = new Person(-1);
public void push(Person person){
Person temp = top;
if(top.getNext() == null){
top.setNext(person);
return;
}
Person cur = top.getNext();
top.setNext(person);
person.setNext(cur);
}
public void pop(){
System.out.println("节点为:"+top.getNext().getNo());
top = top.getNext();
}
public void show(){
if(top.getNext() == null){
System.out.println("栈空。。。");
return;
}
Person temp = top;
while(temp.getNext() != null){
System.out.println("节点为:"+temp.getNext().getNo());
temp = temp.getNext();
}
}
class Person {
private int no;
private Person next;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Person getNext() {
return next;
}
public void setNext(Person next) {
this.next = next;
}
public Person(int no) {
this.no = no;
}
@Override
public String toString() {
return "Person{" +
"no=" + no +
'}';
}
四、栈实现综合计算器(中缀表达式)
1. 分析
- 通过一个 index 值(索引),来遍历我们的表达式
- 如果我们发现是一个数字, 就直接入数栈
- 如果发现扫描到是一个符号, 就分如下情况
- 如果发现当前的符号栈为 空,就直接入栈
- 如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符, 就需要从数栈中pop出两个数,在从符号栈中pop出一个符号,进行运算,将得到结果,入数栈,然后将当前的操作符入符号栈, 如果当前的操作符的优先级大于栈中的操作符, 就直接入符号栈.
- 当表达式扫描完毕,就顺序的从 数栈和符号栈中pop出相应的数和符号,并运行.
- 最后在数栈只有一个数字,就是表达式的结果
2. 代码实现
public class Calculator{
public static void main(String[] args) {
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
String expression = "3+3*6+9";
int num1 = 0;
int num2 = 0;
int index= 0;
int res= 0;
int oper = 0;
char ch = ' ';
String keepNum="";
while(true){
if (index>=expression.length()){
break;
};
ch = expression.substring(index, index + 1).charAt(0);
if (operStack.isOper(ch)){
if(operStack.isEmply()){
operStack.push(ch);
}else{
if (operStack.priority(ch)<=operStack.priority(operStack.peek())){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = operStack.cal(num1,num2,oper);
numStack.push(res);
operStack.push(ch);
}else{
operStack.push(ch);
}
}
}else {
keepNum += ch;
if (index==expression.length()-1){
numStack.push(Integer.parseInt(keepNum));
}else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
}
while (true){
if (operStack.isEmply()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = operStack.cal(num1, num2, oper);
numStack.push(res);
}
System.out.println(numStack.pop());
}
}
class ArrayStack2 {
private int maxsize;
private int[] stack;
private int top = -1;
public ArrayStack2(int maxsize) {
this.maxsize = maxsize;
stack = new int[maxsize];
}
public boolean isFull() {
return top == maxsize - 1;
}
public boolean isEmply() {
return top == -1;
}
public void push(int data) {
if (isFull()) {
System.out.println("栈已经满了");
}
top++;
stack[top] = data;
}
public int peek(){
return stack[top];
}
public int pop() {
if (isEmply()) {
throw new RuntimeException("栈为空");
}
int a= stack[top];
System.out.println("出栈数据=" + a);
top--;
return a;
}
public void showStack() {
for (int i = top; i >= 0; i--) {
System.out.println(stack[i]);
}
}
public int priority(int ch) {
if (ch == '*' || ch == '/') {
return 1;
} else if (ch == '+' || ch == '-') {
return 0;
} else {
return -1;
}
}
public boolean isOper(char ch) {
return ch == '*' || ch == '/' || ch == '+' || ch == '-';
}
public int cal(int num1, int num2, int oper) {
int res = 0;
switch (oper) {
case '*':
res = num1 * num2;
break;
case '+':
res = num1 + num2;
break;
case '-':
res = num2 - num1;
break;
case '/':
res = num2 / num1;
break;
}
return res;
}
}