-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
205 lines (174 loc) · 4.2 KB
/
LinkedList.cpp
File metadata and controls
205 lines (174 loc) · 4.2 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
198
199
200
201
202
203
204
205
#include <cmath>
#include <iostream>
#include <sstream>
#include "LinkedList.h"
using namespace std;
//*********** Node Class Definition ***********
Node::Node() {
next = NULL;
}
Node::Node(string w) {
word = w;
frequency = 0;
next = NULL;
}
string Node::toString() {
return word;
}
// ******** End Node Class Definition *********
// ******* Linked List Class Definition *******
LinkedList::LinkedList(const LinkedList& listToCopy) {
head = NULL;
tail = NULL;
if(listToCopy.size == 0){
return;
} else {
try {
Node* ptr = listToCopy.head;
while (ptr != NULL) {
Node* t = new Node(*ptr);
this->add(t);
ptr = ptr->next;
}
} catch(bad_alloc err) {
cout << "Bad allocation error." << endl;
}
}
}
// Overloaded default constructor
LinkedList::LinkedList() {
size = 0;
head = NULL;
tail = NULL;
}
// Contructor that takes in a head Node
LinkedList::LinkedList(Node* n) {
size = 0;
head = n;
tail = n;
}
// Destructor
LinkedList::~LinkedList() {
Node* ptr = head;
while (ptr != NULL) {
Node* tmp = ptr->next;
delete ptr;
ptr = tmp;
}
}
// Copies a LinkedList to replace an existing LinkedList
void LinkedList::copyList(const LinkedList& ll) {
head = NULL;
tail = NULL;
if (ll.size == 0){
return;
} else {
try {
Node* ptr = ll.head;
while (ptr != NULL) {
Node* t = new Node(*ptr);
this->add(t);
ptr = ptr->next;
}
} catch(bad_alloc err) {
cout << "Bad allocation error." << endl;
}
}
}
// Overloaded assignment operator
LinkedList LinkedList::operator=(const LinkedList& ll) {
LinkedList tmp_list;
tmp_list.copyList(ll);
return tmp_list;
}
// Adds a Node to the end of a LinkedList
void LinkedList::add(Node* n) {
size++;
if (head == NULL) { // List is empty, add Node to the front
head = n;
tail = n;
} else { // List is not empty, add to end of list
tail->next = n;
tail = n;
}
}
// Prints list in order (beginning at head, ending at tail)
void LinkedList::printList() {
Node* ptr = head;
if (head == NULL || size == 0) {
cout << "Empty list." << endl;
return;
}
while (size > 0 && ptr != NULL) {
cout << ptr->toString() << endl;
ptr = ptr->next;
}
}
// Returns true if word is found and moved to front
// Returns false if word is not in list (misspelled word)
bool LinkedList::findAndMoveFront(string word) {
if (head == NULL) {
return false;
}
Node *found = NULL;
Node *beforeFound = NULL;
if (head->getWord() == word) {
found = head;
} else {
Node *previous = head;
Node *ptr = head->next;
while (ptr != NULL) {
if (ptr->getWord() == word) {
found = ptr;
beforeFound = previous;
break;
}
previous = ptr;
ptr = ptr->next;
}
}
if (found == NULL) {
return false;
} else if (found == head) {
found->increaseFrequency();
return true;
} else {
found->increaseFrequency();
beforeFound->next = found->next;
found->next = head;
head = found;
return true;
}
}
void LinkedList::remove(string word) {
if (head == NULL) {
return;
}
Node *found = NULL;
Node *beforeFound = NULL;
if (head->getWord() == word) {
found = head;
} else {
Node *previous = head;
Node *ptr = head->next;
while (ptr != NULL) {
if (ptr->getWord() == word) {
found = ptr;
beforeFound = previous;
break;
}
previous = ptr;
ptr = ptr->next;
}
}
if (found == NULL) {
return;
} else if (found == head) {
head = head->next;
found = NULL;
} else {
beforeFound->next = found->next;
found = NULL;
}
}
// ******** End LinkedList Class Definition *********