队列是先进先出的数据结构,出队的一端叫队首,入队的一端叫队尾,就像是平常生活中排队买火车票同样,先买完的人先出队,也就是咱们常说的先进先出。数据结构
头文件: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; }