-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
151 lines (120 loc) · 3.61 KB
/
LinkedList.cpp
File metadata and controls
151 lines (120 loc) · 3.61 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
#include "LinkedList.h"
#include <iostream>
using namespace std;
LinkedList::LinkedList() {
this->name = "";
this->first = nullptr;
this->last = nullptr;
this->numNodes = 0; //no-arg. starts with no nodes
}
LinkedList::LinkedList(string word) {
this->name = "";
this->first = new Node(word);
this->last = first;
this->numNodes = 1; //starts with one node
}
LinkedList::~LinkedList() {
this->clear();
}
Node* LinkedList::getFirst() const {
return this->first;
}
Node* LinkedList::getLast() const {
return this->last;
}
void LinkedList::setFirst(Node* newFirst) {
this->first = newFirst;
}
void LinkedList::setLast(Node* newLast) {
this->last = newLast;
}
string LinkedList::getNodeData(Node* node) const {
string temp = "";
if (node != nullptr) {
temp = node->getData();
}
return temp; //returns blank string if nullptr
}
void LinkedList::setNodeData(Node* node, const string& newData) {
if (node != nullptr) {
node->setData(newData);
}
}
string LinkedList::getName() const {
return this->name;
}
void LinkedList::setName(const string& newName) {
this->name = newName;
}
void LinkedList::push_back(const string& pushedData) {
Node* temp = new Node(pushedData);
if (first == nullptr) { //if list empty
this->first = temp;
this->last = temp; //make new node the only node in list
} else {
this->last->setNext(temp); //temp becomes new last node
temp->setPrev(last); //last becomes 2nd last node
this->last = temp; //make last point to new last node (temp)
}
++this->numNodes;
}
void LinkedList::push_front(const string& pushedData) {
Node* temp = new Node(pushedData);
if (first == nullptr) { //if list empty
this->first = temp;
this->last = temp; //make new node the only node in list
} else {
this->first->setPrev(temp); //temp becomes new first node
temp->setNext(first); //first becomes 2nd first node
this->first = temp; //make last point to new first node (temp)
}
++this->numNodes;
}
void LinkedList::insert(const string& pushedData, Node* current) {
Node* toInsert = new Node(pushedData);
if (first == nullptr) { //if list empty
this->first = toInsert;
this->last = toInsert; //make new node the only node in list
} else {
Node* prevNode = current->getPrev(); //stores node that comes before pushedData
current->setPrev(toInsert); //current node linked to new node
toInsert->setNext(current); //new node linked to current node
toInsert->setPrev(prevNode); //new node linked to previous node
prevNode->setNext(toInsert); //previous node linked to new node
}
++this->numNodes;
}
void LinkedList::print() const {
Node* temp = this->first;
while (temp != nullptr) {
cout << temp->getData() << endl;
temp = temp->getNext(); //move to next node
}
}
void LinkedList::print_reverse() const {
Node* temp = this->last;
while (temp != nullptr) {
cout << temp->getData() << endl;
temp = temp->getPrev(); //move to previous node
}
}
int LinkedList::size() const {
return this->numNodes;
}
void LinkedList::setSize(int newSize) {
this->numNodes = newSize;
}
void LinkedList::clear() {
Node* current = this->first;
Node* temp;
while (current != nullptr) { //iterate through list;
temp = current;
current = current->getNext(); //move pointer to next node
delete temp; //delete node
} //current should end as nullptr
//deallocate private members
this->first = nullptr;
this->last = nullptr;
this->name = "";
this->numNodes = 0;
}