一、順序隊列
- typedef int QElemType;
- // c3-3.h 隊列的順序存儲結構(可用於循環隊列和非循環隊列)
- #define MAXQSIZE 5 // 最大隊列長度(對於循環隊列,最大隊列長度要減1)
- struct SqQueue
- {
- QElemType *base; // 初始化的動態分配存儲空間
- int front; // 頭指針,若隊列不空,指向隊列頭元素
- int rear; // 尾指針,若隊列不空,指向隊列尾元素的下一個位置
- };
- // bo3-4.cpp 順序隊列(非循環,存儲結構由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 DestroyQueue(SqQueue &Q)
- { // 銷毀隊列Q,Q不再存在
- if(Q.base)
- free(Q.base);
- Q.base=NULL;
- Q.front=Q.rear=0;
- return OK;
- }
- Status ClearQueue(SqQueue &Q)
- { // 將Q清為空隊列
- Q.front=Q.rear=0;
- return OK;
- }
- Status QueueEmpty(SqQueue Q)
- { // 若隊列Q為空隊列,則返回TRUE,否則返回FALSE
- if(Q.front==Q.rear) // 隊列空的標志
- return TRUE;
- else
- return FALSE;
- }
- int QueueLength(SqQueue Q)
- { // 返回Q的元素個數,即隊列的長度
- return(Q.rear-Q.front);
- }
- 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>=MAXQSIZE)
- { // 隊列滿,增加1個存儲單元
- Q.base=(QElemType *)realloc(Q.base,(Q.rear+1)*sizeof(QElemType));
- if(!Q.base) // 增加單元失敗
- return ERROR;
- }
- *(Q.base+Q.rear)=e;
- Q.rear++;
- 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;
- 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++;
- }
- printf("\n");
- return OK;
- }
二、鏈隊列
- typedef int QElemType;
- // c3-2.h 單鏈隊列--隊列的鏈式存儲結構
- typedef struct QNode
- {
- QElemType data;
- QNode *next;
- }*QueuePtr;
- struct LinkQueue
- {
- QueuePtr front,rear; // 隊頭、隊尾指針
- };
- // bo3-2.cpp 鏈隊列(存儲結構由c3-2.h定義)的基本操作(9個)
- Status InitQueue(LinkQueue &Q)
- { // 構造一個空隊列Q
- if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
- exit(OVERFLOW);
- Q.front->next=NULL;
- return OK;
- }
- Status DestroyQueue(LinkQueue &Q)
- { // 銷毀隊列Q(無論空否均可)
- while(Q.front)
- {
- Q.rear=Q.front->next;
- free(Q.front);
- Q.front=Q.rear;
- }
- return OK;
- }
- Status ClearQueue(LinkQueue &Q)
- { // 將Q清為空隊列
- QueuePtr p,q;
- Q.rear=Q.front;
- p=Q.front->next;
- Q.front->next=NULL;
- while(p)
- {
- q=p;
- p=p->next;
- free(q);
- }
- return OK;
- }
- Status QueueEmpty(LinkQueue Q)
- { // 若Q為空隊列,則返回TRUE,否則返回FALSE
- if(Q.front==Q.rear)
- return TRUE;
- else
- return FALSE;
- }
- int QueueLength(LinkQueue Q)
- { // 求隊列的長度
- int i=0;
- QueuePtr p;
- p=Q.front;
- while(Q.rear!=p)
- {
- i++;
- p=p->next;
- }
- return i;
- }
- 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;
- }
三、循環隊列
- // c3-3.h 隊列的順序存儲結構(可用於循環隊列和非循環隊列)
- #define MAXQSIZE 5 // 最大隊列長度(對於循環隊列,最大隊列長度要減1)
- struct SqQueue
- {
- QElemType *base; // 初始化的動態分配存儲空間
- int front; // 頭指針,若隊列不空,指向隊列頭元素
- int rear; // 尾指針,若隊列不空,指向隊列尾元素的下一個位置
- };
- // bo3-3.cpp 循環隊列(存儲結構由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 DestroyQueue(SqQueue &Q)
- { // 銷毀隊列Q,Q不再存在
- if(Q.base)
- free(Q.base);
- Q.base=NULL;
- Q.front=Q.rear=0;
- return OK;
- }
- Status ClearQueue(SqQueue &Q)
- { // 將Q清為空隊列
- Q.front=Q.rear=0;
- return OK;
- }
- Status QueueEmpty(SqQueue Q)
- { // 若隊列Q為空隊列,則返回TRUE,否則返回FALSE
- if(Q.front==Q.rear) // 隊列空的標志
- return TRUE;
- else
- return FALSE;
- }
- int QueueLength(SqQueue Q)
- { // 返回Q的元素個數,即隊列的長度
- return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
- }
- 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;
- }