-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked-list.cc
258 lines (215 loc) · 4.83 KB
/
linked-list.cc
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*Name: Linked list*
*Skills: template class, reference call by function, virtual deconstructor,
new & delete struct pointer or object
Tips: for the operations in linked list, be careful about following special cases
1, All: empty, return value, input value
2, Delete, Reverse: single node
3, Reverse: double node
4, Add, Delete: head, middle or tail
*/
#include <iostream>
using namespace std;
/*NOTE 1: cannot template a typedef*/
template<typename T>
struct Node
{
int data; // use template
Node<T>* next;
};
template<typename T>
class LinkedList
{
public:
LinkedList ()
: m_head (0)
{
}
/*All base class MUST have a virtual deconstructor*/
virtual ~LinkedList ()
{
}
/*operations: user API -- as simple as it can*/
struct Node<T>* Insert (T &data); // insert data to the head of list
struct Node<T>* Delete (T &data);
//struct Node<T>* Search (T &data, LinkedList<T> &list);
struct Node<T>* Reverse ();
void Print ();
private:
/*Internal methods*/
bool IsEmpty ();
private:
struct Node<T> *m_head;
};
/*NOTE 2: template class method format:
tempate<typename T>
returnValue ClassName<T>::MethodName ()
*/
template<typename T>
bool LinkedList<T>::IsEmpty ()
{
if (m_head != 0)
return false;
else
return true;
}
/*NOTE 3: define a new struct node:
struct variable case:
struct Node<T> node;
node.data = 0;
node.next = 0;
struct point case: MUST use new
struct Node<T>* node = new struct Node<T>;
node->data = 0;
node->next = 0;
NOTE 4:
new Object
delete pointer
*/
template<typename T>
struct Node<T>* LinkedList<T>::Insert (T &data)
{
if ( IsEmpty() == true)
{
// this is an empty linked list
// new a node and set its next to zero
struct Node<T> *node = new struct Node<T>;
node->data = data;
node->next = 0;
// add it to head
m_head = node;
}
else
{
// new a node and set its next the head
struct Node<T> *node = new struct Node<T>;
node->data = data;
node->next = m_head;
// add it to head
m_head = node;
}
return m_head;
}
template<typename T>
struct Node<T>* LinkedList<T>::Delete (T &data)
{
if (IsEmpty () == true)
return m_head;
if (m_head->data == data)
{
// delete head
struct Node<T>* head;
head = m_head;
m_head = m_head->next;
delete head;
return m_head;
}
else
{
struct Node<T>* cur;
struct Node<T>* pre;
pre = m_head;
cur = m_head->next;
while ( cur->data != data)
{
cur = cur->next;
pre = pre->next;
// NOTE 5: cannot put this into wihle loop conditions
// because if cur == 0, then cur->data will cause error
if (cur == 0)
break;
}
// delete tail
if ( cur->data == data && cur == 0)
{
pre->next = 0;
delete cur;
return m_head;
}
// delete middle node
if ( cur->data == data && cur->next != 0)
{
pre->next = cur->next;
delete cur;
return m_head;
}
}
}
template<typename T>
struct Node<T>* LinkedList<T>::Reverse ()
{
// empty
if (IsEmpty () == true)
return m_head;
// only head
if ( m_head->next == 0)
return m_head;
// two node case
if ( m_head->next->next == 0)
{
m_head->next->next = m_head;
m_head = m_head->next;
m_head->next->next = 0;
return m_head;
}
// define three pointers
struct Node<T>* parent;
struct Node<T>* me;
struct Node<T>* child;
parent = m_head;
me = parent->next;
child = me->next;
parent->next = 0;
while (child != 0)
{
me->next = parent;
// move forward
parent = me;
me = child;
child = child->next;
}
// reverse last
me->next = parent;
m_head = me;
return m_head;
}
template<typename T>
void LinkedList<T>::Print ()
{
if (IsEmpty () == true)
return;
struct Node<T> *node = m_head;
while (node != 0)
{
std::cout << node->data << " ";
node = node->next;
}
cout << endl;
}
int main (int argc, char *argv[])
{
typedef int type;
LinkedList<type> linkList;
struct Node<type> *node;
cout << "Insert : ";
int data = 8;
node = linkList.Insert (data);
node = linkList.Insert (++data);
node = linkList.Insert (++data);
linkList.Print ();
cout << "Reverse: ";
node = linkList.Reverse ();
linkList.Print ();
cout << "Delete : ";
data = 8;
node = linkList.Delete (data);
linkList.Print ();
cout << "Reverse: ";
node = linkList.Reverse ();
linkList.Print ();
/*
data = 11;
node = linkList.Insert (data);
linkList.Print ();
*/
return 0;
}