鏈佇列、迴圈佇列的基本操作
阿新 • • 發佈:2018-12-18
一.鏈佇列
#include<iostream> #include<cstdlib> #include<stdio.h> #include<iomanip> #define OK 1 #define OVERFLOW 0 #define ERROR 0 typedef int QElemType; using namespace std; typedef struct QNode{ QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct{ QueuePtr front;//隊頭指標 QueuePtr rear;//隊尾指標 }LinkQueue; //1.初始化鏈佇列 int InitQueue(LinkQueue *Q) { Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); if(!Q->front) exit(OVERFLOW);//儲存分配失敗 Q->front->next=NULL; return OK; } //2.清空佇列 int ClearQueue(LinkQueue Q) { Q.front=Q.rear; return 1; } //3.銷燬佇列 int DestoryQueue(LinkQueue *Q) { while (Q->front) { Q->rear = Q->front->next; free(Q->front); Q->front = Q->rear; } return OK; } //4.入隊操作 int EnQueue(LinkQueue *Q,QElemType e) { QNode *p; p=(QueuePtr)malloc(sizeof(QNode)); if (!p) exit(OVERFLOW); // 儲存分配失敗 p->data=e; p->next=NULL; Q->rear->next=p; // 修改尾結點的指標 Q->rear=p; // 移動隊尾指標 return OK; } //5.出隊操作 int DeQueue(LinkQueue *Q,QElemType &e) {// 若佇列不空,則刪除Q的隊頭元素,用e返回其值, 並返回OK;否則返回ERROR if(Q->front==Q->rear) return ERROR; QNode *p; p=Q->front->next; e=p->data; Q->front->next=p->next; if(Q->rear==p) Q->rear=Q->front; free(p); return OK; } //6.判斷鏈佇列是否為空 int QueueEmpty(LinkQueue *Q) { return(Q->front==Q->rear); } //7.求鏈佇列的隊頭元素 int GetHead(LinkQueue *Q, QElemType &e) { if(Q->front==Q->rear) return ERROR; e=Q->front->next->data; return OK; } //8.顯示佇列中的元素 void Print(LinkQueue *Q) { QNode *p; p=Q->front->next; while(p) { cout<<p->data<<" "; p=p->next; } } //9.求佇列長度 int QueueLength(LinkQueue Q) { int i=0; while(Q.front->next) { Q.front=Q.front->next; i++; } return i; } void show_help() { cout<<"1----初始化空佇列"<<endl; cout<<"2----清空佇列"<<endl; cout<<"3----銷燬佇列"<<endl; cout<<"4----入隊"<<endl; cout<<"5----出隊"<<endl; cout<<"6----判空"<<endl; cout<<"7----取出佇列對頭元素"<<endl; cout<<"8----顯示佇列的元素"<<endl; cout<<"9----求佇列長度"<<endl; cout<<" 退出,輸出一個負數!"<<endl; } int main() { int operate_code; show_help(); LinkQueue Q; QElemType e; InitQueue(&Q); while(1) { cout<<"請輸入操作程式碼:"; cin>>operate_code; if(operate_code==1) { InitQueue(&Q); } else if(operate_code==2) { DestoryQueue(&Q); } else if(operate_code==3) { cout<<"請輸入要入隊的元素:"; int e; cin>>e; EnQueue(&Q,e); } else if(operate_code==4) { DeQueue(&Q,e); } else if(operate_code==5) { if(QueueEmpty(&Q)) cout<<"The Queue is empty."<<endl; else cout<<"The Queue is not empty"<<endl; } else if(operate_code==6) { GetHead(&Q,e); cout<<e<<endl; } else if(operate_code==7) { Print(&Q); } else if(operate_code<0) { break; } else { cout<<"\n操作碼錯誤!!!"<<endl; show_help(); } } return 0; }
二.迴圈佇列
#include<iostream> #include<cstdlib> #include<stdio.h> #include<iomanip> #define OK 1 #define OVERFLOW 0 #define ERROR 0 #define MAXQSIZE 100 //最大佇列長度 using namespace std; typedef struct { int *base; //初始化的動態分配儲存空間 int front; //頭指標 int rear; //尾指標 }SqQueue; //1.初始化迴圈佇列 int InitQueue(SqQueue &Q) { Q.base=(int *)malloc(MAXQSIZE*sizeof(int)); if(!Q.base) exit(OVERFLOW); Q.front=Q.rear=0; return OK; } //2.求迴圈佇列的長度 int QueueLength(SqQueue Q) { return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; } //3.迴圈佇列入隊 int EnQueue(SqQueue &Q,int e) { if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR; Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXQSIZE; return OK; } //4.迴圈隊列出隊 int DeQueue (SqQueue &Q,int &e) { if(Q.front==Q.rear) return ERROR; e=Q.base[Q.front]; Q.front=(Q.front+1) % MAXQSIZE; return OK; } //6.取出迴圈佇列頭結點 int GetHead(SqQueue &Q) { if(Q.front!=Q.rear) return Q.base[Q.front]; } //7.刪除佇列頭結點 void Deququ(SqQueue &Q,int &e) { if(Q.front==Q.rear) { cout<<"為空"<<endl; return ; } e=Q.base[Q.front]; Q.front=(Q.front+1)%MAXQSIZE; cout<<"刪除的值為:"; cout<<e<<endl; } //8.輸出迴圈佇列的各個結點值 void visit(SqQueue Q) { int i; i=Q.front; while(i!=Q.rear) { cout<<Q.base[i]; i=(i+1)%MAXQSIZE; } } //9.銷燬佇列 int DestoryQueue(SqQueue &Q) { if(Q.base) delete[]Q.base; Q.front=Q.rear=0; } void show_help() { cout<<"1----初始化迴圈佇列"<<endl; cout<<"2----迴圈佇列的長度 "<<endl; cout<<"3----迴圈佇列入隊"<<endl; cout<<"4----迴圈隊列出隊 "<<endl; cout<<"5----判空"<<endl; cout<<"6-----取出迴圈佇列頭結點"<<endl; cout<<"7-----刪除佇列頭結點"<<endl; cout<<"8-----出迴圈佇列的各個結點值"<<endl; cout<<"9-----銷燬佇列"<<endl; cout<<" 退出,輸出一個負數!"<<endl; } int main() { int operate_code; show_help(); SqQueue head; InitQueue(head); while(1) { cout<<"請輸入操作程式碼:"; cin>>operate_code; if(operate_code==1) { InitQueue(head); } else if(operate_code==2) { cout<<"該迴圈佇列的長度為:"<<QueueLength(head)<<endl; } else if(operate_code==3) { int e; cout<<"請輸入要入隊的元素:"; cin>>e; EnQueue(head,e); } else if(operate_code==4) { int e; DeQueue(head,e); cout<<"出隊的元素為:"<<e<<endl; } else if(operate_code==5) { if(QueueLength(head)) cout<<"The Queue is not empty."<<endl; else cout<<"The Queue is empty."<<endl; } else if(operate_code==6) { cout<<GetHead(head)<<endl; } else if(operate_code==7) { int e; DeQueue(head,e); } else if(operate_code==8) { visit(head); cout<<endl; } else if(operate_code==9) { DestoryQueue(head); } else if(operate_code<0) { break; } else { cout<<"\n操作碼錯誤!!!"<<endl; show_help(); } } }