队列--链式存储
带头结点的队列。
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; typedef int QElemType; typedef struct QNode{ QElemType data; struct QNode * next; }QNode; typedef QNode * QueuePtr; typedef struct{ int length;//记录队列长度 QueuePtr front;//front为头指针,front->next为队首元素 QueuePtr rear;//rear为队尾指针,指向下一次入队元素的位置 }LinkQueue; Status visit(QElemType c){ printf("%d ",c); return OK; } Status InitQueue(LinkQueue *Q){ Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); if( ! Q->front){ return ERROR; } Q->front->next = NULL; Q->length = 0; return OK; } //销毁队列:将头结点也free掉 Status DestroyQueue(LinkQueue *Q){ QueuePtr p; while(Q->front){ p = Q->front->next; free(Q->front); Q->front = p; } Q->length = 0; return OK; } //清空队列:保留头结点 Status ClearQueue(LinkQueue *Q){ QueuePtr p,q; p = Q->front->next;//p指向第一个元素 Q->front->next = NULL;//将头节点的next指针指向空。 while(p){ q = p; p = p->next; free(q); } Q->length = 0; return OK; } Status QueueEmpty(LinkQueue Q){ if(Q.front == Q.rear || Q.length==0){ return TRUE; } return FALSE; } int QueueLength(LinkQueue Q){ return Q.length; } Status GetHead(LinkQueue Q,QElemType *e){ if(Q.front == Q.rear){ return ERROR; } *e = (Q.front)->next->data; return OK; } Status EnQueue(LinkQueue *Q,QElemType e){ QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if( ! p){ return ERROR; } p->data = e; p->next = NULL; Q->rear->next = p; Q->rear = p;//队尾指针指向新插入的元素 Q->length++; return OK; } Status DeQueue(LinkQueue *Q,QElemType *e){ QueuePtr p; if(Q->front == Q->rear){ return ERROR;//空队 } p = Q->front->next;//指向队首元素 *e = p->data;// Q->front->next = p->next; if(p == Q->rear){ Q->rear = Q->front; } Q->length--; free(p); return OK; } Status QueueTraverse(LinkQueue Q){ QueuePtr p = (Q.front)->next;//指向对首元素,空队时,队首元素为NULL while(p){ visit(p->data); p = p->next; } printf("\n"); return OK; } int main(){ Status i; QElemType e; LinkQueue Q; int j; i = InitQueue(&Q); if(i){ printf("成功初始化队列\n"); } else { printf("初始化队列失败\n"); return 1; } printf("队列是否为空 %d\t 队列长度是 %d\n",QueueEmpty(Q),QueueLength(Q)); i = EnQueue(&Q,99); if(i){ printf("成功入队99\n"); } else { printf("入队99失败\n"); } printf("遍历队列\t"); QueueTraverse(Q); for(j = 1; j < 5; j++){ i = EnQueue(&Q,j); if(i){ printf("成功入队%d\n",j); } else { printf("入队%d失败\n",j); } } printf("队列是否为空 %d\t 队列长度是 %d\n",QueueEmpty(Q),QueueLength(Q)); printf("遍历队列\t"); QueueTraverse(Q); GetHead(Q,&e); printf("队首元素是 %d\n",e); for(j = 0; j < 7; j++){ i = DeQueue(&Q,&e); if(i){ printf("出队成功,出队元素是 %d\n",e); printf("遍历队列\t"); QueueTraverse(Q); printf("队列是否为空 %d\t 队列长度是 %d\n\n",QueueEmpty(Q),QueueLength(Q)); } else { printf("出队失败\n\n"); } } for(j = 0; j < 10; j++){ i = EnQueue(&Q,j); if(i){ printf("成功入队%d\n",j); } else { printf("入队%d失败\n",j); } } printf("遍历队列\t"); QueueTraverse(Q); printf("队列是否为空 %d\t 队列长度是 %d\n\n",QueueEmpty(Q),QueueLength(Q)); GetHead(Q,&e); printf("队首元素是 %d\n\n",e); printf("清空队列后\n"); ClearQueue(&Q); printf("遍历队列\t"); QueueTraverse(Q); printf("队列是否为空 %d\t 队列长度是 %d\n\n",QueueEmpty(Q),QueueLength(Q)); EnQueue(&Q,99); DestroyQueue(&Q); printf("队列是否为空 %d\t 队列长度是 %d\n\n",QueueEmpty(Q),QueueLength(Q)); }
如需转载,请注明文章出处,谢谢!!!