-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
161 lines (122 loc) · 2.85 KB
/
LinkedList.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
#include "LinkedList.h"
using namespace std;
Node::Node() {
natID = "";
next = 0;
}
Node::Node(string id) {
natID = id;
next = 0;
}
Node::~Node() {
}
//Linked List:
LinkedList::LinkedList() {
head = tail = 0;
count = 0;
}
int LinkedList::length() {
return count;
}
//insert while sorting
void LinkedList::insert(string id, unordered_map<string, User>& userHash) {
Node* newNode = new Node(id);
if (head == 0) {
head = tail = newNode;
}
else {
int insert_at = -1;
for (int i = 0; i < count; i++) {
Node* current_node = nodeAt(i);
//either the value is smaller than current node or the current node is the last node (tail)
if (userHash[id].age < userHash[current_node->natID].age) {
insert_at = i;
break;
}
}
//if insert_at is -1 (didn't get set) then there was no bigger value therfore the new node should be the tail
if (insert_at == -1) {
Node* tmp = tail;
tmp->next = newNode;
tail = newNode;
}
//it should become the new head
else if (insert_at == 0) {
newNode->next = head;
head = newNode;
}
//if insert_at got set, then find the node before the position to insert, make it point to the new node and then make new node point to the value that got its index replaced
else {
Node* previous_node = nodeAt(insert_at - 1);
newNode->next = nodeAt(insert_at);
previous_node->next = newNode;
}
}
count++;
}
Node* LinkedList::nodeAt(int index) {
Node* tmp = head;
for (int i = 0; i < index; i++)
tmp = tmp->next;
return tmp;
}
void LinkedList::deleteAt(string id) {
int index = -1;
for (int i = 0; i < count; i++) {
Node* current_node = nodeAt(i);
if (current_node->natID == id) {
index = i;
break;
}
}
//there are 3 cases, case #1: delete the head, case #2: delete the tail, case #3: delete a middle node
//case 1:
if (index == 0) {
Node* tmp = head;
head = head->next;
delete tmp;
}
//case 2:
else if (index == count - 1) {
//find the node before tail
Node* tmp = tail;
tail = nodeAt(count - 2);
delete tmp;
}
//case 3:
else {
//find the node before the node you want to delete (the one left side of it)
Node* tmp = nodeAt(index);
Node* previous_node = nodeAt(index - 1);
previous_node->next = tmp->next;
delete tmp;
}
count--;
}
void LinkedList::display() {
qDebug() << "\n -----------\n" << "List content: \n";
for (int i = 0; i < count; i++) {
Node* current_node = nodeAt(i);
qDebug() << current_node->natID << "\n";
}
qDebug() << " -----------\n";
}
//clear all elements and reset head and tail and count
void LinkedList::clear() {
Node* current = head;
while (current != NULL) {
Node* next = current->next;
delete current;
current = next;
}
head = tail = 0;
count = 0;
}
LinkedList::~LinkedList() {
Node* current = head;
while (current != NULL) {
Node* next = current->next;
delete current;
current = next;
}
}