-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.cpp
More file actions
executable file
·116 lines (88 loc) · 2.71 KB
/
list.cpp
File metadata and controls
executable file
·116 lines (88 loc) · 2.71 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
#include <iostream>
#include "list.h"
using namespace std;
List::List(const int postcode): postal_code(postcode)
{
total_people_in_postal_code = 0;
size = 0;
head = NULL;
tail = NULL;
// cout << "A new List has been created!\n";
}
List::~List(void)
{
// cout << "A List is being destructed!\n";
delete head;
head = tail = NULL;
size = 0;
}
void List::insert(char* id)
{
if (size == 0) {
head = new list_node(id, NULL, NULL);
tail = head;
} else {
tail->next = new list_node(id, tail, NULL);
tail = tail->next;
}
size++;
}
bool List::remove(char* id)
{
if (size == 0) {
cout << "\n\nError in List::remove(). List is already empty.\n\n";
return false;
}
if (size == 1) {
delete head;
head = NULL;
tail = NULL;
} else {
list_nodePtr temp = head;
while (temp != NULL) {
if (strcmp(temp->id, id) != 0) {
temp = temp->next;
} else {
break;
}
}
if (temp == NULL) {
cout << "\n\nError in List::remove(). Voter with id " << id << " was not found/\n\n";
return false;
}
if (temp->prev == NULL) { // if this condition is true, then the item we want to remove is the head
head = head->next;
head->prev->next = NULL; // the destructor of list_node destructs the next node, but we don't want that now
delete head->prev;
head->prev = NULL;
} else if (temp->next == NULL) { // if this condition is true, then the item we want to remove is the tail
tail = tail->prev;
delete tail->next;
tail->next = NULL;
} else { // else, normally remove the item from the list and delete it
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
temp->prev = NULL;
temp->next = NULL; // the destructor of list_node deletes list_node->next. But we don't want the next element to be deleted, so we set temp->next to NULL
delete temp;
}
}
total_people_in_postal_code--;
size--;
return true;
}
/* increases by 1 the counter total_people_in_postal_code */
void List::add_person_to_postal_code(void)
{
total_people_in_postal_code++;
}
/* Decreases by 1 the counter total_people_in_postal_code */
bool List::remove_person_from_postal_code(void)
{
if (total_people_in_postal_code == 0) {
cout << "Error in List::remove_person_from_postal_code(). total_people_in_postal_code is already zero.\n";
return false;
}
total_people_in_postal_code--;
return true;
}