单链表的逆序输出分为两种情况,一种是只逆序输出,实际上不逆序;另一种是把链表逆序。本文就分别实例讲述一下两种方法。具体如下:
1.逆序输出
实例代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;
typedef struct node{
int data;
node * next;
}node;
//尾部添加
node * add( int n, node * head){
node * t = new node;
t->data = n;
t->next = NULL;
if (head == NULL){
head = t;
}
else if (head->next == NULL){
head->next = t;
}
else {
node * p = head->next;
while (p->next != NULL){
p = p->next;
}
p->next = t;
}
return head;
}
//顺序输出
void print(node * head){
node * p = head;
while (p != NULL){
cout << p->data << " " ;
p = p->next;
}
cout << endl;
}
//递归
void reversePrint(node * p){
if (p != NULL){
reversePrint(p->next);
cout << p->data << " " ;
}
}
//栈
void reversePrint2(node * head){
stack< int > s;
while (head != NULL){
s.push(head->data);
head = head->next;
}
while (!s.empty()){
cout << s.top() << " " ;
s.pop();
}
}
int main(){
node * head = NULL;
for ( int i = 1; i <= 5; i++){
head = add(i, head);
}
print(head);
reversePrint(head);
reversePrint2(head);
system ( "pause" );
return 0;
}
|
逆序输出可以用三种方法: 递归,栈,逆序后输出。最后一种接下来讲到。
实例代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;
typedef struct node{
int data;
node * next;
}node;
node * add( int n, node * head){
node * t = new node;
t->data = n;
t->next = NULL;
if (head == NULL){
head = t;
}
else if (head->next == NULL){
head->next = t;
}
else {
node * p = head->next;
while (p->next != NULL){
p = p->next;
}
p->next = t;
}
return head;
}
//循环
node * reverse(node * head){
if (head == NULL || head->next == NULL){
return head;
}
node * p1 = head;
node * p2 = head->next;
node * p3 = NULL;
head->next = NULL;
while (p2 != NULL){
p3 = p2;
p2 = p2->next;
p3->next = p1;
p1 = p3;
}
head = p1;
return head;
}
void print(node * head){
node * p = head;
while (p != NULL){
cout << p->data << " " ;
p = p->next;
}
cout << endl;
}
//递归
node * reverse2(node * p){
if (p == NULL || p->next == NULL){
return p;
}
node * newHead = reverse2(p->next);
p->next->next = p;
p->next = NULL;
return newHead;
}
int main(){
node * head = NULL;
for ( int i = 1; i <= 5; i++){
head = add(i, head);
}
print(head);
head = reverse(head);
print(head);
head = reverse2(head);
print(head);
system ( "pause" );
return 0;
}
|
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 83
-
2025-06-04 58
-
2025-05-25 73
-
2025-05-25 27
-
2025-05-29 39
热门评论