-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorted List.cpp
194 lines (183 loc) · 5 KB
/
Sorted List.cpp
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
#include <iostream>
#include <string>
template <typename T>
class LinkedList {
protected:
struct Node {
T value;
Node* next;
};
int _size {0};
Node* _head;
public:
void add (T value, int index=-1) {
if (index == -1) // add to end by default
index = _size;
Node* temp {_head};
for (int i {0}; i < index-1; i++) { // get node to add after
temp = temp->next;
}
Node* temp2 = new Node(); // create new node
temp2->value = value;
if (index != 0) {
temp2->next = temp->next;
temp->next = temp2;
} else {
temp2->next = _head;
_head = temp2;
}
_size++;
}
void remove (T value, bool sorted=false) {
if (_head == nullptr) // empty list
return;
Node* temp {_head};
if (_head->value == value) { // remove head
temp = _head->next;
delete _head; // delete old head
if (_size == 1)
_head = nullptr; // empty list
else
_head = temp; // new head
_size--;
return;
} else { // remove other
for (int i {0}; i < _size-1; i++) {
if (temp->next->value == value) { // find node with next node matching value
Node* del {temp->next}; // node to be deleted
temp->next = del->next; // remove links to deleted node
delete del; // delete node
_size--;
return;
} else {
temp = temp->next;
if (sorted && temp->value > value) // break early when sorted
break;
}
}
std::cout << "Cannot remove\n";
return;
}
}
T pop (int index) {
if (index >= _size) {
std::cout << "Index Error\n";
return (int) NULL;
} else {
Node* temp {_head};
for (int i {0}; i < index-1; i++) { // iterate to end of list-1
temp = temp->next;
}
Node* del {temp->next};
T value = del->value;
temp->next = del->next;
delete del; // delete last element
_size--;
return value; // return removed value
}
}
bool contains (T value) const {
if (_head == nullptr) // empty list
return false;
Node* temp {_head};
for (int i {0}; i < _size; i++) {
if (temp->value == value) {
return true;
} else {
temp = temp->next;
}
}
return false;
}
int size () const {
return _size;
}
void print () const {
Node* temp {_head};
std::cout << "[ ";
for (int i {0}; i < _size; i++) {
std::cout << temp->value << " ";
temp = temp->next;
}
std::cout << "]\n";
}
T operator[] (int index) const {
if (index >= _size || index < -_size ) { // index out of range
std::cout << "Index Error\n";
return;
} else if (index == 0) { // return first value
return _head->value;
} else if (index > 0) {
index = ((index %= _size) < 0) ? index+_size: index;
Node* temp {_head};
for (int i {0}; i < index; i++) { // iterate to index
temp = temp->next;
}
return temp->value;
}
}
};
template <typename T>
class SortedList: public LinkedList<T> {
public:
void add (T value) {
struct LinkedList<T>::Node* temp = new struct LinkedList<T>::Node(); // create new node
temp->value = value;
if (LinkedList<T>::_head == nullptr) { // add to empty list
LinkedList<T>::_head = temp;
} else if (value < LinkedList<T>::_head->value) { // add to beginning
temp->next = LinkedList<T>::_head;
LinkedList<T>::_head = temp;
} else { // other
struct LinkedList<T>::Node* temp2 {LinkedList<T>::_head};
while (temp2->next != nullptr && value > temp2->next->value) {
temp2 = temp2->next;
}
temp->next = temp2->next;
temp2->next = temp;
}
LinkedList<T>::_size++;
}
void remove (T value) {
LinkedList<T>::remove(value, true);
}
};
int main() {
LinkedList<int> l;
l.add(2); // [2]
l.add(5); // [2, 5]
l.add(7); // [2, 5, 7]
l.print();
std::cout << l.size() << "\n"; // 3
std::cout << l.contains(2) << "\n"; // 1
l.remove(7); // [2, 5]
l.print();
l.remove(2); // [5]
l.print();
std::cout << l.contains(2) << "\n"; // 0
l.remove(2); // Cannot remove
l.remove(5); // []
l.print();
l.add(1, 0); // [1]
l.add(2, 1); // [1, 2]
l.add(4, 2); // [1, 2, 4]
l.print();
l.add(3, 2); // [1, 2, 3, 4]
l.print();
std::cout << l.pop(3) << std::endl; // 4; [1, 2, 3]
l.print();
SortedList<int> l2;
l2.add(2); // [2]
l2.add(1); // [1, 2]
l2.add(3); // [1, 2, 3]
l2.add(0); // [0, 1, 2, 3]
l2.add(4); // [0, 1, 2, 3, 4]
l2.print();
l2.remove(5); // Cannot remove
l2.remove(3); // [0, 1, 2, 4]
l2.remove(0); // [1, 2, 4]
l2.remove(4); // [1, 2]
l2.remove(1); // [2]
l2.remove(2); // []
l2.print();
}