隊列是先進先出的數據結構,出隊的一端叫隊首,入隊的一端叫隊尾,就像是平常生活中排隊買火車票同樣,先買完的人先出隊,也就是咱們常說的先進先出。數據結構
頭文件:ide
/***************************************************************************************************** *Copyright:Yue Workstation * *FileName:Queue.h * *Function:隊列相關數據定義和函數聲明 * *Author:Abel Lee * *CreateOn:2011-7-21 * *Log:2011-7-21 由Lijian建立 *****************************************************************************************************/ #ifndef QUEUE_H #define QUEUE_H #include "global.h" typedef int QElemType; typedef struct QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; int InitQueue(LinkQueue *Q); int DestroyQueue(LinkQueue *Q); int QueueEmpty(LinkQueue Q); int QueueLength(LinkQueue Q); int GetHead(LinkQueue Q,QElemType *e); int InsertQueue(LinkQueue *Q,QElemType e); int DelQueue(LinkQueue *Q,QElemType *e); int PrintQueue(LinkQueue Q); #endif
源文件:函數
/***************************************************************************************************** *Copyright:Yue Workstation * *FileName:Queue.c * *Function:隊列基本操做 * *Author:Abel Lee * *CreateOn:2011-7-21 * *Log:2011-5-3 由Abel Lee建立 *****************************************************************************************************/ #include "../inc/Queue.h" /**************************************************************************************************** *Function Name:InitQueue * *Function:初始化一個隊列 * *Parameter: Q:隊列的首部 * *Return Value:成功返回0,失敗返回-1 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int InitQueue(LinkQueue *Q) { Q->front = Q->rear = (QueuePtr )malloc(sizeof(QNode)); if(!Q->front) { perror("malloc error\n"); return -1; } Q->front->next = NULL; Q->front->data = 0; return 0; } /**************************************************************************************************** *Function Name:DestroyQueue * *Function:銷燬一個隊列 * *Parameter: Q:隊列的首部 * *Return Value:成功返回0 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int DestroyQueue(LinkQueue *Q) { while(Q->front) { Q->rear = Q->front->next; free(Q->front); Q->front = Q->rear; } Q = NULL; return 0; } /**************************************************************************************************** *Function Name:QueueEmpty * *Function:隊列是否爲空 * *Parameter: Q:隊列的首部 * *Return Value:隊列爲空返回1,非空返回0 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int QueueEmpty(LinkQueue Q) { if(Q.front == Q.rear) { return 1; } else { return 0; } } /**************************************************************************************************** *Function Name:QueueLength * *Function:隊列長度 * *Parameter: Q:隊列的首部 * *Return Value:隊列的長度 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int QueueLength(LinkQueue Q) { return Q.front->data; } /**************************************************************************************************** *Function Name:GetHead * *Function:獲取隊列頭元素 * *Parameter: Q:隊列的首部 * e:保存隊首元素 * *Return Value:成功返回0,失敗返回-1 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int GetHead(LinkQueue Q,QElemType *e) { if(Q.front->next == NULL) { perror("Queue is empty!\n"); *e = -1; return -1; } *e = Q.front->next->data; return 0; } /**************************************************************************************************** *Function Name:InsertQueue * *Function:入隊操做,入隊元素爲e * *Parameter: Q:隊列的首部 * e:入隊元素 * *Return Value:成功返回0,失敗返回-1 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int InsertQueue(LinkQueue *Q,QElemType e) { QueuePtr p = (QueuePtr )malloc(sizeof(QNode)); if(p == NULL) { perror("malloc error!\n"); return -1; } p->data = e; p->next = NULL; (Q->rear)->next = p; Q->rear = p; Q->front->data++; return 0; } /**************************************************************************************************** *Function Name:DelQueue * *Function:出隊操做,出隊的元素保存在e中 * *Parameter: Q:隊列的首部 * e:出隊元素 * *Return Value:成功返回0,失敗返回-1 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int DelQueue(LinkQueue *Q,QElemType *e) { if(Q->front == Q->rear) { perror("The queue is empty!"); return -1; } QueuePtr p = (QueuePtr )malloc(sizeof(QNode)); p = Q->front->next; *e = p->data; Q->front->next = p->next; if(Q->rear == p) { Q->rear = Q->front; } free(p); Q->front->data--; return 0; } /**************************************************************************************************** *Function Name:PrintQueue * *Function:打印隊列中的元素 * *Parameter: Q:隊列的首部 * *Return Value:成功返回0 * *Author:Abel Lee * *Log:2011-7-21 ***************************************************************************************************/ int PrintQueue(LinkQueue Q) { Q.front = Q.front->next; while(Q.front != NULL) { printf("%d-----",Q.front->data); Q.front = Q.front->next; } return 0; }