#include<iostream>#include<malloc.h>#include<stdlib.h>usingnamespace std;typedefint ElemType;typedefbool Status;typedefstruct Node
{
ElemType data;
Node *next;}node,*pNode;typedefstruct{
pNode front;
pNode rear;}queue,*pQueue;
Status Init(pQueue);//初始化队列
Status Isempty(pQueue);//判断队列是否为空
Status Inqueue(pQueue,ElemType);//入队
Status Dequeue(pQueue,ElemType*);//出队并将出队的值带回voidTraverse(pQueue);intQueuelenth(pQueue);
Status Gethead(pQueue,ElemType*);
Status Clearqueue(pQueue);
Status Destoryqueue(pQueue);intmain(){
queue linkqueue;int lenth;if(Init(&linkqueue))cout<<"The queue is initiate successful."<<'\n'<<endl;else{
cout<<"Initiating Error!!!"<<endl;exit(-1);}//判断队列是否为空if(Isempty(&linkqueue))cout<<"The queue is empty."<<endl;else cout<<"The queue is not empty."<<endl;//1、2、3、4分别入队if(!Inqueue(&linkqueue,1)){cout<<"Inqueue error!!!"<<endl;exit(-1);};if(!Inqueue(&linkqueue,2)){cout<<"Inqueue error!!!"<<endl;exit(-1);};if(!Inqueue(&linkqueue,3)){cout<<"Inqueue error!!!"<<endl;exit(-1);};if(!Inqueue(&linkqueue,4)){cout<<"Inqueue error!!!"<<endl;exit(-1);};Traverse(&linkqueue);
lenth=Queuelenth(&linkqueue);
cout<<"The lenth of queue is "<<lenth<<"."<<'\n'<<endl;//1、2、3 分别出队
ElemType val;if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;if(Dequeue(&linkqueue,&val))cout<<"The number "<<val<<" has dequeue."<<endl;Traverse(&linkqueue);
lenth=Queuelenth(&linkqueue);
cout<<"The lenth of queue is "<<lenth<<"."<<'\n'<<endl;//取队头if(Gethead(&linkqueue,&val))cout<<"The front of the queue is "<<val<<"."<<'\n'<<endl;else cout<<"The front of the queue maybe not exist or empty."<<'\n'<<endl;//清理队列if(Clearqueue(&linkqueue))cout<<"The queue is clearing."<<'\n'<<endl;else cout<<"The queue maybe not exist."<<'/n'<<endl;//销毁队列if(Destoryqueue(&linkqueue))cout<<"The queue is destorying"<<'\n'<<endl;else cout<<"The queue maybe not exist or empty."<<'\n'<<endl;}
Status Init(pQueue linkqueue){
linkqueue->front=linkqueue->rear=(pNode)malloc(sizeof(Node));if(linkqueue->rear==0){returnfalse;}
linkqueue->rear->next=NULL;returntrue;}
Status Isempty(pQueue linkqueue){if(linkqueue->rear==linkqueue->front)returntrue;elsereturnfalse;}
Status Inqueue(pQueue linkqueue,ElemType val){
pNode pNew=(pNode)malloc(sizeof(Node));if(pNew==0)returnfalse;
pNew->data=val;
linkqueue->rear->next=pNew;
linkqueue->rear=pNew;
linkqueue->rear->next=NULL;returntrue;}
Status Dequeue(pQueue linkqueue,ElemType* val){if(!Isempty(linkqueue)){
pNode q=linkqueue->front->next;*val=q->data;
linkqueue->front->next=q->next;free(q);
q=NULL;}else{returnfalse;}returntrue;}voidTraverse(pQueue linkqueue){if(!Isempty(linkqueue)){
pNode p=linkqueue->front->next;while(p!=NULL){
cout<<p->data<<" ";
p=p->next;}
p=NULL;}else{
cout<<"The queue is empty.";}
cout<<endl;}intQueuelenth(pQueue linkqueue){int i=0;
pNode p=linkqueue->front->next;while(p!=0){
i++;
p=p->next;}return i;}
Status Gethead(pQueue linkqueue,ElemType* val){if(!Isempty(linkqueue)){*val=linkqueue->front->next->data;returntrue;}elsereturnfalse;}
Status Clearqueue(pQueue linkqueue){if(!Isempty(linkqueue)){
pNode p=linkqueue->front->next;while(p!=0){
linkqueue->front->next=p->next;free(p);
p=linkqueue->front->next;}
linkqueue->rear=linkqueue->front;returntrue;}else{returnfalse;}}
Status Destoryqueue(pQueue linkqueue){if(linkqueue->front!=NULL){
pNode p=linkqueue->front->next;while(p!=0){
linkqueue->front->next=p->next;free(p);
p=linkqueue->front->next;}free(linkqueue->front);
linkqueue->rear=linkqueue->front;returntrue;}else{returnfalse;}}