-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalone_linked_list_sample_code.cpp
More file actions
568 lines (521 loc) · 11.3 KB
/
standalone_linked_list_sample_code.cpp
File metadata and controls
568 lines (521 loc) · 11.3 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/**
* @file linked_list_code_sample.cpp
* @author Max Dunn (maxdunn123@gmail.com)
* @copyright Copyright Max Dunn (c) 2020. All rights reserved.
*
* @brief LinkedList Code Sample
*
* This sample code demonstrates my ability to:
* - write classes
* - employ inheritance
* - employ polymorphism
* - combine classes to create useful data structures
*
* Instructions:
* 1) If not already done, change the file extension from .txt to .cpp
* $ mv linked_list_sample_code.txt linked_list_sample_code.cpp
* 2) Compile:
* $ g++ -std=c++11 linked_list_sample_code.cpp -o linked_list_sample_code
* 3) Run:
* $ ./linked_list_sample_code
*
* Sample Output:
* $ ./linked_list_sample_code
*
* -----------------------
* Linked List Sample Code
* -----------------------
* Author: Max Dunn (maxdunn123@gmail.com)
* GitHub: git@github.com/jomadu
* LinkedIn: www.linkedin.com/in/maxdunn123
* -----------------------
* Adding to LinkedList using LinkedList::append()
* { [0] }
* { [0] [1] }
* { [0] [1] [2] }
* { [0] [1] [2] [3] }
* { [0] [1] [2] [3] [4] }
* Done adding.
* { [0] [1] [2] [3] [4] }
* Clearing LinkedList by calling LinkedList::removeHead()
* { [1] [2] [3] [4] }
* { [2] [3] [4] }
* { [3] [4] }
* { [4] }
* { }
* Adding to LinkedList using LinkedList::prepend()
* { [0] }
* { [1] [0] }
* { [2] [1] [0] }
* { [3] [2] [1] [0] }
* { [4] [3] [2] [1] [0] }
* Clearing LinkedList by calling LinkedList::removeTail()
* { [4] [3] [2] [1] }
* { [4] [3] [2] }
* { [4] [3] }
* { [4] }
* { }
*
*/
#include <memory>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <iostream>
class Node
{
public:
Node();
Node(const int data);
int data() const;
void data(const int data);
protected:
int data_;
};
class LinkedListNode : public Node
{
public:
LinkedListNode();
LinkedListNode(const int data);
std::shared_ptr<LinkedListNode> next() const;
std::shared_ptr<LinkedListNode> prev() const;
void next(std::shared_ptr<LinkedListNode> next);
void prev(std::shared_ptr<LinkedListNode> prev);
private:
std::shared_ptr<LinkedListNode> next_;
std::shared_ptr<LinkedListNode> prev_;
};
class LinkedList
{
public:
LinkedList();
int size() const;
std::shared_ptr<LinkedListNode> head() const;
std::shared_ptr<LinkedListNode> tail() const;
virtual int data(const int idx) const;
virtual void data(const int idx, const int data);
virtual void append(const int data);
virtual void prepend(const int data);
virtual void insert(const int idx, const int data);
virtual void removeHead();
virtual void removeTail();
virtual void remove(const int idx);
void clear();
std::vector<std::shared_ptr<LinkedListNode>> search(const int data) const;
bool isEmpty() const;
void display() const;
protected:
int size_;
std::shared_ptr<LinkedListNode> head_;
std::shared_ptr<LinkedListNode> tail_;
};
/**
* @brief Construct a new Node::Node
*/
Node::Node() : data_(0)
{
}
/**
* @brief Construct a new Node::Node
*
* @param data initial data_
*/
Node::Node(const int data) : data_(data)
{
}
/**
* @brief Get data_
*
* @return int data_
*/
int Node::data() const
{
return data_;
}
/**
* @brief Set data_
*
* @param data
*/
void Node::data(const int data)
{
data_ = data;
}
/**
* @brief Construct a new LinkedListNode::LinkedListNode
*/
LinkedListNode::LinkedListNode() : next_(nullptr), prev_(nullptr)
{
}
/**
* @brief Construct a new LinkedListNode::LinkedListNode
*
* @param data initial data_
*/
LinkedListNode::LinkedListNode(const int data) : next_(nullptr), prev_(nullptr)
{
data_ = data;
}
/**
* @brief Get next_
*
* @return std::shared_ptr<LinkedListNode> next_
*/
std::shared_ptr<LinkedListNode> LinkedListNode::next() const
{
return next_;
}
/**
* @brief Get prev_
*
* @return std::shared_ptr<LinkedListNode> prev_
*/
std::shared_ptr<LinkedListNode> LinkedListNode::prev() const
{
return prev_;
}
/**
* @brief Set next_
*
* @param next
*/
void LinkedListNode::next(std::shared_ptr<LinkedListNode> next)
{
next_ = next;
}
/**
* @brief Set prev_
*
* @param prev
*/
void LinkedListNode::prev(std::shared_ptr<LinkedListNode> prev)
{
prev_ = prev;
}
/**
* @brief Construct a new LinkedList::Linked List
*/
LinkedList::LinkedList() : size_(0), head_(nullptr), tail_(nullptr)
{
}
/**
* @brief Gets size_
*
* @return int size_
*/
int LinkedList::size() const
{
return size_;
}
/**
* @brief Gets head_
*
* @return std::shared_ptr<LinkedListNode> head_
*/
std::shared_ptr<LinkedListNode> LinkedList::head() const
{
return head_;
}
/**
* @brief Gets tail_
*
* @return std::shared_ptr<LinkedListNode> tail_
*/
std::shared_ptr<LinkedListNode> LinkedList::tail() const
{
return tail_;
}
/**
* @brief Gets LinkedListNode::data_ from LinkedListNode at \p idx
*
* @param idx index in list. [0, (LinkedList::size_ - 1)]
* @return int LinkedListNode::data_ from LinkedListNode at \p idx
*/
int LinkedList::data(const int idx) const
{
auto curr = head_;
if (idx < 0 || idx >= size_)
{
throw std::out_of_range("Data retrieval idx out of range.");
}
for (auto i = 0; i < idx; i++)
{
curr = curr->next();
}
return curr->data();
}
/**
* @brief Sets LinkedListNode::data_ from LinkedListNode at \p idx
*
* @param idx index in list. [0, (LinkedList::size_ - 1)]
* @param data LinkedListNode::data_
*/
void LinkedList::data(const int idx, const int data)
{
auto curr = head_;
if (idx < 0 || idx >= size_)
{
throw std::out_of_range("Data retrieval idx out of range.");
}
for (auto i = 0; i < idx; i++)
{
curr = curr->next();
}
curr->data(data);
}
/**
* @brief Appends list with a LinkedListNode with data_ = \p data
*
* @param data LinkedListNode::data_
*/
void LinkedList::append(const int data)
{
auto new_node = std::make_shared<LinkedListNode>(data);
if (this->isEmpty())
{
head_ = new_node;
tail_ = new_node;
}
else
{
tail_->next(new_node);
tail_ = new_node;
}
size_++;
}
/**
* @brief Prepends list with a LinkedListNode with data_ = \p data
*
* @param data LinkedListNode::data_
*/
void LinkedList::prepend(const int data)
{
auto new_node = std::make_shared<LinkedListNode>(data);
if (this->isEmpty())
{
head_ = new_node;
tail_ = new_node;
}
else
{
new_node->next(head_);
head_ = new_node;
}
size_++;
}
/**
* @brief Inserts a LinkedListNode with data_ = \p data at index \p idx
*
* @param idx index in list. [0, (LinkedList::size_ - 1)]
* @param data LinkedListNode::data_
*/
void LinkedList::insert(const int idx, const int data)
{
if (idx <= 0)
{
prepend(data);
}
else if (idx >= size_)
{
append(data);
}
else
{
// Insert into middle
auto new_node = std::make_shared<LinkedListNode>(data);
auto curr = head_;
auto prev = head_;
for (auto i = 0; i < idx; i++)
{
prev = curr;
curr = curr->next();
}
prev->next(new_node);
new_node->next(curr);
size_++;
}
}
/**
* @brief Removes the first LinkedListNode from list
*/
void LinkedList::removeHead()
{
if (this->isEmpty())
{
return;
}
else if (head_ == tail_)
{
// Single node in list
head_.reset();
head_ = nullptr;
tail_ = nullptr;
}
else
{
// Multiple nodes in list
auto second = head_->next();
head_.reset();
head_ = second;
}
size_--;
}
/**
* @brief Removes the last LinkedListNode from list
*/
void LinkedList::removeTail()
{
if (this->isEmpty())
{
return;
}
else if (head_ == tail_)
{
// Single node in list
tail_.reset();
head_ = nullptr;
tail_ = nullptr;
}
else
{
// Multiple nodes in list
auto curr = head_;
auto prev = head_;
while (curr->next() != nullptr)
{
prev = curr;
curr = curr->next();
}
curr.reset();
prev->next(nullptr);
tail_ = prev;
}
size_--;
}
/**
* @brief Removes the LinkedListNode at index \p idx in the list
*
* @param idx index in list. [0, (LinkedList::size_ - 1)]
*/
void LinkedList::remove(const int idx)
{
if (idx <= 0)
{
removeHead();
}
else if (idx >= (size_ - 1))
{
removeTail();
}
else
{
// Remove from middle
auto curr = head_;
auto prev = head_;
for (auto i = 0; i < idx; i++)
{
prev = curr;
curr = curr->next();
}
prev->next(curr->next());
curr.reset();
size_--;
}
}
/**
* @brief Removes all LinkedListNodes from list
*/
void LinkedList::clear()
{
while (!isEmpty())
{
removeHead();
}
}
/**
* @brief Searches list for LinkedListNodes with data_ matching \p data
*
* @param data search parameter
* @return std::vector<std::shared_ptr<LinkedListNode>> vector of matching nodes
*/
std::vector<std::shared_ptr<LinkedListNode>> LinkedList::search(const int data) const
{
std::vector<std::shared_ptr<LinkedListNode>> results;
auto curr = head_;
for (auto i = 0; i < size(); i++)
{
if (curr->data() == data)
{
results.push_back(curr);
}
curr = curr->next();
}
return results;
}
/**
* @brief Evaluates if the list is empty
*
* @return true if empty
* @return false if not empty
*/
bool LinkedList::isEmpty() const
{
return ((head_ == nullptr) && (tail_ == nullptr));
}
/**
* @brief Prints formatted list to std::cout
*/
void LinkedList::display() const
{
std::stringstream ss;
ss << "{ ";
auto curr = head_;
for (auto i = 0; i < size(); i++)
{
ss << "[" + std::to_string(curr->data()) + "] ";
curr = curr->next();
}
std::cout << ss.str() << "}" << std::endl;
}
/**
* @brief Linked List Sample Code
*
* @return int
*/
int main()
{
std::cout << "-----------------------" << std::endl;
std::cout << "Linked List Sample Code" << std::endl;
std::cout << "-----------------------" << std::endl;
std::cout << "Author: Max Dunn (maxdunn123@gmail.com)" << std::endl;
std::cout << "GitHub: git@github.com/jomadu" << std::endl;
std::cout << "LinkedIn: www.linkedin.com/in/maxdunn123" << std::endl;
std::cout << "-----------------------" << std::endl;
LinkedList ll;
std::cout << "Adding to LinkedList using LinkedList::append()" << std::endl;
for (int i = 0; i < 5; i++)
{
ll.append(i);
ll.display();
}
std::cout << "Done adding." << std::endl;
ll.display();
std::cout << "Clearing LinkedList by calling LinkedList::removeHead()" << std::endl;
while (!ll.isEmpty())
{
ll.removeHead();
ll.display();
}
std::cout << "Adding to LinkedList using LinkedList::prepend()" << std::endl;
for (int i = 0; i < 5; i++)
{
ll.prepend(i);
ll.display();
}
std::cout << "Clearing LinkedList by calling LinkedList::removeTail()" << std::endl;
while (!ll.isEmpty())
{
ll.removeTail();
ll.display();
}
}