#include "Queue.h" // ====================测试代码====================
void Test(char actual, char expected)
{
if(actual == expected)
printf("Test passed.\n");
else
printf("Test failed.\n");
} int main(int argc, char* argv[])
{
CQueue<char> queue; queue.appendTail('a');
queue.appendTail('b');
queue.appendTail('c'); char head = queue.deleteHead();
Test(head, 'a'); head = queue.deleteHead();
Test(head, 'b'); queue.appendTail('d');
head = queue.deleteHead();
Test(head, 'c'); queue.appendTail('e');
head = queue.deleteHead();
Test(head, 'd'); head = queue.deleteHead();
Test(head, 'e'); return ;
}
QueueWithTwoStacks.cpp
// 面试题9:用两个栈实现队列
// 题目:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail
// 和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。 #pragma once
#include <stack>
#include <exception>
#include<stdio.h>
#include<stdexcept> using namespace std; 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> CQueue<T>::CQueue(void)
{
} template <typename T> CQueue<T>::~CQueue(void)
{
} template<typename T> void CQueue<T>::appendTail(const T& element)
{
stack1.push(element);
} template<typename T> T CQueue<T>::deleteHead()
{
if(stack2.size()<= )
{
while(stack1.size()>)
{
T& data = stack1.top();
stack1.pop();
stack2.push(data);
}
} if(stack2.size() == ){
std::logic_error ex("queue is empty");
throw std::exception(ex);
} T head = stack2.top();
stack2.pop(); return head;
}
Queue.h