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(...
分类:
其他好文 时间:
2015-07-11 16:48:45
阅读次数:
86
1 class Queue { 2 public: 3 stack stack1; 4 stack stack2; 5 6 Queue() { 7 // do intialization if necessary 8 } 9 10 void...
分类:
其他好文 时间:
2015-06-28 16:57:59
阅读次数:
120
题目意思:用队列实现栈,push(),pop(),top(),empty()思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop,再将目标队列变为另一个 ps:用栈实现队列,参考剑指offer 1 class Stack { 2 ....
分类:
其他好文 时间:
2015-06-17 10:52:17
阅读次数:
85
【要求】用两个队列queue1,queue2来实现栈的压栈和出栈功能。【分析】此问题类似于用两个栈实现队列的功能的思路,将队列queue1作为入栈专职,queue2作为中转,主要思路是,压栈时将所有元素全部进队queue1,出栈时由于要将queue1最后一个进来的元素输出,所以先将queue1除最后一个元素以外的所有元素转到queue2,出队输出最后一个元素后,将queue2所有元素出队,重新入队到...
分类:
其他好文 时间:
2015-05-27 19:05:45
阅读次数:
129
【题目】用两个栈实现一个队列,分别完成在队列尾部插入结点和在队列头部删除结点的功能。【分析】假设两个栈,栈1和栈2,来实现队列,栈1用来入队,栈2用来出队。
入队时,将元素全部压入栈1;
出队时,如果栈2不为空,就直接pop栈2,否则,就将栈1的所有元素pop到栈1里,再把栈2栈顶弹出。
由图所示,abcdef依次入队,压栈,出队时先将元素从s1弹出,压入s2,从s2出栈实现出队,即使后面再有...
分类:
其他好文 时间:
2015-05-27 10:23:41
阅读次数:
166
题目描述用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution
{
public:
void push(int node) {
stack1.push(node);
} int pop() {
if(stack2.empty()) {//第二个栈为空时
while(!stack1.empty()) {...
分类:
其他好文 时间:
2015-05-18 14:51:02
阅读次数:
102
规定你只能使用数据结构栈(支持pop, push),怎么样用栈来模拟一个队列的pop和push?...
分类:
编程语言 时间:
2015-05-13 10:31:03
阅读次数:
139
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 水题,直接上代码: class Solution
{
public: void push(int node) { stack1.push(node); } int pop() { if (stack2.e...
分类:
其他好文 时间:
2015-05-11 14:27:40
阅读次数:
114
题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。
template class CQueue{
public:
CQueue();
~CQueue();
void appendTail(const T &node);
T deleteHead();
private:
st...
分类:
其他好文 时间:
2015-05-08 22:05:03
阅读次数:
156