栈是一种基本的数据结构,按照最后进入栈的数据最先出栈(后进先出,Last In First Out,LIFO)的规则管理数据。

操作

  • push(x):在栈顶部添加元素\(x\)
  • pop():从栈顶部取出元素
  • isEmpty():检查栈是否为空
  • isFull():检查栈是否已满

规则
数据中最后加入的元素最先被取出。


刚刚说的都是栈的逻辑结构,栈的实现可以使用数组或者链表。下面的例子将用数组来实现一个栈并进行操作。

以AOJ(Aizu Online Judge)中的一题逆波兰表示法为例进行说明。题目链接 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_A

Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.
Write a program which reads an expression in the Reverse Polish notation and prints the computational result.
An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.
You can assume that +, - and * are given as the operator and an operand is a positive integer less than \(10^6\)

Output

Print the computational result in a line.

Constraints

2 ≤ the number of operands in the expression ≤ 100
1 ≤ the number of operators in the expression ≤ 99
-1 × \(10^9\) ≤ values in the stack ≤ \(10^9\)
Sample Input 1
1 2 +
Sample Output 1
3
Sample Input 2
1 2 + 3 4 - *
Sample Output 2
-3

题目大意就是给定一个逆波兰表达式,求出计算结果。

用逆波兰表示法描述的算式可以借助栈进行运算。从算式开头逐一读取字符串,遇到数字压入栈,遇到运算符便从栈中取出该运算符需要的操作数,比如“+”需要取出两个操作数,然后计算结果后再压入栈即可。最终栈中剩下的数值便是答案。

为了用数组表示栈,没有用Java中的集合类,只自行定义了栈的出栈和入栈操作,以及栈顶指针top和数组。

参考代码如下:

import java.util.Scanner;
 
public class Main {
 
    // 因为内存空间充足,所以num[0]一直为空,正常会设为-1
    static int top = 0;
    static int[] num = new int[1000];
 
    //在入栈时应该检查是否栈满,此处省略了
    public static void push(int x){
 
        num[++top] = x;
    }
    
    //在出栈时应该检查是否栈空,也省略了
    public static int pop(){
 
        top--;
        return num[top+1];
    }
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
 
        String[] strings = str.split(" ");
        for (int i=0; i<strings.length; i++){
            if (strings[i].equals("+")){
                int a = pop();
                int b = pop();
                push(a+b);
            }
            else if (strings[i].equals("-")){
                int a = pop();
                int b = pop();
                push(b-a);
            }
            else if (strings[i].equals("*")){
                int a = pop();
                int b = pop();
                push(a*b);
            }
            else {
                push(Integer.parseInt(strings[i]));
            }
        }
 
        System.out.println(pop());
    }
}

转载于:https://www.cnblogs.com/WanJiaJia/p/7940992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值