首页 > 其他 > 详细

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

时间:2019-12-18 20:48:47      阅读:85      评论:0      收藏:0      [点我收藏+]

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

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

Input

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

Output

输出这组整数。

Sample Input

8
12 56 4 6 55 15 33 62

Sample Output

12 56 4 6 55 15 33 62

Hint

不得使用数组!

Source

 

 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,*p,*tail;
12     head = (struct node*)malloc(sizeof(struct node));
13     head->next = NULL;
14     tail = head;
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         tail->next = p;
22         tail = p;
23     }
24     for(p = head->next;p!=NULL;p=p->next)
25     {
26         if(p->next==NULL)
27             printf("%d\n",p->data);
28         else printf("%d ",p->data);
29     }
30     return 0;
31 }

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

原文:https://www.cnblogs.com/xiaolitongxueyaoshangjin/p/12061679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!