逆序建立链表

 1 #include<stdio.h>
2 #include<malloc.h>
3 struct stu
4 {
5 int data ;
6 struct stu *next ;
7 };
8 struct stu *creat(int n)
9 {
10 int i;
11 struct stu *head,*p;
12 head = (struct stu *)malloc(sizeof(struct stu));
13 head->next = NULL;
14 for(i = 1 ; i <= n ;i++)
15 {
16 p = (struct stu *)malloc(sizeof(struct stu));
17 scanf("%d", &p->data);
18 p->next = head->next;
19 head->next = p ;
20 }
21 return head;
22 }
23 void traverse_list(struct stu*head)
24 {
25 int i = 1;
26 struct stu *p;
27 p = head->next;
28 while(p!=NULL)
29 {
30 if(i != 1)
31 printf(" ");
32 printf("%d", p->data);
33 p = p->next;
34 i++;
35 }
36 printf("\n");
37 }
38 int main()
39 {
40 int n;
41 struct stu *head;
42 scanf("%d", &n);
43 head = creat(n);
44 traverse_list(head);
45 return 0;
46 }
原文地址:https://www.cnblogs.com/shangyu/p/2345688.html