user-0 提问: 队列中入队入不进去

回复:
user: user-1
content: #include＜stdio.h＞ #include＜stdlib.h＞ typedef struct Qnode { 	int time; 	 int seq;  	char *payload; 	struct Qnode *next; }Qnode,*QueuePtr; typedef struct { 	QueuePtr front;     QueuePtr rear; }LinkQueue; void initQueue(LinkQueue &Q); void Enqueue(LinkQueue &Q,int t,int s); void Queuetraverse(LinkQueue &Q); void Dequeue(LinkQueue &Q); void initQueue(LinkQueue &Q) { 	Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode)); 		if(!Q.front) 		exit(0); } void Enqueue(LinkQueue &Q,int t,int s) { 	QueuePtr q; 	q=(QueuePtr)malloc(sizeof(Qnode)); 	  if (!q)  		  exit(0);  	q-＞time=t;  	q-＞seq=s;   			q-＞payload=(char *)malloc(sizeof (char ));      *q-＞payload=0; 	 q-＞next=0; 	 Q.rear-＞next=q;  	 Q.rear=q; 	 //free(d.payload); } void Queuetraverse(LinkQueue &Q) { 	QueuePtr a; //	a=(QueuePtr)malloc(sizeof (Qnode)); 	a=Q.front-＞next; 	while (a) 	{ 		printf("Current RIP Packet with Timestamp:"); 		 printf("%d ",a-＞time);  		 printf("%d ",a-＞seq); 		 printf("%c\n",a-＞payload);  		 a=a-＞next; 	 }	 } void Dequeue(LinkQueue &Q) { 	QueuePtr b,t; 	 b=Q.front-＞next; 	 while (b) 	 {   	    printf("%d ",b-＞time); 	    printf("%d\n",b-＞seq); 	    t=b; 	 	b=b-＞next;   	 	free(t); 	 } } int main() { 	int num; 	int seq; 	int time,i,c; 	LinkQueue a; 	printf("请输入包数："); 	scanf("%d",&num); 	printf("请输入序号："); 	scanf("%d",&seq); 	printf("请输入时间："); 	scanf("%d",&time); 	//完成输入 	initQueue(a); 	printf("******入队*****\n"); 	for (i=0;i＜num;i++) 	{ 		int t; 	    int s;      	    t=time+i*3600; 	    s=seq+i;  	    Enqueue(a,t,s); 	    printf("Current RIP Packet with Timestamp:%d\t",t); 	    printf("CSeq:%d\t",s); 	    printf("Payload:%c\n",'0');      } 	 //完成入队 	 c=a.front-＞next-＞seq;  	 printf("神经病"); 	 printf("%d",c); 	 Queuetraverse(a);  //完成出队 	 Dequeue(a);    //释放队列 	 return 1; }

user: user-1
content: 我怎么还没看懂。。。。。。。。。。。。。

user: user-0
content: paschen，您好，您说得其他两个地方我分别为它们分配了内存空间即你所说的初始化，我还有一点没明白 “while(!a) { printf("Current RIP Packet with Timestamp:");printf("%d",a->time); printf("%d",a->seq);printf("%c\n",a->payload); a=a->next; }  此时a为NULL才进入循环，而循环中a->time将对NULL指针进行访问” 这个我改为a=Q.front->next;然而程序依旧显示没有入队，这是怎么回事呢

user: user-2
content: 初始化，入队，清队写得都有问题，我根据你的意思给改了下，你看看，数据成员上你自己再完善下： // csdn.cpp : Defines the entry point for the console application. //  #include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<crtdbg.h>  typedef struct Qnode{     int time;     int seq;     char payload;     struct Qnode *next; }Qnode,*QueuePtr;  typedef struct{     QueuePtr front;     QueuePtr rear; }LinkQueue;  void initQueue(LinkQueue &Q); void Enqueue(LinkQueue &Q,int t,int s); void Queuetraverse(LinkQueue &Q); void Dequeue(LinkQueue &Q);  void initQueue(LinkQueue &Q) {     Q.front=Q.rear=NULL; }  void Enqueue(LinkQueue &Q,int t,int s) {     QueuePtr q;      q=(QueuePtr)malloc(sizeof(Qnode));     if(!q) exit(0);      q->time=t;      q->seq=s;     q->payload= 'R';     q->next=0;      if(NULL == Q.front)     {         Q.front = q;     }     else     {         Q.rear->next=q;     }     Q.rear = q;  }  void Queuetraverse(LinkQueue &Q) {     QueuePtr a = Q.front;     while(a)     {         printf("Current RIP Packet with Timestamp:");         printf("%d,",a->time);         printf("seq=%d,",a->seq);         printf("payload=%c\n",a->payload);         a=a->next;     }  }  void Dequeue(LinkQueue &Q) {     QueuePtr b;     b=Q.front;     while(!b)     {         free(b);         b=b->next;     }   }  int _tmain(int argc, _TCHAR* argv[]) {     int num;     int seq;     int time;      printf("请输入包数：");scanf("%d",&num);     printf("请输入序号：");scanf("%d",&seq);     printf("请输入时间：");scanf("%d",&time);     //完成输入    LinkQueue a;     initQueue(a);     printf("******入队*****\n");     for(int i=0;i<num;i++)     {            int t;         int s;         t=time+i*3600;s=seq+i;         Enqueue(a,t,s);         printf("Current RIP Packet with Timestamp:%d\t",t);         printf("CSeq:%d\t",s);         printf("Payload:%c\n",'0');     }     //完成入队   int c;     c=a.front->next->seq;      printf("%d",c);      Queuetraverse(a);      //完成出队   Dequeue(a);//释放队列   _CrtDumpMemoryLeaks();     getchar();     return 0; }

user: user-3
content: 引用 4 楼 IT_Blossom 的回复:paschen，您好，您说得其他两个地方我分别为它们分配了内存空间即你所说的初始化，我还有一点没明白 “while(!a) { printf("Current RIP Packet with Timestamp:");printf("%d",a->time); printf("%d",a->seq);printf("%c\n",a->payload); a=a->next; }  此时a为NULL才进入循环，而循环中a->time将对NULL指针进行访问” 这个我改为a=Q.front->next;然而程序依旧显示没有入队，这是怎么回事呢  while(!a) 表示只有a为NULL才进入循环，楼主意思是否应该是要写成while(a)

user: user-4
content: 完全不明白为什么要这样用。 while(!a){...........} a是空指针，直接->解引用会发生异常 还有下面的while(!b){ b=b->next; .............}

user: user-3
content: Dequeue函数中的b 没有初始就使用了

user: user-3
content: while(!a) 	{ 		printf("Current RIP Packet with Timestamp:");printf("%d",a->time); 		printf("%d",a->seq);printf("%c\n",a->payload); 		a=a->next; 	}  此时a为NULL才进入循环，而循环中a->time将对NULL指针进行访问

user: user-3
content: Enqueue中的q没有初始化就使用了