-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.hpp
292 lines (254 loc) · 6.31 KB
/
LinkedList.hpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* list.hpp
*
* functionality of a singly linked-list
*
* define your methods in coherence with the following
* function signatures
*
*
*/
#ifndef LIST_HPP
#define LIST_HPP 1
#include <iostream>
using namespace std;
namespace cs202
{
template<class T>
class LinkedList;
template<class T>
class Node_linkedList {
friend class LinkedList<T>;
private:
T _data;
Node_linkedList* _link;
public:
/*
* Primary constructor.
* creates an empty node.
* creates Link to NULL.
*/
Node_linkedList();
/*
* Secondary constructor.
* sets the value of node to val.
* links to NULL.
*/
Node_linkedList(T val);
/*
* Secondary constructor.
* sets value of node to val.
* links to link.
*/
Node_linkedList(T val, Node_linkedList* link);
/*
* Returns value stored in node.
*/
T& data();
/*
* Returns link to the next element stored.
*/
Node_linkedList* link();
/*
* Sets link to the link.
*/
void set_link(Node_linkedList* link);
/*
* Sets data to the val.
*/
void set_data(T val);
};
template<class T>
Node_linkedList<T>::Node_linkedList() {
_link = NULL;
}
template<class T>
Node_linkedList<T>::Node_linkedList(T val) {
_data = val;
_link = NULL;
}
template<class T>
Node_linkedList<T>::Node_linkedList(T val, Node_linkedList* link) {
_data = val;
_link = link;
}
template<class T>
T& Node_linkedList<T>::data() {
return _data;
}
template<class T>
Node_linkedList<T>* Node_linkedList<T>::link() {
return _link;
}
template<class T>
void Node_linkedList<T>::set_link(Node_linkedList* link) {
_link = link;
}
template<class T>
void Node_linkedList<T>::set_data(T val) {
_data = val;
}
template<class T>
class LinkedList
{
private:
Node_linkedList<T>* _head;
Node_linkedList<T>* _tail;
int _size;
public:
/*
* Primary contructor.
* Should construct an empty list.
* Size of the created list should be zero.
*/
LinkedList();
/*
* Seondary constructor.
* Creates a new list which is a copy of the provided list.
*/
LinkedList(LinkedList<T> & x);
/*
* Destructor.
* Frees all the memory acquired by the list.
*/
~LinkedList();
/*
* adds value at the end of the list.
*/
void append(const T& value);
/*
* Returns the length of the list.
*/
inline int length();
/*
* Returns a boolean indicating whether the list is empty.
*/
inline bool empty();
/*
* Adds a value to the front of the list.
*/
void cons(const T& value);
/*
* Removes the first occurence of the value from list.
*/
void remove(const T& x);
/*
* Appends the given list x at the end of the current list.
*/
void append(LinkedList<T>& x);
/*
* Returns starting pointer of the list
*/
Node_linkedList<T>* get_head();
/*
* Returns ending pointer of the list
*/
Node_linkedList<T>* get_tail();
/*
*
*/
void set_head(Node_linkedList<T>* temp);
};
template<class T>
LinkedList<T>::LinkedList() {
_head = NULL;
_tail = NULL;
_size = 0;
}
template<class T>
LinkedList<T>::LinkedList(LinkedList<T> & x) {
Node_linkedList<T>* traverse = x.get_head();
_head = new Node_linkedList<T>;
_head -> set_data(traverse -> data());
_tail = _head;
traverse = traverse -> link();
_size++;
while(traverse != NULL) {
Node_linkedList<T>* ptr = new Node_linkedList<T>;
ptr -> set_data(traverse -> data());
_tail -> set_link(ptr);
_tail = _tail -> link();
traverse = traverse -> link();
_size++;
}
}
template<class T>
void LinkedList<T>::append(const T& value) {
_size++;
Node_linkedList<T>* new_node = new Node_linkedList<T>;
new_node -> _data = value;
if(_tail == NULL) {
_tail = new_node;
_head = new_node;
return;
}
_tail -> _link = new_node;
_tail = new_node;
}
template<class T>
inline int LinkedList<T>::length() {
return _size;
}
template<class T>
inline bool LinkedList<T>::empty() {
return (_size == 0);
}
template<class T>
void LinkedList<T>::cons(const T& value) {
_size++;
Node_linkedList<T>* new_node = new Node_linkedList<T>;
new_node -> _data = value;
new_node -> _link = _head;
_head = new_node;
if(_tail == NULL)
_tail = _head;
}
template<class T>
void LinkedList<T>::remove(const T& x) {
Node_linkedList<T>* trav = _head;
if(_head -> _data == x) {
_head = _head -> _link;
delete trav;
_size--;
return;
}
while(trav -> _link != NULL) {
if(trav -> _link -> _data == x) {
Node_linkedList<T>* pos = trav -> _link;
trav -> _link = trav -> _link -> _link;
delete pos;
_size--;
return;
}
}
}
template<class T>
void LinkedList<T>::append(LinkedList<T>& x) {
_size += x.length();
_tail -> _link = x.get_head();
_tail = x.get_tail();
_tail -> _link = NULL;
}
template<class T>
Node_linkedList<T>* LinkedList<T>::get_head() {
return _head;
}
template<class T>
Node_linkedList<T>* LinkedList<T>::get_tail() {
return _tail;
}
template<class T>
LinkedList<T>::~LinkedList() {
Node_linkedList<T>* trav = _head;
while(_head != NULL) {
_head = _head -> _link;
delete trav;
trav = _head;
}
}
template<class T>
void LinkedList<T>::set_head(Node_linkedList<T>* temp) {
_head = temp;
}
}
#endif