用队列实现stack
2015-06-13 06:59
225 查看
用queue实现stack
题目要求: Implement the following operations
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.
其中 use only standard operations of a queue -- which means only
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).
方法1:用两个queue
用两个queue的话很容易实现,类似于用stack实现queue的思路。其中一个queue主要用于改变element顺序时,临时存储element。
Solution 1 : making pop operation costly
pop和top时,因为需要取最后一个,需要把q中前面的移动到q2中。
In push operation, the new element is always enqueued to q1.
In pop() operation, if q2 is empty then all the elements except the last, are moved to q2.
Finally the last element is dequeued from q1 and returned.
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.
View Code
方法2:使用一个queue
在方法1中的push costly方法的基础上优化,减少空间
在pop and top costly 方法基础上优化,用一个元素mark peek即可
题目要求: Implement the following operations
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.
其中 use only standard operations of a queue -- which means only
push to back,
peek/pop from front,
size, and
is emptyoperations 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).
方法1:用两个queue
用两个queue的话很容易实现,类似于用stack实现queue的思路。其中一个queue主要用于改变element顺序时,临时存储element。
Solution 1 : making pop operation costly
pop和top时,因为需要取最后一个,需要把q中前面的移动到q2中。
In push operation, the new element is always enqueued to q1.
In pop() operation, if q2 is empty then all the elements except the last, are moved to q2.
Finally the last element is dequeued from q1 and returned.
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 { //update when push Queue<Integer> q = new LinkedList<Integer>(); Queue<Integer> q2 = new LinkedList<Integer>(); // Push element x onto stack. public void push(int x) { q2.offer(x); while(!q.isEmpty()){ q2.offer(q.poll()); } //swap q and q2 name or push back to q while(!q2.isEmpty()){ q.offer(q2.poll()); } } // Removes the element on top of the stack. public void pop() { q.poll(); } // Get the top element. public int top() { return q.peek(); } // Return whether the stack is empty. public boolean empty() { return q.isEmpty(); } }
View Code
方法2:使用一个queue
在方法1中的push costly方法的基础上优化,减少空间
class MyStack { //update when push Queue<Integer> q = new LinkedList<Integer>(); // Push element x onto stack. public void push(int x) { q.offer(x); int size = q.size(); while(size > 1){ q.offer(q.poll()); size--; } } // Removes the element on top of the stack. public void pop() { q.poll(); } // Get the top element. public int top() { return q.peek(); } // Return whether the stack is empty. public boolean empty() { return q.isEmpty(); } }
在pop and top costly 方法基础上优化,用一个元素mark peek即可
class MyStack { //update when pop and top operation //use 1 queue Queue<Integer> q = new LinkedList<Integer>(); private Integer topElement = null; // Push element x onto stack. public void push(int x) { topElement = x; q.offer(x); } // Removes the element on top of the stack. public void pop() { int size = q.size(); Integer second = null; while(size > 1){//all element to q2 except last element second = q.poll(); q.offer(second); size--; } topElement = second;//the second last element q.poll();//pop the last element } // Get the top element. public int top() { return topElement; } // Return whether the stack is empty. public boolean empty() { return q.isEmpty(); } }
相关文章推荐
- 填坑计划(未完)
- 探秘重编译(Recompilations)(2/2)
- windows系统自带便笺/截图/Tablet PC损坏怎么修复?
- windows 7系统自带便签消失了怎么办?
- windows系统电脑字体突然变细了怎么办?
- 5大常用网站推广方法 吸引大流量
- 我与学院的点点滴滴
- mysql 的表名和列名设计时应该注意的一个问题
- createrepo使用(三)
- creatrepo使用(二)
- 亚投行国家分工非常明确,一路一带是欧亚大融合之路,欢呼吧!
- DHCP中继服务器
- HDU--杭电--3572--Task Schedule--最大流
- 智能DNS服务器
- Search in Rotated Sorted Array
- kickstart是什么
- repodata创建本地YUM仓库(一)
- PXE+kicstart+Vsftpd/NFS/HTTP无人值守批量网络安装
- 量子计算与通讯的基本原理(量子纠缠)
- MySQL+DRBD+heartbeat+LVS+keepalived