-
Notifications
You must be signed in to change notification settings - Fork 0
/
Double_sentinel_list.h
327 lines (310 loc) · 8.51 KB
/
Double_sentinel_list.h
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef DOUBLE_SENTINEL_LIST_H
#define DOUBLE_SENTINEL_LIST_H
#include<iostream>
#include "Exception.h"
#define nullptr NULL
template<typename Type>
class Double_sentinel_list
{
public:
class Double_node
{
public:
Double_node(Type const& = Type(), Double_node* = nullptr, Double_node* = nullptr);
Type node_value;
Double_node* previous_node;
Double_node* next_node;
Type value() const;
Double_node* next() const;
Double_node* previous() const;
};
Double_sentinel_list();
~Double_sentinel_list();
Double_sentinel_list(Double_sentinel_list const&);
Double_sentinel_list(Double_sentinel_list&&);
//Accessors
int size() const;
void traversal();
bool empty() const;
Type front() const;
Type back() const;
Double_node* begin() const;
Double_node* end() const;
Double_node* rbegin() const;
Double_node* rend() const;
Double_node* find(Type const&) const;
int count(Type const&) const;
//Mutators
void swap(Double_sentinel_list& );
Double_sentinel_list& operator=(Double_sentinel_list const);
Double_sentinel_list& operator=(Double_sentinel_list&&);
void push_front(Type const&);
void push_back(Type const&);
void pop_front();
void pop_back();
int erase(Type const& value);
private:
int list_size;
Double_node* head;
Double_node* tail;
};
//declarations
template <typename Type>
Double_sentinel_list<Type>::Double_sentinel_list()
{
head = new typename Double_sentinel_list<Type>::Double_node(0, nullptr, nullptr);
tail = new Double_sentinel_list<Type>::Double_node(0, nullptr, nullptr);
//List head is now a double node (Acts like a head sentinel) which contains null with 2 nullptrs.
//List tail is now a double node (Acts like a tail sentinel) and will contain null and 2 pointers. Update its 'previous' pointer to point to the head sentinel.
list_size = 0; //THe list size is 0 since list is empty.
head->next_node = tail;
tail->previous_node = head;
}
template<typename Type>
Double_sentinel_list<Type>::~Double_sentinel_list()
{
list_size=0;
typename Double_sentinel_list<Type>::Double_node* ptr =head;
while (ptr != end())
{
typename Double_sentinel_list<Type>::Double_node* tmp = ptr;
ptr = ptr->next_node;
delete tmp;
tmp = nullptr;
}
delete ptr;
ptr = nullptr;
}
template<typename Type>
Double_sentinel_list<Type>::Double_sentinel_list(Double_sentinel_list<Type> const& list)
{
list_size = list.list_size;
typename Double_sentinel_list<Type>::Double_node* ptr = (list.head);
ptr = ptr->next_node;
head = new typename Double_sentinel_list<Type>::Double_node(0, nullptr, nullptr);
tail = new typename Double_sentinel_list<Type>::Double_node(0, head, nullptr);
head->next_node = tail;
typename Double_sentinel_list<Type>::Double_node* cur = head;
while (ptr!=list.end())
{
typename Double_sentinel_list<Type>::Double_node* obj=new typename Double_sentinel_list<Type>::Double_node(ptr->node_value, cur, nullptr);
cur->next_node = obj;
cur = obj;
ptr = ptr->next_node;
}
tail->previous_node = cur;
cur->next_node = tail;
}
template<typename Type>
Double_sentinel_list<Type>::Double_sentinel_list(Double_sentinel_list<Type>&& list)
{
head = new Double_node(0, nullptr, nullptr);
tail = new Double_node(0, head, nullptr);
head->next_node = tail;
list_size = 0;
swap(list);
}
// //Accessors Declaration
template<typename Type>
int Double_sentinel_list<Type>::size() const
{
return list_size;
}
template<typename Type>
bool Double_sentinel_list<Type>::empty() const
{
if (list_size == 0)
return true;
else
return false;
}
template<typename Type>
Type Double_sentinel_list<Type>::front() const
{
if (empty())
{
throw underflow();
}
typename Double_sentinel_list<Type>::Double_node* obj = head->next_node;
return obj->node_value;
}
template<typename Type>
Type Double_sentinel_list<Type>::back() const
{
if (empty())
{
throw underflow();
}
typename Double_sentinel_list<Type>::Double_node* obj = tail->previous_node;
return obj->node_value;
}
template<typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::begin() const
{
return head->next();
}
template<typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::end() const
{
return tail;
}
template<typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::rbegin() const
{
return tail->previous_node;
}
template<typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::rend() const
{
return head;
}
template<typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::find(Type const& obj) const
{
typename Double_sentinel_list<Type>::Double_node* ptr = head->next_node;
while (ptr != end())
{
if (ptr->node_value == obj)
return ptr;
ptr = ptr->next_node;
}
return end();
}
template<typename Type>
int Double_sentinel_list<Type>::count(Type const& obj) const
{
int cnt = 0;
typename Double_sentinel_list<Type>::Double_node* ptr = head;
while (ptr != end())
{
if (ptr->node_value == obj)
cnt++;
ptr = ptr->next_node;
}
return cnt;
}
//Mutators Declaration
template <typename Type>
void Double_sentinel_list<Type>::swap(Double_sentinel_list<Type>& list)
{
std::swap(head, list.head);
std::swap(tail, list.tail);
std::swap(list_size, list.list_size);
}
template <typename Type>
Double_sentinel_list<Type>& Double_sentinel_list<Type>::operator=(Double_sentinel_list<Type> rhs)
{
swap(rhs);
return *this;
}
template<typename Type>
Double_sentinel_list<Type>& Double_sentinel_list<Type>::operator=(Double_sentinel_list<Type>&& rhs)
{
swap(rhs);
return *this;
}
template<typename Type>
void Double_sentinel_list<Type>::push_front(Type const& new_value)
{
typename Double_sentinel_list<Type>::Double_node* obj = new typename Double_sentinel_list<Type>::Double_node(new_value, head, head->next_node);
head->next_node->previous_node = obj;
head->next_node = obj;
++list_size;
}
template<typename Type>
void Double_sentinel_list<Type>::push_back(Type const& new_value)
{
typename Double_sentinel_list<Type>::Double_node* ptr = new typename Double_sentinel_list<Type>::Double_node(new_value, tail->previous_node, tail);
tail->previous_node->next_node = ptr;
tail->previous_node = ptr;
++list_size;
}
template<typename Type>
void Double_sentinel_list<Type>::traversal()
{
typename Double_sentinel_list<Type>::Double_node* ptr = head->next_node;
while (ptr != end())
{
std::cout << ptr->node_value;
std::cout << "\n";
ptr = ptr->next_node;
}
}
template<typename Type>
void Double_sentinel_list<Type>::pop_front()
{
if (empty())
{
throw underflow();
}
typename Double_sentinel_list<Type>::Double_node* obj = head->next_node;
head->next_node = obj->next_node;
obj->next_node->previous_node = head;
delete obj;
obj = nullptr;
--list_size;
}
template<typename Type>
void Double_sentinel_list<Type>::pop_back()
{
if (empty())
throw underflow();
typename Double_sentinel_list<Type>::Double_node* obj = tail->previous_node;
obj->previous_node->next_node = tail;
tail->previous_node = obj->previous_node;
delete obj;
obj = nullptr;
--list_size;
}
template<typename Type>
int Double_sentinel_list<Type>::erase(Type const& value)
{
int cnt = 0;
typename Double_sentinel_list<Type>::Double_node* ptr = head->next_node;
while (ptr != end())
{
typename Double_sentinel_list<Type>::Double_node* ptr2 = nullptr;
if (ptr->node_value == value)
{
cnt++;
ptr->previous_node->next_node = ptr->next_node;
ptr->next_node->previous_node = ptr->previous_node;
ptr2 = ptr;
}
ptr = ptr->next_node;
if (ptr2 != nullptr)
{
delete ptr2;
ptr2 = nullptr;
}
}
list_size-=cnt;
return cnt;
}
template <typename Type>
Double_sentinel_list<Type>::Double_node::Double_node(
Type const& nv,
typename Double_sentinel_list<Type>::Double_node* pn,
typename Double_sentinel_list<Type>::Double_node* nn) :
// Updated the initialization list here
node_value(nv), // This assigns 'node_value' the default value of Type
previous_node(pn),
next_node(nn)
{
}
template <typename Type>
Type Double_sentinel_list<Type>::Double_node::value() const {
// Enter your implementation here
return node_value; // This returns a default value of Type
}
template <typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::Double_node::previous() const {
// Enter your implementation here
return previous_node;
}
template <typename Type>
typename Double_sentinel_list<Type>::Double_node* Double_sentinel_list<Type>::Double_node::next() const {
// Enter your implementation here
return next_node;
}
#endif