文档详情

用两个栈实现队列.pdf

发布:2017-09-15约5.39千字共4页下载文档
文本预览下载声明
RReeddeeaaggllee__ggbbff的的专专栏栏 jjaavvaa、、jjsspp、、wweebb 用用两两个个栈栈实实现现队队列列 分类: 数 结构 C/C++ 2013-11-09 22:23 153人阅读 评论 (0) 收藏 举报 //用两个栈实现队列,实现在队尾添加元素和在队列头部删除元素 #include iostream using namespace std; const int DEPTH = 20; //定义栈的深度 template class T struct stack //定义栈模版 { int top; //栈顶指针 T [DEPTH]; //定义一个数组存储栈中数 bool push(T member) { if (top = 20) { throw (栈已满 !); } [top] = member; top++; } T pop () { if (0 = top) { throw (栈为空!); } top--; return [top]; } }; template class T class CQueue //定义队列模版 { public: CQueue(void); ~CQueue(void); void appendTail(const T node); //队尾添加元素 T deleteHead(); //队头删除元素 private: stackT stack1; stackT stack2; }; template class T CQueueT::CQueue(void) { stack1.top = 0; stack2.top = 0; } 1 template class T CQueueT::~CQueue(void) { } template class T void CQueueT::appendTail(const T node) { try { stack1.push(node); } catch(char *e) { cout 队列已满 ! endl; } } template class T T CQueueT::deleteHead() { int size2 = stack2.top; //stack1中的元素个数 if (size2 == 0) {
显示全部
相似文档