欢迎来到天天文库
浏览记录
ID:6383840
大小:191.50 KB
页数:13页
时间:2018-01-12
《资料-树的孩子兄弟表示法及相关操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、[Copytoclipboard]ViewCodeCPP1234567891011121314151617181920212223//basic.h常用头文件 #include#include#include#include#include#include//#include#include//#include#includeusingnamespacestd; //函数结果状态代码#defin
2、eTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1 typedefintStatus;typedefintBoolean;[Copytoclipboard]ViewCodeCPP123456789101112//LinkQueue-define.h队列的链式存储结构 typedefstructQNode{QElemTypedata;QNode*next;}*QueuePtr; structLinkQueue{QueuePtrfront,rear;};13[Copytoclipboard]ViewCodeCP
3、P12345678910111213141516171819202122232425262728293031323334353637383940414243//LinkQueue-operations.h链队列的基本操作 StatusInitQueue(LinkQueue&Q){//构造一个空队列Q。if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))exit(OVERFLOW);Q.front->next=NULL;returnOK;} StatusDestroyQueue(LinkQueue&Q){//销毁队列。whil
4、e(Q.front){Q.rear=Q.front->next;free(Q.front);Q.front=Q.rear;}returnOK;} StatusClearQueue(LinkQueue&Q){//清空队列。QueuePtrp,q;Q.rear=Q.front;p=Q.front->next;Q.front->next=NULL;while(p){//释放每一个结点。q=p;p=p->next;free(q);}returnOK;} StatusQueueEmpty(LinkQueueQ){//判空。if(Q.rear==Q.front)returnTRUE;els
5、ereturnFALSE;}134445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 intQueueLength(LinkQueueQ){//求队列长度。inti=0;QueuePtrp;p=Q.front;while(Q.rear!=p){i++;p=p->next;}returni;} StatusGetHead(LinkQueueQ,QElemType&e){//取队头元素,用e返回其值。QueuePtrp;if(Q.front==Q.rear)r
6、eturnERROR;p=Q.front->next;e=p->data;returnOK;} StatusEnQueue(LinkQueue&Q,QElemTypee){//将元素e入队。QueuePtrp;if(!(p=(QueuePtr)malloc(sizeof(QNode))))exit(OVERFLOW);p->data=e;p->next=NULL;Q.rear->next=p;Q.rear=p;//注意此步!先连接上,后转移。returnOK;} StatusDeQueue(LinkQueue&Q,QElemType&e){//队头元素出列,并用e返回其值。Qu
7、euePtrp;if(Q.front==Q.rear)returnERROR;p=Q.front->next;e=p->data;Q.front->next=p->next;13888990919293949596979899100101102103104105if(Q.rear==p)//队列中只有一个元素。Q.rear=Q.front;free(p);returnOK;} StatusQueueTraverse(LinkQueueQ,void(*vi)(QElemType)){//从队头
此文档下载收益归作者所有