stack.java

来自「java版公式计算器」· Java 代码 · 共 52 行

JAVA
52
字号
package calculator;

public class Stack<T> {      
    public StackNode<T> stackTop;      
    public int count;      
    public void push(T info) {      
        StackNode<T> node = new StackNode<T>();      
        node.info = info;      
        node.link = stackTop;      
        stackTop = node;      
        count++;      
    }       
          
    public void pop() {      
        if(stackTop == null) {      
            System.out.println("null stack");      
        } else {      
            stackTop = stackTop.link;      
            count--;      
        }      
     
    }      
          
    public boolean isEmpty() {      
        return count == 0;      
    }      
          
    public T top() {      
        if(stackTop == null) {      
            return null;      
        }      
        return stackTop.info;      
    }      
          
    public String toString(){      
        Stack<T> other = new Stack<T>();      
        while(count != 0){      
            T top = top();      
            pop();      
            other.push(top);      
        }      
        StringBuffer buf = new StringBuffer();      
        while(other.count !=0){      
            buf.append(other.top());      
            other.pop();      
        }      
        return buf.toString();      
    }      
     
}     
  
  

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?