2116=数据结构实验之链表一:顺序建立链表

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct node
 4 {
 5     int data;
 6     struct node*next;
 7 };
 8 int main()
 9 {
10     int n,i;
11     struct node*head,*end,*p;
12     head=(struct node*)malloc(sizeof(struct node));//为head在这个链表中开辟一个空间。
13     head->next=NULL;//一个链表要有结尾。
14     end=head;//用end的移动来确定下一个P。
15     scanf("%d",&n);
16     for(i=0;i<n;i++)
17     {
18         p=(struct node*)malloc(sizeof(struct node));
19         scanf("%d",&p->data);
20         p->next=NULL;
21         end->next=p;
22         end=p;//进行end的移动。
23     }
24     for(p=head->next;p;p=p->next)
25     {
26 
27         printf("%d",p->data);
28         if(p->next!=NULL)printf(" ");
29     }
30     return 0;
31 }
原文地址:https://www.cnblogs.com/Angfe/p/10430305.html