-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
144 lines (125 loc) · 3.45 KB
/
LinkedList.cpp
File metadata and controls
144 lines (125 loc) · 3.45 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
#include <stdexcept>
#include <iostream>
#include <string>
#include "Node.hpp"
#include "LinkedList.hpp"
/******************************
M ROGOVE
2019-08-04
Please see specification document for further detail
LinkedList.cpp
contents of array for hash table.
handles all Node mem
*******************************/
//constructors
LinkedList::LinkedList(){
this->head = nullptr;
this->size = 0;
}
//not implemented
LinkedList::LinkedList(const LinkedList& other){
throw std::runtime_error("Not Implemented");
}
//not implemented
LinkedList& LinkedList::operator=(const LinkedList& other){
throw std::runtime_error("Not Implemented");
}
LinkedList::~LinkedList(){
while(this->head != nullptr) {
Node* pickoff = this->head;
this->head = pickoff->getNext();
delete pickoff;
}
if (this->head == nullptr){
delete head;
}
}
void LinkedList::addToFront(Node* node){
if (this->head == nullptr){
this->head = node;
} else {
Node* whereHeadLooks = this->head;
node->setNext(whereHeadLooks);
this->head = node;
}
this->size++;
}
void LinkedList::removeFront(){
if(this->head == nullptr){return;}
Node* current = this->head;
this->head = current->getNext();
delete current;
this->size--;
}
bool LinkedList::removeWord(std::string word){
Node* pointerToDelete;
Node* current = this->head;
Node* prev;
if(this->head == nullptr){return false;}//empty list, implicitly.
//what if it's the head?
if(this->head->getWord()==word){
pointerToDelete = this->head;
this->head = this->head->getNext();
delete pointerToDelete;
}
else{
prev = current; //it's head now
current = this->head->getNext();
while(current!=nullptr && current->getWord()!=word){
prev = current;
current = current->getNext();
}
if(current != nullptr){//implicitly, let's delete current.
pointerToDelete = current;
prev->setNext(current->getNext());
delete pointerToDelete;
} else{return false;}
}
this->size--;//implicitly we succeeded
return true;//and return
}
Node* LinkedList::findWord(std::string word) const{
// std::cout <<"word is "<<word<<std::endl;
if(this->head == nullptr){return nullptr;}//empty list, implicitly.
Node* current = this->head;
while(current!=nullptr){
if(current->getWord()!=word){
current = current->getNext();
}
else{return current;}
}
//implicitly, we return current since we don't match:
//this should never be reached:
return nullptr;
}
int LinkedList::getSize() const{
return this->size;
}
bool LinkedList::isEmpty() const {
if(this->head == nullptr){return true;}
return false;
}
Node* LinkedList::at(int ith) const {
return at(this->head,0,ith);
}
Node* LinkedList::at(Node* current, int tracking, int ith) const {
if(tracking == ith){return current;}//base case
else{return at(current->getNext(),tracking+1,ith);}
}
std::string LinkedList::toString() const{
std::string aggcombo = "";
aggcombo.append("|(front)->");
if (this->head == nullptr) {//if this is the tail, append terminator.
aggcombo.append("_|");
} else {
Node* current = this->head;
while (current->getNext() != nullptr) {
aggcombo.append("["+current->toString()+"]"); //add next value.
aggcombo.append("->"); //and arrow thingy
current = current->getNext();
};
aggcombo.append("["+current->toString()+"]"); //add next value.
}
aggcombo.append("|"); //add terminator once we're at nullptr.
return aggcombo;
}