c++创建队列和栈的类

c++创建队列和栈的类

ID:1771663

大小:33.00 KB

页数:3页

时间:2017-11-13

c++创建队列和栈的类_第1页
c++创建队列和栈的类_第2页
c++创建队列和栈的类_第3页
资源描述:

《c++创建队列和栈的类》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、C++栈的类的实现和队列的实现由于最近面试老是面这么些题,索性自己再写了一遍下面是栈的类的实现的代码。typedefchar*ELEM;classStack{private:constintsize;inttop;ELEM*arraylist;public:Stack(constintsz):size(sz),top(-1){arraylist=newELEM[size];}~Stack(){delete[]arraylist;}boolisEmpty(){return(top==-1);}voidpush(constELEM&item);ELEMpop();};void

2、Stack::push(constELEM&item){assert(top!=size-1);//notfullarraylist[++top]=item;}ELEMStack::pop(){assert(!isEmpty());returnarraylist[top--];}测试的例子:#include#include"stack.h"usingnamespacestd;intmain(){Stacksa(5);ELEMex[5];ex[0]="hello";ex[1]="laojiang";ex[2]="donggua";ex[3]="every

3、thing";ex[4]="ok";for(inti=0;i!=5;i++){sa.push(ex[i]);}for(intj=0;j!=5;j++)cout<

4、rray;}voidenqueue(constELEM&item);ELEMdequeue();ELEMfirstValue()const;boolisEmpty()const{returnfront==rear;}};voidQueue::enqueue(constELEM&item){assert((rear+1)%size!=front);rear=(rear+1)%size;listarray[rear]=item;}ELEMQueue::dequeue(){assert(!isEmpty());front=(front+1)%size;returnlistarr

5、ay[front];}ELEMQueue::firstValue()const{assert(!isEmpty());returnlistarray[(front+1)%size];}以及测试的代码#include#include"queue.h"#includeusingnamespacestd;intmain(){Queueq(5);ELEMee[5];ee[0]="elem1";ee[1]="elem2";ee[2]="elem3";ee[3]="elem4";ee[4]="elem5";for(inti=0;i!=5;i++

6、)q.enqueue(ee[i]);for(intj=0;j!=5;j++){cout<

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。