-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw311_single_linked_list.hpp
153 lines (128 loc) · 3.46 KB
/
hw311_single_linked_list.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
/*
* Harrison, Kevin
* Vanderhoef, Trevor
* Vansteel, Alexander
*/
#ifndef GVSU_CS263_SINGLE_LINKED_LIST
#define GVSU_CS263_SINGLE_LINKED_LIST
#include <iostream>
using namespace std;
/* The following single_linked_list class shall be implemented WITHOUT
* using any of the C++ data structures (std::vector, std::list, std::map,
* ...)
*
* This assignment is primarily a reinforcement of writing recursive
* functions to manipulate a recursive data structure.
*/
template <typename E>
class single_linked_list {
private:
/* a struct is a class whose members are all public */
struct Node;
Node *head;
public:
/* the following public functions are NOT RECURSIVE, but they will
* invoke the recursive counterpart */
/* Complete each of the following public function */
/* constructor */
single_linked_list() {
head = nullptr;
}
/* destructor */
~single_linked_list() {
/* call a RECURSIVE private function for removing
* all the nodes
*/
_destructor(head);
}
unsigned long size() const {
return _size(head);
}
void print (ostream& destination) const {
_print(head, destination);
}
bool is_contained (const E& x) const {
return _is_contained (head, x);
}
void addItem (const E& x) {
_add_Item(head, x);
}
void remove (const E& x) {
_removeItem(head, x);
}
void print_reverse (ostream& destination) const {
_print_reverse(head, destination);
}
private:
/* the following private functions are RECURSIVE */
unsigned long _size (Node *from) const {
if (from != nullptr) {
int size_after_me = _size(from->next);
return 1 + size_after_me;
}
else
return 0;
}
void _print (Node* from, ostream& out) const {
if (from != nullptr) {
out << from->data << " ";
_print (from->next, out);
}
}
bool _is_contained (Node *from, const E& val) const {
if (from != nullptr) {
if (from->data == val) {
return true;
}
else{
return _is_contained(from->next, val);
}
}
return false;
}
void _add_Item(Node* & from, const E& val) const {
if(from != nullptr) {
_add_Item(from->next, val);
}
else {
Node *temp = new Node;
temp->next = nullptr;
temp->data = val;
from = temp;
}
}
void _removeItem (Node* & from, const E& val) const {
if (from != nullptr) {
if (from->data == val) {
Node *temp = from;
from = from->next;
delete(temp);
}
else {
_removeItem(from->next, val);
}
}
}
void _destructor (Node* & from) const {
if (from != nullptr) {
Node *temp = from;
from = from->next;
delete(temp);
_destructor(from);
}
}
/* The following private functions are Non-Recursive */
void _print_reverse (Node *from, ostream& out) const {
int i = (int) size();
while (i > 0) {
Node *temp = from;
for (int n = 1; n < i; n++) {
temp = temp->next;
}
out << temp->data << " ";
i--;
}
}
};
#include "gvsu_node.hpp"
#endif