/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//
#include <iostream>
#include <exception>
#include <stack>
#include <cstdio>
#include <cstdlib>
template<typename T>
class CQueue
{
public:
CQueue(){}
~CQueue(){}
void appendToTail(const T&value);
T deleteHead();
private:
std::stack<T> stack1;
std::stack<T> stack2;
};
template<typename T>
void CQueue<T>::appendToTail(const T&value)
{
stack1.push(value);
}
template<typename T>
T CQueue<T>::deleteHead()
{
while(stack2.size() <= 0)
{
while(stack1.size() > 0)
{
T&value = stack1.top();
stack1.pop();
stack2.push(value);
}
}
if(stack2.size() == 0)
throw ("The Queue is empty.");
T head = stack2.top();
stack2.pop();
return head;
}
void test(int head_data, int GT_data)
{
if(head_data == GT_data)
std::cout << "Test pass." << std::endl;
else
std::cout << "Test fail." << std::endl;
}
void test_append_delete()
{
CQueue<int> q;
q.appendToTail(10);
q.appendToTail(9);
q.appendToTail(8);
int head = q.deleteHead();
test(head, 10);
head = q.deleteHead();
test(head, 9);
head = q.deleteHead();
test(head, 8);
q.appendToTail(7);
head = q.deleteHead();
test(head, 7);
CQueue<char> queue;
queue.appendToTail('a');
queue.appendToTail('b');
queue.appendToTail('c');
char head_2 = queue.deleteHead();
test(head_2, 'a');
head_2 = queue.deleteHead();
test(head_2, 'b');
queue.appendToTail('d');
head_2 = queue.deleteHead();
test(head_2, 'c');
queue.appendToTail('e');
head_2 = queue.deleteHead();
test(head_2, 'd');
head_2 = queue.deleteHead();
test(head_2, 'e');
}
int main(int argc, char*argv[])
{
test_append_delete();
return 0;
}