/
LinkedList.cpp
222 lines (178 loc) · 4.13 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "LinkedList.h"
//// Public methods
//! No-arg constructor. Initializes an empty linked list
LinkedList::LinkedList(){
head = NULL;
tail = NULL;
}
//! Copy constructor. Makes a complete copy of its argument
LinkedList::LinkedList(const LinkedList & other){
LLNode* pNode = other.head;
head = NULL;
LLNode* pCurNode = NULL;
LLNode* pPrevNode = NULL;
// make sure we initialize our head
if(pNode){
pCurNode = new LLNode(*pNode);
this->head = pCurNode;
pNode = pNode->next;
}
while(pNode){
pPrevNode = pCurNode;
pCurNode = new LLNode(*pNode);
pCurNode->prev = pPrevNode;
pPrevNode->next = pCurNode;
pNode = pNode->next;
}
// pCurNode is our new tail
this->tail = pCurNode;
}
//! Destructor
LinkedList::~LinkedList(){
this->Clear();
}
//! Assignment operator. Makes a complete copy of its argument
//! @return A reference to oneself
LinkedList& LinkedList::operator =(const LinkedList & other){
if(head == other.head){
return *this;
}
if(this->head){
this->Clear();
}
LLNode* pNode = other.head;
LLNode* pCurNode = NULL;
LLNode* pPrevNode = NULL;
// make sure we initialize our head
if(pNode){
pCurNode = new LLNode(*pNode);
this->head = pCurNode;
pNode = pNode->next;
}
while(pNode){
pPrevNode = pCurNode;
pCurNode = new LLNode(*pNode);
pCurNode->prev = pPrevNode;
pPrevNode->next = pCurNode;
pNode = pNode->next;
}
// pCurNode is our new tail
this->tail = pCurNode;
return *this;
}
//! @return true if the list is empty, or false if the list is not empty
bool LinkedList::IsEmpty() const{
return (head == NULL);
}
//! Removes all values from the list
void LinkedList::Clear(){
LLNode* pToDelete;
LLNode* pCurNode = head;
while(pCurNode){
pToDelete = pCurNode;
pCurNode = pCurNode->next;
delete pToDelete;
}
head = NULL;
tail = NULL;
}
//! @return the number of values in the list
int LinkedList::GetSize() const{
int iCount = 0;
LLNode* pCurNode = head;
while(pCurNode){
iCount++;
pCurNode = pCurNode->GetNext();
}
return iCount;
}
//! @return a pointer to the first node in the list, or NULL if the list is empty
LLNode* LinkedList::GetFirst()const{
return head;
}
//! @returns a pointer to the last node in the list, or NULL if the list is empty
LLNode* LinkedList::GetLast()const{
return tail;
}
//! Inserts value v into the list after node n
//!
//! @param v The new value being inserted
//! @param n A node that is already in the list after which the new node should
//! be inserted.
//! If n is NULL, the new node should be inserted at the beginning of the list.
//!
//! @return a pointer to the newly inserted node
LLNode* LinkedList::Insert(const std::string & v, LLNode * n){
LLNode* pNode;
LLNode* pPrev = NULL;
LLNode* pNext = NULL;
if(n){
pPrev = n;
if(n->next){
pNext = n->next;
}
pNode = new LLNode(v, pPrev, pNext);
if(pNext){
pNext->prev = pNode;
}
pPrev->next = pNode;
if(n == tail){
tail = pNode;
}
}else{
pNext = head;
pNode = new LLNode(v, pPrev, pNext);
if(head){
pNode->next = head;
head->prev = pNode;
}else{
tail = pNode;
}
head = pNode;
}
return pNode;
}
//! Searches for the first occurrence of value v that appears in the list
//! after node n
//!
//! @param v The value being searched for
//! @param n The node in the list after which the search should begin.
//! If n is NULL, the list should be searched from the beginning.
//!
//! @return a pointer to the node containing v, or NULL if v is not found
LLNode* LinkedList::Find(const std::string & v, LLNode * n) const{
if(n && n->next){
if(v.compare(n->next->value) == 0){
return n->next;
}else{
return Find(v, n->next);
}
}else if(!n && head){
if(v.compare(head->value) == 0){
return head;
}else{
return Find(v, head);
}
}
return NULL;
}
//! Removes node n from the list
//!
//! @param n The node being removed from the list
void LinkedList::Remove(LLNode * n){
if(n != NULL){
if(n->prev != NULL){
n->prev->next = n->next;
}
if(n->next != NULL){
n->next->prev = n->prev;
}
if(n == head){
head = n->next;
}
if(n == tail){
tail = n->prev;
}
delete n;
}
}