您的位置:首页 > 理论基础 > 数据结构算法

sdut 2118-数据结构实验之链表三:链表的逆置

2016-12-12 18:07 627 查看
Statistic Discuss


Problem Description

输入多个整数,以-1作为结束标志,顺序建立一个带头结点的单链表,之后对该单链表的数据进行逆置,并输出逆置后的单链表数据。


Input

输入多个整数,以-1作为结束标志。


Output

输出逆置后的单链表数据。

Example Input

12 56 4 6 55 15 33 62 -1



Example Output

62 33 15 55 6 4 56 12



Hint

不得使用数组。


Author

在头插法的基础上稍稍改动即可

01
#include
<iostream>
02
#include<stdio.h>
03
#include
<stdlib.h>
04
using
namespace
std;
05
struct
node
06
07
{
08
09
int
date;
10
struct
node
*next;
11
};
12
int
main()
13
{
14
int
n;
15
struct
node
*L,*s,*r;
16
L=(
struct
node*)
malloc
(
sizeof
(
struct
node));
17
18
L->next=NULL;
19
r=L;
20
21
22
   
while
(
scanf
(
"%d"
,&n)&&n!=-1)
23
   
{
24
s=(
struct
node*)
malloc
(
sizeof
(
struct
node));
25
s->date=n;
26
s->next=r->next;
27
   
r->next=s;
28
29
   
}
30
s=L->next;
31
while
(s!=NULL)
32
{
33
printf
(
"%d
"
,s->date);
34
s=s->next;
35
}
36
return
0;
37
}
38
39
40
/***************************************************
41
User
name: YT1558503112东野
42
Result:
Accepted
43
Take
time: 0ms
44
Take
Memory: 148KB
45
Submit
time: 2016-12-12 18:01:14
46
****************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: