overjjjj / c-

0 stars 0 forks source link

实例6 #6

Open overjjjj opened 3 years ago

overjjjj commented 3 years ago

反向输出一个链表。

overjjjj commented 3 years ago

include

include

include

typedef struct LNode{ int data; struct LNode next; }LNode,LinkList;

LinkList CreateList(int n); void print(LinkList h); int main() { LinkList Head=NULL; int n;

scanf("%d",&n);
Head=CreateList(n);

printf("刚刚建立的各个链表元素的值为:\n");
print(Head);

printf("\n\n");
system("pause");
return 0;

} LinkList CreateList(int n) { LinkList L,p,q; int i; L=(LNode*)malloc(sizeof(LNode)); if(!L)return 0; L->next=NULL; q=L; for(i=1;i<=n;i++) { p=(LinkList)malloc(sizeof(LNode)); printf("请输入第%d个元素的值:",i); scanf("%d",&(p->data)); p->next=NULL; q->next=p; q=p; } return L; } void print(LinkList h) { LinkList p=h->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } }