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

来源:互联网 发布:禁止外资参与网络出版 编辑:程序博客网 时间:2024/05/21 06:24

数据结构实验之链表二:逆序建立链表

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

Problem Description

输入整数个数N,再输入N个整数,按照这些整数输入的相反顺序建立单链表,并依次遍历输出单链表的数据。

Input

第一行输入整数N;;
第二行依次输入N个整数,逆序建立单链表。

Output

依次输出单链表所存放的数据。

Example Input

1011 3 5 27 9 12 43 16 84 22 

Example Output

22 84 16 43 12 9 27 5 3 11 

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 *p;
11    int i;
12    head->next=NULL;
13    for(i=1;i<=n;i++)
14    {
15        p=(struct node*)malloc(sizeof(struct node));
16        scanf("%d",&p->data);
17        p->next=head->next;
18        head->next=p;
19    }
20    return(head);
21}
22struct node *show(struct node *head)
23{
24    struct node *p;
25    p=(struct node*)malloc(sizeof(struct node));
26    p=head->next;
27    while(p)
28    {
29        printf("%d ",p->data);
30        p=p->next;
31    }
32    return(head);
33}
34int main()
35{
36    int n;
37    struct node *head;
38    head=(struct node*)malloc(sizeof(struct node));
39    scanf("%d",&n);
40    build(head,n);
41    show(head);
42    return 0;
43}
44 
45 
46/***************************************************
47User name: jk160618郭衣鹏
48Result: Accepted
49Take time: 0ms
50Take Memory: 148KB
51Submit time: 2017-10-06 17:33:22
52****************************************************/
阅读全文
0 0
原创粉丝点击