+ 1
why it ebdless loop
#include<stdio.h> struct node { int date; struct node *next; }; typedef struct node linknode; typedef linknode *linklist; int main() { linklist head,tail,q; printf("input a.numeber(仄ē©ŗę ¼åéļ¼ä»„0ä½äøŗē»ę):\n"); int n; scanf("%d",&n); while(n!=0) { q=(linklist)malloc(sizeof(linknode)); q->date=n; if(head==NULL) head=tail=q; else { tail->next=q; tail=q; } scanf("%d",&n); } if(tail!=NULL) tail->next=NULL; linklist p=head; while(p!=NULL) { printf("%5d",p->date); p->next; } return 0; }
2 Answers
+ 3
while(p!=NULL)
p will never be NULL
===
p = p->next;
instead
p->next;
0
head needs to be set to NULL or nullptr, if you want to enter this decade. The if(head==NULL) statement will compare with whatever junk was left by the previous program so your list will be random memory values.
You also need to fix the p->next; statement as pointed out already.