关于链式结构的队列的一个问题,请大家指导。。
添加if(pFirst->front == pFirst->rear)
{
puts("queue is empty!!");
exit(0);
}出队失败。。,删除后出队成功
程序代码:
#include <stdio.h> #include <stdlib.h> typedef struct NQUEUE { int data; struct NQUEUE *next; }nQueue; typedef struct LQUEUE { nQueue *front; nQueue *rear; }lQueue; void display(lQueue *); void init(lQueue *); void enQueue(lQueue *, int); void deQueue(lQueue *); int main(void) { lQueue *pFirst = (lQueue *)malloc(sizeof(lQueue)); int data = 0; int i = 0; init(pFirst); for( i = 1; i < 8; i++ ) { enQueue(pFirst, i); } display(pFirst); deQueue(pFirst); deQueue(pFirst); puts(""); display(pFirst); return 0; } void display(lQueue *pFirst) { nQueue *tmp = pFirst->front->next; while(tmp) { printf("%3d", tmp->data); tmp = tmp->next; } } void init(lQueue *pFirst) { pFirst->front = pFirst->rear = (nQueue *)malloc(sizeof(nQueue)); pFirst->front->next = NULL; pFirst->front->data = 0; } void enQueue(lQueue *pFirst, int e) { nQueue *tmp = (nQueue *)malloc(sizeof(nQueue)); tmp->data = e; tmp->next = NULL; pFirst->rear->next = tmp; pFirst->rear = tmp; } void deQueue(lQueue *pFirst) { nQueue *tmp = NULL; if(pFirst->front == pFirst->rear)//问题所在。。。 { puts("queue is empty!!"); exit(0); } tmp = pFirst->front->next; pFirst->front->next = tmp->next; if(tmp == pFirst->rear); { pFirst->rear = pFirst->front; } free(tmp); tmp = NULL; }