算法练习Stack(五)--P225、232

link on JianShu

栈stack
225. Implement Stack using Queues Easy
232. Implement Queue using Stacks Easy

这两道提互为实现,刚好放到一起学习。既然两种数据结构可以互相实现,可以看看有什么相似之处。

队列queue:先进先出FIFO;栈Stack 后进先出LIFO。只需要保证添加数据时排好顺序即可。

Implement the following operations of a stack using queues.

  • push(x) – Push element x onto stack.
  • pop() – Removes the element on top of the stack.
  • top() – Get the top element.
  • empty() – Return whether the stack is empty.

Example:

MyStack stack = new MyStack();

stack.push(1);
stack.push(2);  
stack.top();   // returns 2
stack.pop();   // returns 2
stack.empty(); // returns false\

Notes:

  • You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

实现好第一个方法即可。

private Queue<Integer> queue = new LinkedList<>();

    public void push(int x) {
        //重新放到第一个位置
        queue.add(x);
        // 移动后面的值
        for (int i=1; i<queue.size(); i++) {
            queue.add(queue.remove());
        }
    }

    public int pop() {
        return queue.remove();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }

Implement the following operations of a queue using stacks.

  • push(x) – Push element x to the back of queue.
  • pop() – Removes the element from in front of queue.
  • peek() – Get the front element.
  • empty() – Return whether the queue is empty.

Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);  
queue.peek();  // returns 1
queue.pop();   // returns 1
queue.empty(); // returns false

Notes:

  • You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

相同的地方:只需要实现好push方法即可。

    //转换位置:新添加的x要保证最后出来
    public void push(int x) {
        Stack<Integer> temp = new Stack<>();
        while(!stack.empty()){
            temp.push(stack.pop());
        }
        stack.push(x);
        while(!temp.empty()){
            stack.push(temp.pop());
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return stack.pop();
    }

    /** Get the front element. */
    public int peek() {
        return stack.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return stack.isEmpty();
    }
 
comments powered by Disqus