栈的介绍
- 一个需求
-
栈是一个先入后出(FILO-First In Lst Out)的有序列表
-
最先放入的元素在栈底,最后放入的元素在栈顶,删除元素相反,最后放入的元素最先删除,最先放入的元素最后被取出
-
图解方式说明出栈入栈
栈的思路分析与代码实现
实现栈的思路分析
- 使用数组来模拟栈
- 定义一个top来表示栈顶,初始化为-1
- 入栈的操作,当有数据加入到栈时,top++;stack[top]=data
- 出栈的操作,int valve=stack[top];top–;return value;
import java.util.Scanner;
public class ArrayStackDemo {
public static void main(String[] args) {
//测试ArrayStack是否正确,先创建对象
ArrayStack as=new ArrayStack(4);
String key="";
boolean loop=true; //控制是否退出循环
Scanner input=new Scanner(System.in);
while(loop){
System.out.println("show:表示显示栈");
System.out.println("exit:退出程序");
System.out.println("push:添加数据");
System.out.println("pop:取出数据");
key=input.next();
switch (key){
case "show":
as.show();
break;
case "push":
System.out.println("请输入一个数");
int value=input.nextInt();
as.push(value);
break;
case "pop":
try{
int res=as.pop();
System.out.println("数据"+res);
}catch (Exception e){
}
break;
case "exit":
loop=false;
break;
}
}
}
}
//定义一个ArrayStack表示栈
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 isEmpty(){
return top==-1;
}
//入栈操作
public void push(int value){
//先判断栈是否满
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top]=value;
}
//出栈操作,将栈顶的数据返回
public int pop(){
//先判断栈是否为空
if(isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据");
}
int value=stack[top];
top--;
return value;
}
//显示栈的情况【遍历栈】。遍历时,需要从栈顶开始显示数据
public void show() {
if(isEmpty()){
System.out.println("栈空,没有数据");
return ;
}
for(int i=top;top>=0;top--){
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
}
栈实现综合计算器
使用栈完成计算一个表达式的结果
数栈:存数据
符号栈:存放运算符
思路分析:
- 通过一个index值(索引),来遍历我们的表达式
- 如果我们发现是一个数字,就直接入数栈
- 如果发现是一个运算符,就分如下情况来解决
1.如果发现当前的符号栈为空,就直接入栈
2.如果符号栈有操作符,就进行比较,如果当前的操作符的优先级小于或者等于栈中的操作符就需要从数栈中pop出两个数,再从符号栈pop出一个符号,进行运算,将得到的结果,入数栈,然后将当前的操作符入符号栈。如果当前的操作符的优先级大于栈中的操作符,就直接入符号栈。 - 当表达式扫描完毕,就顺序从数栈和符号栈pop出相应的数和符号,并运行。
- 最后在数栈只有一个数字,就是表达式的数值
public class Calculator {
public static void main(String[] args) {
String expression="3+2*6-2";
//创建两个栈
ArrayStack2 numStack=new ArrayStack2(10);
ArrayStack2 operStack=new ArrayStack2(10);
//扫描字符串
int index=0;
int num1=0;
int num2=0;
int oper=0;
int res=0;
char ch=' ';//将每次扫描到char保存到ch
//用while扫描字符串
while(true){
//一次得到的expression的每一个字符
ch=expression.substring(index,index+1).charAt(0);
//判断ch是数字还是符号
if(operStack.isOper(ch)){
if(!operStack.isEmpty()) {
if(operStack.priority(ch)<=operStack.priority(operStack.peek())){
num1=numStack.pop();
num2=numStack.pop();
oper=operStack.pop();
if (oper == '-') {
res=num2-num1;
}else res=numStack.cla(num1,num2,oper);
//把运算结果入数栈
numStack.push(res);
operStack.push(ch);
}else {
operStack.push(ch);
}
}else {
operStack.push(ch);
}
}else {
numStack.push(ch-48);
}
//让index+1,并判断是否扫描到最后
index++;
if(index>=expression.length()){
break;
}
}
//当表达式扫描完毕,就顺序从数栈和符号栈pop出相应的数和符号,并运行。
while (true){
//如果符号栈为空,计算最后的结果
if(operStack.isEmpty()){
break;
}
num1=numStack.pop();
num2=numStack.pop();
oper=operStack.pop();
if(oper=='-'){
res=num2-num1;
}else res=numStack.cla(num1,num2,oper);
numStack.push(res);
}
System.out.println("表达式的结果="+res);
}
}
//定义一个ArrayStack2表示栈,需要扩展功能
class ArrayStack2{
private int maxSize;//栈的大小
private int[] stack;//数组,数组模拟栈。数据就放在该数组
private int top=-1;//表示栈顶
//构造器
public ArrayStack2(int maxSize){
this.maxSize=maxSize;
stack=new int[maxSize];
}
//增加一个方法,可以返回当前栈顶的值
public int peek(){
return stack[top];
}
//栈满
public boolean isFull(){
return top==maxSize-1;
}
//栈空
public boolean isEmpty(){
return top==-1;
}
//入栈操作
public void push(int value){
//先判断栈是否满
if(isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top]=value;
}
//出栈操作,将栈顶的数据返回
public int pop(){
//先判断栈是否为空
if(isEmpty()){
//抛出异常
throw new RuntimeException("栈空,没有数据");
}
int value=stack[top];
top--;
return value;
}
//显示栈的情况【遍历栈】。遍历时,需要从栈顶开始显示数据
public void show() {
if(isEmpty()){
System.out.println("栈空,没有数据");
return ;
}
for(int i=top;top>=0;top--){
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
//返回运算符的优先级,优先级由程序员确定,优先级用数字表示
public int priority(int oper){
if(oper=='*'||oper=='/'){
return 1;
}else if(oper =='+'||oper=='-'){
return 0;
}else{
return -1;
}
}
//判断是不是一个操作符
public boolean isOper(char val){
return val=='+'||val=='-'||val=='/'||val=='*';
}
//计算方法
public int cla(int num1,int num2,int oper){
int res=0;//res用于存放计算的结果
switch (oper){
case '+':
res=num1+num2;
break;
case '-':
res=num1-num2;
break;
case '*':
res=num1*num2;
break;
case '/':
res=num1/num2;
break;
}
return res;
}
}