+ 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; }

16th Nov 2017, 10:37 PM
juary
2 Answers
+ 3
while(p!=NULL) p will never be NULL === p = p->next; instead p->next;
16th Nov 2017, 11:01 PM
Š’Š°Š“ŠøŠ¼ Š”ухŠ¾Ń‚ŠøŠ½ (Vadim Sukhotin)
Š’Š°Š“ŠøŠ¼ Š”ухŠ¾Ń‚ŠøŠ½ (Vadim Sukhotin) - avatar
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.
16th Nov 2017, 11:14 PM
John Wells
John Wells - avatar