程序员不能不懂栈

PS:
1.这篇以数组为方式写的栈其实是为了给:文章地址作补充关于栈的详细解释可以参考这篇文章
2.下面有关于逆波兰式求解数学表达式的代码


import java.util.*;
public class arithmetic {
     
	public static void main(String[] args){
     
		Scanner sc=new Scanner(System.in);
		stack Stack=new stack(4); //创建一个栈的对象
		Stack.pop();//在空的情况下出个栈试试
		for(int i=0;i<4;i++) {
     //压栈
			Stack.push(sc.nextInt());
		}
		Stack.push(4);//在栈已经满的情况下压栈
		
		for(int i=0;i<4;i++) {
     //出栈
			Stack.pop();
		}
		System.out.println("This stack whether empty "+Stack.Empty());
	}
}
class stack{
     
	int size;  //栈大小
	int[] arrstack;//构建的栈数组
	int top=-1;//栈顶指针用来记录目前栈里面的位置
	public stack(int size) {
        // 建立构造方法同时也是栈的大小输入点
		this.size=size;
		this.arrstack=new int[this.size];
	}
	public boolean Empty() {
      //判断栈大小是否为空
		return this.top==-1;
	}
	public boolean Full() {
      //判断栈是否以满
		return this.top==this.size-1;
	}
	public void push(int value) {
      //压栈
		if(Full()) {
      //压栈前先判断是否栈里面元素已满
			System.out.println("sorry stack is full");
			return ;
		}
		top++;
		arrstack[top]=value; //把刚输入的元素放到栈顶
	}
	public void pop() {
      //出栈
		if(Empty()) {
      //判断栈是否为空
			System.out.println("sorry stack is empty");
			return ;
		}
		System.out.println(arrstack[top]);
		top--; //将指针下移
		
	}
	
}

逆波兰式求解数学表达式

import java.util.*;
public class arithmetic {
     
	public static void main(String[] args){
     
		Scanner sc=new Scanner(System.in);
		String equation=sc.next();
		stack Stack=new stack(equation.length());

		char[] arr=equation.toCharArray();//创建字符型数组因为一会要判断+,-,*,/
		for(char i:arr) {
     
			int tmp=Integer.valueOf(i);//寻找数组元素的ASCLL主要是判断是数组还是运算符
			if(tmp>=48&&tmp<=57) {
     //如果是数字就转为整型存入栈里
				String t=String.valueOf(i);
				int int_t=Integer.valueOf(t);
				Stack.push(int_t);
			}
			else {
     
				if(tmp==Integer.valueOf('-')) {
     
					
					int second=Stack.pop();//先出来的是减数
					int first=Stack.pop();//接着是被减数
					int result=first-second;
					Stack.push(result);//将结果入栈
				}
				if(tmp==Integer.valueOf('+')) {
     
					int second=Stack.pop();
					int first=Stack.pop();
					int result=first+second;
					Stack.push(result);
				}
				if(tmp==Integer.valueOf('*')) {
     
					int second=Stack.pop();
					int first=Stack.pop();
					int result=first*second;
					Stack.push(result);
				}
				if(tmp==Integer.valueOf('/')) {
     
					int second=Stack.pop();
					int first=Stack.pop();
					int result=first/second;
					Stack.push(result);
				}
				
			}
		}
		System.out.println(Integer.valueOf(Stack.pop()));
		
	}
}
class stack{
     
	int size;  //栈大小
	int[] arrstack;//构建的栈数组
	int top=-1;//栈顶指针用来记录目前栈里面的位置
	public stack(int size) {
        // 建立构造方法同时也是栈的大小输入点
		this.size=size;
		this.arrstack=new int[this.size];
	}
	public boolean Empty() {
      //判断栈大小是否为空
		return this.top==-1;
	}
	public boolean Full() {
      //判断栈是否以满
		return this.top==this.size-1;
	}
	public void push(int value) {
      //压栈
		if(Full()) {
      //压栈前先判断是否栈里面元素已满
			System.out.println("sorry stack is full");
			return ;
		}
		top++;
		arrstack[top]=value; //把刚输入的元素放到栈顶
	}
	public int pop() {
      //出栈
		if(Empty()) {
      //判断栈是否为空
			System.out.println("sorry stack is empty");
			return -1;
		}
		int ans=arrstack[top];
		//System.out.println(arrstack[top]);
		top--; //将指针下移
		return ans;
	}
	
}

你可能感兴趣的:(算法,栈,算法)