-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cpp
131 lines (115 loc) · 2.5 KB
/
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
#include "List.h"
//List::List() : head(nullptr), tail(nullptr), nextPos(nullptr), numberOfNeighbors(0)
List::List() : head(nullptr), tail(nullptr), numberOfNeighbors(0)
{
}
List::~List() {
ClearList();
numberOfNeighbors = 0;
delete head;
head = nullptr;
tail = nullptr;
}
void List::AddToHead(ListNode* newNodeToAdd) {
newNodeToAdd->next = head->next;
newNodeToAdd->prev = head;
if (!IsEmpty()) {
head->next->prev = newNodeToAdd;
}
if (tail == nullptr) {
tail = newNodeToAdd;
}
head->next = newNodeToAdd;
numberOfNeighbors++;
}
ListNode* List::RemoveFromTail() {
ListNode* nodeToDelete = tail;
if (!IsEmpty()) {
tail = tail->prev;
tail->next = nullptr;
numberOfNeighbors--;
}
else {
tail = nullptr;
}
return nodeToDelete;
}
bool List::IsEmpty() {
bool result = false;
if (numberOfNeighbors == 0)
result = true;
return result;
}
void List::MakeEmptyList(int vertex) {
head = MakeNewNode(vertex);
numberOfNeighbors = 0;
tail = nullptr;
}
void List::ClearList() {
ListNode* nodeToDelete;
while (head->next != nullptr) {
nodeToDelete = head->next;
head->next = nodeToDelete->next;
delete nodeToDelete;
}
numberOfNeighbors = 0;
head->next = nullptr;
tail = head;
}
ListNode* List::MakeNewNode(int vertex, ListNode* prev, ListNode* next) {
ListNode* newNode = new ListNode;
newNode->vertex = vertex;
newNode->next = next;
newNode->prev = prev;
return newNode;
}
int List::GetListHeadValue() {
return head->vertex;
}
ListNode* List::GetListHead() {
return head->next;
}
ListNode* List::FindNodeByData(int vertex) {
ListNode* result = nullptr;
ListNode* curr = head->next;
while (curr != nullptr) {
if (curr->vertex == vertex)
{
result = curr;
break;
}
curr = curr->next;
}
return result;
}
bool List::IsNodeInList(int vertex) {
bool result = false;
ListNode * temp = FindNodeByData(vertex);
if (temp != nullptr)
result = true;
return false;
}
bool List::RemoveFromList(int vertex) {
bool result = false;
ListNode* temp = FindNodeByData(vertex);
if (temp == tail) {
temp = RemoveFromTail();
result = true;
}
else {
if (temp->next != nullptr) {
temp->next->prev = temp->prev;
}
temp->prev->next = temp->next;
numberOfNeighbors--;
result = true;
}
delete temp;
return result;
}
int List::AddToList(int vertex) {
int result = 1 ;
ListNode* newNode = MakeNewNode(vertex);
AddToHead(newNode);
return result;
}