您的位置:首页 > 职场人生

面试题13 - 用两个栈实现队列 【栈】

2013-01-29 13:52 155 查看
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define BUG cout << "here\n";
using namespace std;
const int N = 105;
struct Node {
int value;
Node* lchild;
Node* rchild;
};
template<typename T> class CQueue {
public :
CQueue(void);
~CQueue(void);
void appendTail(const T& Node);
T deleteHead();
private :
stack<T> stack1;
stack<T> stack2;
};
template<typename T> void CQueue<T>::appendTail(const T& element) {
stack1.push(element);
}
template<typename T> T CQueue<T>::deleteHead() {
if(!stack2.empty()) {
T tmp = stack2.top();
stack2.pop();
return tmp;
}
else {
if(stack1.empty()) {
cout << "异常" << endl;
}
while(!stack1.empty()) { // 我感觉这么写更快呢!
T tmp = stack1.top();
stack1.pop();
stack2.push(tmp);
}
T tmp = stack2.top();
stack2.pop();
return tmp;
}
}
int main() {
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: