链表无法正常运行

    技术2024-01-26  107

    #include<stdio.h> #include<stdlib.h> typedef struct A{ int z; struct A *next; }a; a * ini(); void display(a * p); a * ini() { a *w=(a *)malloc(sizeof(a)); a *e=w; e->z=0; for(int i=1;i<10;i++) { a * r=(a*)malloc(sizeof(a)); r->z=i; // r->next=NULL; e->next=r; e=e->next; } return w; } void display(a * p) { a *w=p; while(w) { printf("%d ",w->z); w=w->next; } printf("\n"); } void main() { a *p=ini(); display(p); display(p); }

    当你发现运行不正常时· 或许你在初始化时,忘记了写z->next=NULL,(非null,文中注释的地方)

    Processed: 0.025, SQL: 9