数据结构——链表之顺序建立链表

来源:互联网 发布:ssm商城源码 编辑:程序博客网 时间:2024/06/02 05:07

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

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

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

Input

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

Output

输出这组整数。

Example Input

812 56 4 6 55 15 33 62

Example Output

12 56 4 6 55 15 33 62

Hint

01#include <stdio.h>
02#include <stdlib.h>
03struct node
04{
05    int data;
06    struct node *next;
07};
08struct node *build(struct node *head,int n)
09{
10    struct node *tail,*p;
11    int i;
12    head->next=NULL;
13    tail=head;
14    for(i=1;i<=n;i++)
15    {
16        p=(struct node*)malloc(sizeof(struct node));
17        scanf("%d",&p->data);
18        p->next=NULL;
19        tail->next=p;
20        tail=p;
21    }
22    return(head);
23}
24struct node *show(struct node *head)
25{
26    struct node *p;
27    p=(struct node*)malloc(sizeof(struct node));
28    p=head->next;
29    while(p)
30    {
31        printf("%d ",p->data);
32        p=p->next;
33    }
34    return(head);
35}
36int main()
37{
38    int n;
39    struct node *head;
40    head=(struct node*)malloc(sizeof(struct node));
41    scanf("%d",&n);
42    build(head,n);
43    show(head);
44    return 0;
45}
46 
47 
48/***************************************************
49User name: jk160618郭衣鹏
50Result: Accepted
51Take time: 0ms
52Take Memory: 176KB
53Submit time: 2017-10-06 17:27:30
54****************************************************/

阅读全文
0 0
原创粉丝点击