轉載:http://blog.chinaunix.net/uid-26548237-id-3473528.html
與棧相反,隊列是一種先進先出的線性表,它只允許在表的一端進行,而在另一端刪除元
- //單鏈隊列,隊列的鏈式存儲結構
- typedef struct QNode
- {
- ElemType data;
- struct QNode *next;
- }QNode,
*QueuePtr;
- typedef struct
- {
- QueuePtr front; //隊頭指針
- QueuePtr rear; //隊尾指針
- }LinkQueue;
- #include <stdio.h>
- #include <stdlib.h>
- #define ElemType int
- #define OK 1
- #define ERROR 0
- //單鏈隊列,隊列的鏈式存儲結構
- typedef struct QNode
- {
- ElemType data;
- struct QNode *next;
- }QNode,
*QueuePtr;
- typedef struct
- {
- QueuePtr front; //隊頭指針
- QueuePtr rear; //隊尾指針
- }LinkQueue;
- //構造一個空隊列
- int InitQueue(LinkQueue
*q)
- {
- //
- q->front
= q->rear
= (QueuePtr)malloc(sizeof(QNode));
- if(q->front
==
NULL)
- {
- fprintf(stderr,
"malloc() error.\n");
- return ERROR;
- }
- q->front->next
= NULL;
- q->rear->next
= NULL;
- return OK;
- }
- //銷燬隊列,Q不在存在
- int DestroyQueue(LinkQueue
*q)
- {
- //
- while(q->front)
- {
- q->rear
= q->front->next;
- free(q->front);
- q->front
= q->rear;
- }
- return OK;
- }
- //判斷隊列是否爲空
- int IsEmptyQueue(LinkQueue
*q)
- {
- return (q->front->next
==
NULL && q->rear->next
==
NULL);
- }
- //插入元素e爲新的隊尾元素
- int InsertQueue(LinkQueue
*q, ElemType e)
- {
- //
- QueuePtr p =
(QueuePtr)malloc(sizeof(QNode));
- if(p
==
NULL)
- {
- fprintf(stderr,
"malloc() error.\n");
- return ERROR;
- }
-
- p->data
= e;
- p->next
= NULL;
-
- //如果隊列爲空
- if(IsEmptyQueue(q))
- {
- q->front->next
= p;
- q->rear
= p;
- }
- else
- {
- q->rear->next
= p;
- q->rear
= p; //不要忘記這句啊
- }
- return OK;
- }
- //若隊列不空,則刪除隊頭元素,用e返回其值,並返回OK
- int DeQueue(LinkQueue
*q, ElemType
*e)
- {
- //
- if(IsEmptyQueue(q))
- {
- fprintf(stdout,
"the queue is null.\n");
- return ERROR;
- }
- //注意有隊頭結點
- QueuePtr temp;
- temp = q->front->next;
- *e
= temp->data;
- q->front->next
= temp->next;
- //一個元素
- if(q->rear
== temp)
- {
- q->rear
= q->front;
- }
- free(temp);
- return OK;
- }
- //打印隊列
- void printQueue(LinkQueue
*q)
- {
- QueuePtr temp;
- temp = q->front->next;
- while(temp
!=
NULL)
- {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- }
- int main(int argc, char
**argv)
- {
- LinkQueue *q;
- InitQueue(q);
- srand(time(NULL));
- int i
= 0;
- for(i
= 0; i
< 10; i++)
- {
- InsertQueue(q, i);
- }
- printf("插入隊列的元素爲:\n");
- printQueue(q);
- int e;
- DeQueue(q,
&e);
- printf("隊頭元素爲: %d\n", e);
- return 0;
- }
- #define ElemType int
- typedef struct
- {
- ElemType data[MAXSIZE];
- int front;
//頭指針
- int rear;
//尾指針,若隊列不爲空時,rear指向隊列尾元素的下一個位置
- }SqQueue;
- #define ElemType int
- #define MAXSIZE 100
- typedef struct
- {
- ElemType data[MAXSIZE];
- int front; //頭指針
- int rear; //尾指針,若隊列不爲空時,rear指向隊列尾元素的下一個位置
- }SqQueue;
- #define OK 1
- #define ERROR 0
- //初始化一個空隊列
- int InitQueue(SqQueue
*q)
- {
- q->front
= 0;
- q->rear
= 0;
- return OK;
- }
- //循環隊列求隊列長度
- //返回隊列的元素的個數,也就是隊列的當前長度
- int QueueLength(SqQueue
*q)
- {
- return (q->rear
- q->front
+ MAXSIZE)
% MAXSIZE;
- }
- //循環隊列的入隊操作
- //若隊列未滿,則插入元素e爲隊列新的隊尾元素
- int EnQueue(SqQueue
*q, ElemType e)
- {
- if((q->rear
+ 1)
% MAXSIZE == q->front) //隊列滿的判斷
- {
- return ERROR;
- }
- q->data[q->rear]
= e; //將元素e賦值給隊尾
- q->rear
= (q->rear
+ 1) MAXSIZE; //rear指向後移一位置,若到最後則轉爲數組頭部
- //注意
- return OK;
- }
- //循環隊列的出對操作
- //若隊列不空,則刪除q中隊頭元素,用e返回值
- int DeQueue(SqQueue
*q, ElemType
*e)
- {
- if(q->rear
== q->front) //隊列空的判斷
- {
- return ERROR;
- }
-
- *e
= q->data[q->front]; //將隊頭元素賦值給e
- q->front
= (q->front
+ 1)
% MAXSIZE;//front指針向後一位置,若到最後,則轉到數組頭部
- return OK;
- }