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

来源:互联网 发布:容声和美菱哪个好 知乎 编辑:程序博客网 时间:2024/06/06 13:18

Problem Description

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

Input

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

Output

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

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 <iostream>
02#include<stdio.h>
03#include <stdlib.h>
04 
05using namespace std;
06struct node
07 
08{
09 
10    int date;
11    struct node *next;
12};
13int main()
14{
15    int i,n;
16    struct node *L,*s,*r;
17    L=(struct node*)malloc(sizeof(struct node));
18 
19    L->next=NULL;
20    r=L;
21 
22    cin>>n;
23    for(i=0;i<n;i++)
24    {
25 
26        s=(struct node*)malloc(sizeof(struct node));
27        scanf("%d",&s->date);
28        s->next=r->next;
29        r->next=s;
30 
31 
32    }
33 
34    s=L->next;
35    while(s!=NULL)
36    {
37        printf("%d ",s->date);
38        s=s->next;
39    }
40    return 0;
41}
42 
43 
44/***************************************************
45User name: YT1558503112东野
46Result: Accepted
47Take time: 0ms
48Take Memory: 160KB
49Submit time: 2016-12-12 17:36:05
50****************************************************/

0 0