Loading

225. [栈]用队列实现栈

225. 用队列实现栈

方法一:单链表实现栈

class MyStack {

    LinkedList<Integer> stack ;
    /** Initialize your data structure here. */
    public MyStack() {
        stack = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        stack.offerLast(x);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return stack.pollLast();
    }
    
    /** Get the top element. */
    public int top() {
        return stack.peekLast();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }
}
posted @ 2020-10-24 11:18  上海井盖王  阅读(78)  评论(0)    收藏  举报