顺序建立链表

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