連結串列資料輸入與輸出的問題
阿新 • • 發佈:2019-02-12
程式碼如下:
#include<stdio.h> struct Node { int data; struct Node *next; }*Head,*p,*q; typedef struct Node *LinkList; void main() { int i,n=0; int idata; printf("輸入0結束\n"); scanf("%d",&idata); while(idata!=0) { if(n==0){p=(LinkList)malloc(sizeof(struct Node)); p->data=idata; q=p; n++;} else {p=(LinkList)malloc(sizeof(struct Node)); p->data=idata; p->next =q; n++; } scanf("%d",&idata); p=p->next ; } Head=q; for(i=0;i<n;i++) { if(i!=n-1) { printf("%d-->",Head->data); Head=Head->next; } else { printf("Null"); } free(q); q=Head; } printf("\n"); }