建立一個隊,1,3,5,7,9入隊,再出隊列。
鏈隊列的方法和迴圈隊列兩種方法。
首先是鏈隊列:
#include<string> #include<iostream> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 using namespace std; typedef int Status; /* Status是函數的類型,其值是函數結果狀態碼,如OK等 */ typedef int QElemType; typedef struct QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front,rear; /* 隊頭、隊尾指標 */ }LinkQueue; Status InitQueue(LinkQueue &Q) { // 構造一個空隊列Q if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)))) exit(OVERFLOW); Q.front->next=NULL; return OK; } Status QueueEmpty(LinkQueue Q) { // 若Q為空白隊列,則返回TRUE,否則返回FALSE if(Q.front==Q.rear) return TRUE; else return FALSE; } Status GetHead(LinkQueue Q,QElemType &e) { // 若隊列不空,則用e返回Q的隊頭元素,並返回OK,否則返回ERROR QueuePtr p; if(Q.front==Q.rear) return ERROR; p=Q.front->next; e=p->data; return OK; } Status EnQueue(LinkQueue &Q,QElemType e) { // 插入元素e為Q的新的隊尾元素 QueuePtr p; if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 儲存分配失敗 exit(OVERFLOW); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; return OK; } Status DeQueue(LinkQueue &Q,QElemType &e) { // 若隊列不空,刪除Q的隊頭元素,用e返回其值,並返回OK,否則返回ERROR QueuePtr p; if(Q.front==Q.rear) return ERROR; p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; free(p); return OK; } Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType)) { // 從隊頭到隊尾依次對隊列Q中每個元素調用函數vi()。一旦vi失敗,則操作失敗 QueuePtr p; p=Q.front->next; while(p) { vi(p->data); p=p->next; } printf("\n"); return OK; } void visit(QElemType i) { cout<<i<<" "; } int main() { int i; QElemType d; LinkQueue q; i=InitQueue(q); EnQueue(q,1); EnQueue(q,3); EnQueue(q,5); EnQueue(q,7); EnQueue(q,9); cout<<"已經插入5個元素(1,3,5,7,9)\n"; cout<<"隊列的元素依次為:"; QueueTraverse(q,visit); cout<<endl; i=GetHead(q,d); if(i==OK) cout<<"隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了最後一個元素"<<d<<endl; if(QueueEmpty(q)) cout<<"現在隊列是一個空隊列"; else cout<<"隊列非空"; cout<<endl; }
迴圈鏈表的程式:
#include<iostream> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 using namespace std; typedef int Status; /* Status是函數的類型,其值是函數結果狀態碼,如OK等 */ typedef int QElemType; #define MAXQSIZE 5 // 最大隊列長度(對於迴圈隊列,最大隊列長度要減1) typedef struct { QElemType *base; // 初始化的動態分配儲存空間 int front; // 頭指標,若隊列不空,指向隊列頭元素 int rear; // 尾指標,若隊列不空,指向隊列尾元素的下一個位置 }SqQueue; /* bo3-3.c 迴圈隊列(儲存結構由c3-3.h定義)的基本操作(9個) */ Status InitQueue(SqQueue &Q) { /* 構造一個空隊列Q */ (Q).base=(QElemType *)malloc(MAXQSIZE*sizeof(QElemType)); if(!(Q).base) /* 儲存分配失敗 */ exit(OVERFLOW); (Q).front=(Q).rear=0; return OK; } Status QueueEmpty(SqQueue Q) { /* 若隊列Q為空白隊列,則返回TRUE,否則返回FALSE */ if(Q.front==Q.rear) /* 隊列空的標誌 */ return TRUE; else return FALSE; } Status GetHead(SqQueue Q,QElemType &e) { /* 若隊列不空,則用e返回Q的隊頭元素,並返回OK,否則返回ERROR */ if(Q.front==Q.rear) /* 隊列空 */ return ERROR; e=*(Q.base+Q.front); return OK; } Status EnQueue(SqQueue &Q,QElemType e) { /* 插入元素e為Q的新的隊尾元素 */ if(((Q).rear+1)%MAXQSIZE==(Q).front) /* 隊列滿 */ return ERROR; (Q).base[(Q).rear]=e; (Q).rear=((Q).rear+1)%MAXQSIZE; return OK; } Status DeQueue(SqQueue &Q,QElemType &e) { /* 若隊列不空,則刪除Q的隊頭元素,用e返回其值,並返回OK;否則返回ERROR */ if((Q).front==(Q).rear) /* 隊列空 */ return ERROR; e=(Q).base[(Q).front]; (Q).front=((Q).front+1)%MAXQSIZE; return OK; } Status QueueTraverse(SqQueue Q,void(*vi)(QElemType)) { /* 從隊頭到隊尾依次對隊列Q中每個元素調用函數vi().一旦vi失敗,則操作失敗 */ int i; i=Q.front; while(i!=Q.rear) { vi(*(Q.base+i)); i=(i+1)%MAXQSIZE; } printf("\n"); return OK; }void visit(QElemType i) { cout<<i<<" "; }int main() { int i; QElemType d; SqQueue q; i=InitQueue(q); EnQueue(q,1); EnQueue(q,3); EnQueue(q,5); EnQueue(q,7); EnQueue(q,9); cout<<"已經插入5個元素(1,3,5,7,9)\n"; cout<<"隊列的元素依次為:"; QueueTraverse(q,visit); cout<<endl; i=GetHead(q,d); if(i==OK) cout<<"隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"刪除了隊頭元素,"<<d; i=GetHead(q,d); if(i==OK) cout<<"\t新的隊頭元素是: "<<d<<endl; DeQueue(q,d); cout<<"\t刪除了最後一個元素"<<d<<endl; if(QueueEmpty(q)) cout<<"現在隊列是一個空隊列"; else cout<<"隊列非空"; cout<<endl; return 0; }