-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathDoublyLinkedList.cpp
190 lines (148 loc) · 4.19 KB
/
DoublyLinkedList.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
#include <iostream>
class Node {
private:
int value;
public:
Node *next{nullptr};
Node *previous{nullptr};
// Node constructor
Node(int value) { this->value = value; }
int getValue() { return this->value; };
};
class DoublyLinkedList {
private:
Node *first{nullptr};
// Receives the node reference and make the pointers around stop pointing to
void remove_pointers_to(Node *&node) {
if (node->next) {
node->next->previous = node->previous;
}
if (node->previous) {
node->previous->next = node->next;
} else {
this->first = node->next;
}
}
public:
DoublyLinkedList() {}
void push_front(int value) {
// Initialize a pointier to a new node with received value
Node *node = new Node(value);
// Node points to the first
node->next = this->first;
// If there is a first node, make him point to new node
if (this->first) {
this->first->previous = node;
}
// Node becomes the first
this->first = node;
}
// Checks if there is a first node
bool isEmpty() {
if (!this->first) {
return true;
}
return false;
}
void push_back(int value) {
Node *node = new Node(value);
Node *selectedNode = this->first;
if (this->isEmpty()) {
this->first = node;
return;
}
Node *aux{nullptr};
while (selectedNode) {
aux = selectedNode;
selectedNode = selectedNode->next;
}
node->previous = aux;
aux->next = node;
};
void remove(int value) {
// Throw exception to empty List
if (this->isEmpty()) {
throw std::logic_error("List is empty, nothing to be removed!");
}
// Initialize a pointier to first element
Node *node = this->first;
// Go through the list until find the value or the end
while ((node) && (node->getValue() != value)) {
node = node->next;
}
// Throw exception if didn't find the value
if (!node) {
throw std::logic_error("Value must be in the list!");
}
// Remove all pointier to the value if exists
this->remove_pointers_to(node);
// Delete node
delete (node);
}
// Removes the k-th element, where k it's an ordinal number
void remove_ordinary(int position) {
if (this->isEmpty()) {
throw std::logic_error("List is empty, nothing to be removed!");
}
if (position < 1) {
throw std::logic_error("Invalid position!");
}
Node *selectedNode = this->first;
int aux = 1;
while ((selectedNode) && (aux != position)) {
selectedNode = selectedNode->next;
aux++;
}
if (!selectedNode) {
throw std::logic_error("Index out of bound!");
}
this->remove_pointers_to(selectedNode);
delete (selectedNode);
}
std::pair<bool, Node *> find(int value) {
// Throw exception to empty List
if (this->isEmpty()) {
throw std::logic_error("List is empty, nothing to be removed!");
}
// Initialize a pointier to first element
Node *node = this->first;
// Go through the list until find the value or the end
while ((node) && (node->getValue() != value)) {
node = node->next;
}
if (!node) {
return {false, nullptr};
}
return {true, node};
}
void print() {
Node *selectedNode = this->first;
std::cout << "[ ";
while (selectedNode) {
std::cout << selectedNode->getValue() << " ";
selectedNode = selectedNode->next;
}
std::cout << "]" << std::endl;
}
};
int main() {
DoublyLinkedList *doubly_linked_list = new DoublyLinkedList();
std::cout << "Push_front(20, 30, 40):" << std::endl;
doubly_linked_list->push_front(20);
doubly_linked_list->push_front(30);
doubly_linked_list->push_front(40);
doubly_linked_list->print();
std::cout << "Remove second!" << std::endl;
doubly_linked_list->remove_ordinary(2);
doubly_linked_list->print();
std::cout << "Remove(20, 40)!" << std::endl;
doubly_linked_list->remove(20);
doubly_linked_list->remove(40);
std::cout << "List is empty: ";
doubly_linked_list->print();
std::cout << "Push_back(512, 123, 412)!" << std::endl;
doubly_linked_list->push_back(512);
doubly_linked_list->push_back(123);
doubly_linked_list->push_back(412);
doubly_linked_list->print();
}