数据结构实验之链表三:链表的逆置
Time Limit: 1000MS
Memory Limit: 65536KB
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
数据结构实验之链表三:链表的逆置
Time Limit: 1000MS
Memory Limit: 65536KB
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
ANSWER:
01 | #include<bits/stdc++.h> |
02 | using namespace std; |
03 |
04 | typedef struct node |
05 | { |
06 | int data; |
07 | struct node *next,*front; |
08 | }link_list; |
09 |
10 | int main() |
11 | { |
12 | link_list *p,*head; |
13 | head = new node; |
14 | head->next = NULL; |
15 | int n; |
16 | while (cin>>n) |
17 | { |
18 | if (n==-1) |
19 | { |
20 | break ; |
21 | } |
22 | else |
23 | { |
24 | p = new node; |
25 | p->data = n; |
26 | p->next = head->next; |
27 | head->next = p; |
28 | } |
29 | } |
30 | p = head->next; |
31 | while (p) |
32 | { |
33 | if (p->next!=NULL) |
34 | { |
35 | cout <<p->data<< " " ; |
36 |
37 | } |
38 | else |
39 | { |
40 | cout <<p->data<< "\n" ; |
41 | } |
42 | p=p->next; |
43 | } |
44 | return 0; |
45 | } |