【IT笔试面试题整理】用两个栈实现队列

【试题描述】用两个栈实现一个队列。队列声明如下,请实现它的两个函数appendTail,deleteHead,分别完成在队列尾部插入节点和在头部删除节点。

【试题来源】未知

【参考代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stack>
using namespace std;

template <typename T>
class MyQueue
{

public:
void appendTail(const T& element);
T deleteHead();

private:
stack<T> inStack;
stack<T> outStack;
};

template <typename T>
void MyQueue<T>::appendTail(const T& element) {

inStack.push(element);
}

template <typename T>
T MyQueue<T>::deleteHead() {

if(outStack.empty()) {
//将inStack里面的数据放入outStack中
if(!inStack.empty()) {
while(!inStack.empty()) {
T element = inStack.top();
outStack.push(element);
inStack.pop();
}
} else {
throw "The Queue is empty.";
}
}
T element = outStack.top();
outStack.pop();

return element;
}

int main() {
MyQueue<int> queue;
queue.appendTail(1);
queue.appendTail(3);
queue.appendTail(5);
cout << queue.deleteHead() << endl;
cout << queue.deleteHead() << endl;
cout << queue.deleteHead() << endl;

return 0;
}