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

题目描述

输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据。

输入

第一行输入整数的个数N;
第二行依次输入每个整数。

输出

输出这组整数。

示例输入

8
12 56 4 6 55 15 33 62

示例输出

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