-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathprint_list_tail_to_head.cpp
More file actions
170 lines (154 loc) · 3.64 KB
/
print_list_tail_to_head.cpp
File metadata and controls
170 lines (154 loc) · 3.64 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright(c) 2019 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stack>
using namespace std;
struct ListNode {
ListNode(): m_pNext(NULL) {}
int m_nKey;
ListNode* m_pNext;
};
//此函数不进行安全检查
ListNode* find_tail(ListNode* pHead)
{
ListNode* res = pHead;
while(res->m_pNext)
res = res->m_pNext;
return res;
}
//此函数不进行安全检查
ListNode* find_value_prev(ListNode* pHead, int value)
{
ListNode* head = pHead;
ListNode* prev = pHead;
while(head->m_nKey != value && head->m_pNext) {
prev = head;
head = head->m_pNext;
}
if(head->m_nKey == value)
return prev;
else
return NULL;
}
void add_to_tail(ListNode** pHead, int value)
{
if(pHead) {
if(*pHead) {
ListNode* tail = find_tail(*pHead);
ListNode* next = new ListNode();
tail->m_pNext = next;
next->m_nKey = value;
} else {
*pHead = new ListNode();
(*pHead)->m_nKey = value;
}
}
}
void add_to_head(ListNode** pHead, int value)
{
if(pHead) {
if(*pHead) {
ListNode* pHead_ = *pHead;
*pHead = new ListNode();
(*pHead)->m_nKey = value;
(*pHead)->m_pNext = pHead_;
} else {
*pHead = new ListNode();
(*pHead)->m_nKey = value;
}
}
}
void remove_node(ListNode** pHead, int value)
{
if(pHead && *pHead) {
ListNode* prev = find_value_prev(*pHead, value);
if(prev) {
ListNode* rm = prev->m_pNext;
if(rm->m_pNext) {
ListNode* next = rm->m_pNext;
if(prev == *pHead)
*pHead = next;
else
prev->m_pNext = next;
delete rm;
} else { //此情况时,待删除的节点为最后一个或仅有的且是第一个
if(prev == *pHead)
*pHead = NULL;
else
prev->m_pNext = NULL;
delete rm;
}
}
}
}
void print_list_tail_to_head(ListNode* pHead)
{
if(pHead) {
stack<ListNode*> list_stack;
ListNode* node = pHead;
list_stack.push(node);
while(node->m_pNext) {
node = node->m_pNext;
list_stack.push(node);
}
while(!list_stack.empty()){
node = list_stack.top();
cout << node->m_nKey << ' ';
list_stack.pop();
}
cout << endl;
}
}
void print_list_head_to_tail(ListNode* pHead)
{
if(pHead) {
ListNode* head = pHead;
cout << head->m_nKey << ' ';
while(head->m_pNext) {
head = head->m_pNext;
cout << head->m_nKey << ' ';
}
cout << endl;
}
}
int main(int argc, char** argv)
{
ListNode* pHead = NULL;
add_to_tail(&pHead, 1);
add_to_tail(&pHead, 2);
add_to_tail(&pHead, 3);
add_to_tail(&pHead, 4);
add_to_tail(&pHead, 5);
print_list_head_to_tail(pHead);
print_list_tail_to_head(pHead);
add_to_head(&pHead, 10);
add_to_head(&pHead, 9);
add_to_head(&pHead, 8);
add_to_head(&pHead, 7);
add_to_head(&pHead, 6);
print_list_head_to_tail(pHead);
print_list_tail_to_head(pHead);
remove_node(&pHead, 6);
remove_node(&pHead, 5);
remove_node(&pHead, 10);
remove_node(&pHead, 3);
remove_node(&pHead, 4);
remove_node(&pHead, 7);
print_list_head_to_tail(pHead);
print_list_tail_to_head(pHead);
return 0;
}