Question

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.

Notes:

    • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, 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).

Solution

Java Queue Interface

Key to the solution is to use two queues. Queue is strictly First In, First Out.

Two Methods

Method 1: making push operation costly

push(s, x) // x is the element to be pushed and s is stack
1) Enqueue x to q2
2) One by one dequeue everything from q1 and enqueue to q2.
3) Swap the names of q1 and q2
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1. pop(s)
1) Dequeue an item from q1 and return it.

Method 2: making pop operation costly

push(s,  x)
1) Enqueue x to q1 (assuming size of q1 is unlimited). pop(s)
1) One by one dequeue everything except the last element from q1 and enqueue to q2.
2) Dequeue the last item of q1, the dequeued item is result, store it.
3) Swap the names of q1 and q2
4) Return the item stored in step 2.
// Swapping of names is done to avoid one more movement of all elements
// from q2 to q1.
 class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2; public MyStack(){
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
} // Push element x onto stack.
public void push(int x) {
queue1.add(x);
} // Removes the element on top of the stack.
public void pop() {
if (queue1.peek() == null)
return;
int length = queue1.size();
queue2 = new LinkedList<Integer>();
while (length > 1) {
queue2.add(queue1.remove());
length--;
}
queue1 = queue2;
} // Get the top element.
public int top() {
if (queue1.peek() == null)
return -1;
queue2 = new LinkedList<Integer>();
int length = queue1.size();
int lastElement = 0;
while (length > 0) {
lastElement = queue1.remove();
queue2.add(lastElement);
length--;
}
queue1 = queue2;
return lastElement;
} // Return whether the stack is empty.
public boolean empty() {
if (queue1.peek() == null)
return true;
return false;
}
}

Implement Stack using Queues 解答的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  5. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  6. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  7. Java for LeetCode 225 Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  9. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. phpWeb

    Ruby on Rails框架在REST走得很前,开发时默认都按照RESTful风格搭建. <RESTful Web Services>是本好书 SOAP

  2. 关于vs的lib文件和dll文件

    一.LIB文件概念 一个lib文件是obj文件的集合.当然,其中还夹杂着其他一些辅助信息,目的是为了让编译器能够准确找到对应的obj文件 二.与DLL的区别 (1)lib是编译时需要的,dll是运行时 ...

  3. 常用JS代码整理

    1: function request(paras) { 2: var url = location.href; 3: var paraString = url.substring(url.index ...

  4. AC Milan VS Juventus(模拟)

    AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

  5. [Git] --no-verify

    Somtimes, the project might set the commit message guide line, if your commit doesn't meet the requi ...

  6. iOS面试题集

    现在寒假时间在家里面无法敲代码了,但是自己总要找些事情来做,回头想想马上就要开始就业了,所以不免要参加面试,于是便开始了面试题集的浏览和探索,今天下午我看了一部分的面试题感觉还是挺实用的,所以以后还是 ...

  7. [服务器运维][Minecraft服务器搭建]

    参考资料: http://neekey.net/2016/02/01/%E5%A6%82%E4%BD%95%E7%94%A8%E9%98%BF%E9%87%8C%E4%BA%91ecs%E6%90%A ...

  8. 自定义上传按钮 <input type="file" name = "file"/> (将file隐藏在button下)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. SqlBulkCopy类进行大数据(一万条以上)插入测试

    好多天没写博客了,刚刚毕业一个多月! 关于上一篇博客中提到的,在进行批量数据插入数据库的时候可以通过给存储过程传递一个类型为Table的参数进行相关操作,在这个过程中本人没有进行效率的测试.后来查找发 ...

  10. iOS_block内存分析

    ----------------------MRC情况下Block内存分析---------------------------- 1.如果在block中使用全局变量,他为了持有这个变量,会将对应的对 ...