user-0 提问: 大佬 帮忙看下哪里出错了 链式队列。

回复:
user: user-1
content: 小心驶得万年船。

user: user-2
content: dequeue的问题吧，PNODE p = q->front->next;这个p在show之后应该是NULL了吧。

user: user-3
content: show()函数里修改了front，后面出队还要用到front，但是这个front已经不是队头了。

user: user-3
content: 引用 2 楼 heheda7918 的回复:[quote=引用 1 楼 cfjtaishan 的回复:] //#include "stdafx.h" #include<stdio.h> //#include<malloc.h> #include<stdlib.h>  typedef struct node {     int data;     struct node * next; }NODE, *PNODE;  typedef struct {     PNODE rear, front; }queue;  void init(queue*); void insert(queue*, int val); void show(queue*); void dequeue(queue*, int *);  int main(void) {     int val;     queue  q;     init(&q);     insert(&q, 1);     insert(&q, 12);     insert(&q, 13);     insert(&q, 14);     show(&q);     dequeue(&q, &val);     printf("%d\n", val);      return 0; }  void init(queue* q) {     q->front = q->rear = (PNODE)malloc(sizeof(NODE));     if (q->front == NULL)         exit(-1);     q->rear->next = NULL;     return; }  void insert(queue* q, int val) {     PNODE p = (PNODE)malloc(sizeof(NODE));     if (p == NULL)         exit(-1);     p->data = val;     p->next = NULL;     q->rear->next = p;     q->rear = p; }  void show(queue* q) {     PNODE p = q->front;     if (!p) {         fprintf(stdout, "queue is empty!\n");         return;     }     p = q->front->next;     while (p)     {         printf("%d ", p->data);         p = p->next;     }     printf("\n");  }  void dequeue(queue * q, int *val) {     PNODE p = q->front->next;     *val = p->data;     q->front->next = p->next;     free(p);     return; }  show函数逻辑有问题； 详见上面的代码  能告诉我我写的show为什么不对么。[/quote] 注意看一下你的show函数里，你修改了front指针，front指针已经不指向队列的头了。 show()函数不建议修改队列指针，只对队列读即可。

user: user-0
content: 引用 1 楼 cfjtaishan 的回复://#include "stdafx.h" #include<stdio.h> //#include<malloc.h> #include<stdlib.h>  typedef struct node {     int data;     struct node * next; }NODE, *PNODE;  typedef struct {     PNODE rear, front; }queue;  void init(queue*); void insert(queue*, int val); void show(queue*); void dequeue(queue*, int *);  int main(void) {     int val;     queue  q;     init(&q);     insert(&q, 1);     insert(&q, 12);     insert(&q, 13);     insert(&q, 14);     show(&q);     dequeue(&q, &val);     printf("%d\n", val);      return 0; }  void init(queue* q) {     q->front = q->rear = (PNODE)malloc(sizeof(NODE));     if (q->front == NULL)         exit(-1);     q->rear->next = NULL;     return; }  void insert(queue* q, int val) {     PNODE p = (PNODE)malloc(sizeof(NODE));     if (p == NULL)         exit(-1);     p->data = val;     p->next = NULL;     q->rear->next = p;     q->rear = p; }  void show(queue* q) {     PNODE p = q->front;     if (!p) {         fprintf(stdout, "queue is empty!\n");         return;     }     p = q->front->next;     while (p)     {         printf("%d ", p->data);         p = p->next;     }     printf("\n");  }  void dequeue(queue * q, int *val) {     PNODE p = q->front->next;     *val = p->data;     q->front->next = p->next;     free(p);     return; }  show函数逻辑有问题； 详见上面的代码  能告诉我我写的show为什么不对么。

user: user-3
content: //#include "stdafx.h" #include<stdio.h> //#include<malloc.h> #include<stdlib.h>  typedef struct node {     int data;     struct node * next; }NODE, *PNODE;  typedef struct {     PNODE rear, front; }queue;  void init(queue*); void insert(queue*, int val); void show(queue*); void dequeue(queue*, int *);  int main(void) {     int val;     queue  q;     init(&q);     insert(&q, 1);     insert(&q, 12);     insert(&q, 13);     insert(&q, 14);     show(&q);     dequeue(&q, &val);     printf("%d\n", val);      return 0; }  void init(queue* q) {     q->front = q->rear = (PNODE)malloc(sizeof(NODE));     if (q->front == NULL)         exit(-1);     q->rear->next = NULL;     return; }  void insert(queue* q, int val) {     PNODE p = (PNODE)malloc(sizeof(NODE));     if (p == NULL)         exit(-1);     p->data = val;     p->next = NULL;     q->rear->next = p;     q->rear = p; }  void show(queue* q) {     PNODE p = q->front;     if (!p) {         fprintf(stdout, "queue is empty!\n");         return;     }     p = q->front->next;     while (p)     {         printf("%d ", p->data);         p = p->next;     }     printf("\n");  }  void dequeue(queue * q, int *val) {     PNODE p = q->front->next;     *val = p->data;     q->front->next = p->next;     free(p);     return; }  show函数逻辑有问题； 详见上面的代码