-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.h
More file actions
197 lines (178 loc) · 3.49 KB
/
DoublyLinkedList.h
File metadata and controls
197 lines (178 loc) · 3.49 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
#ifndef DOUBLYLINKEDLIST_H
#define DOUBLYLINKEDLIST_H
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node() {
this->next = nullptr;
this->prev = nullptr;
}
Node(int data) {
this->data = data;
this->next = nullptr;
this->prev = nullptr;
}
};
class DoublyLinkedList {
private:
Node *head;
Node *tail;
public:
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
}
Node* getHead(){
if(head==nullptr){
return nullptr;
}
return head;
}
// returns the contents of the list as a vector starting from the head
vector<int>* getVector() const {
vector<int>* returnVector = new vector<int>();
Node* curr = head;
while(curr != nullptr){
returnVector->push_back(curr->data);
curr = curr->next;
}
return returnVector;
}
// Checks if the DoublyLinkedList is empty
bool isEmpty(){
return(head == nullptr && tail == nullptr);
}
// Clean up DoublyLinkedList
void deleteAllNodes() {
if (head != nullptr) {
Node *curr = head;
Node *next = nullptr;
while (curr != nullptr) {
next = curr->next;
delete curr;
curr = next;
}
head = nullptr;
tail = nullptr;
}
}
// Adds data to either the front or tail
void append(int data) {
Node *newNode = new Node(data);
if (head == nullptr) {
head = newNode;
tail = newNode;
return;
}
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
void appendNode(Node* newNode){
if (head == nullptr) {
head = newNode;
tail = newNode;
return;
}
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
// Deletes the first node
void deleteFirstNode() {
if (head == nullptr) {
return;
}
if (head == tail) {
delete head;
head = nullptr;
tail = nullptr;
return;
}
Node *temp = head->next;
temp->prev = nullptr;
delete head;
head = temp;
}
// Deletes the last node
void deleteLastNode() {
if (head == nullptr) {
return;
}
if (head == tail) {
delete head;
head = nullptr;
tail = nullptr;
return;
}
Node *temp = tail->prev;
temp->next = nullptr;
delete tail;
tail = temp;
}
// Deletes a node my passing a pointer
void deleteNode(Node *node) {
if (head == node) {
deleteFirstNode();
return;
}
if (tail == node) {
deleteLastNode();
return;
}
node->prev->next = node->next;
node->next->prev = node->prev;
delete node;
}
// Gets the root value
int getRootValue() {
if (head == nullptr) {
return -1;
}
return head->data;
}
// Debugging purposes
void printList() {
Node *temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
struct District {
// Population in *this* district
int population;
// Will hold & keep track of seniority of IDS(people in district)
DoublyLinkedList List;
// Constructor for District
District() {
population = 0;
List = DoublyLinkedList();
}
};
struct Person {
int id;
int x;
int y;
Node *pointer;
bool alive;
// Constructor for Person
Person(int idNumber, int xAxis, int yAxis) {
id = idNumber;
alive = true;
x = xAxis;
y = yAxis;
}
void setNullAndDelete() {
pointer = nullptr;
delete pointer;
}
};
#endif