阅读背景:

java学习之—使用栈实现字符串数字四则运算

来源:互联网 
/**
 * 使用栈存储后缀表达式
 * Create by Administrator
 * 2018/6/13 0013
 * 下午 2:25
 **/
public class StackX {

    private int maxSize;
    private char[] stackArray;
    private int top;

    public StackX(int size)      // 构造函数
    {
        maxSize = size;
        stackArray = new char[maxSize];
        top = -1;
    }

    public void push(char j)     // 将项目放在堆栈的顶部
    {
        stackArray[++top] = j;
    }

    public char pop()            // 从堆栈顶部取项
    {
        return stackArray[top--];
    }

    public char peek()           // 从堆栈顶部查看
    {
        return stackArray[top];
    }

    public boolean isEmpty()    // 如果栈为空,则为true
    {
        return (top == -1);
    }

    public boolean isFull()     // 如果堆栈已满 true
    {
        return (top == maxSize - 1);
    }

    public int size()           // return size
    {
        return top + 1;
    }

    public char peekN(int n)     // peek at index n
    {
        return stackArray[n];
    }

    public void displayStack(String s) {
        System.out.print(s);
        System.out.print("Stack (bottom-->top): ");
        for (int j = 0; j < size(); j++) {
            System.out.print(peekN(j));
            System.out.print(' ');
        }
        System.out.println("");
    }

}
/**
 * 使用栈存储后缀表达式
 * Create by Administrato



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: