-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
171 lines (132 loc) · 2.31 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
/**
* @file : LinkedList.h
* @author : Ethan Ward
* @date : 2015.02.12
* @brief: Creates Pokemon and changes their values when needed.
*/
#include "LinkedList.h"
LinkedList::LinkedList() {
m_front = nullptr;
m_size = 0;
}
LinkedList::~LinkedList() {
Node* temp;
Node* temp2;
temp = m_front;
temp2 = temp;
while(temp != nullptr) {
temp = temp->getNext();
delete temp2;
temp2 = temp;
}
}
bool LinkedList::isEmpty() const {
if(size() == 0) {
return(true);
}
else {
return(false);
}
}
int LinkedList::size() const {
return(m_size);
}
bool LinkedList::search(int value) const {
Node* temp;
temp = m_front;
while(temp != nullptr) {
if(temp->getValue() == value) {
return(true);
}
temp = temp->getNext();
}
return(false);
}
void LinkedList::printList() const {
if(isEmpty()) {
std::cout << "List Empty.";
}
else {
Node* temp;
temp = m_front;
while(temp != nullptr) {
std::cout << temp->getValue() << " ";
temp = temp->getNext();
}
}
}
void LinkedList::addBack(int value) {
Node* temp = new Node();
Node* temp2;
temp->setValue(value);
if(m_size == 0) {
m_front = temp;
m_size++;
}
else if (m_size == 1) {
m_front->setNext(temp);
m_size++;
}
else {
temp2 = m_front;
while(temp2->getNext() != nullptr) {
temp2 = temp2->getNext();
}
temp2->setNext(temp);
m_size++;
}
}
void LinkedList::addFront(int value) {
Node* temp = new Node();
temp->setNext(m_front);
temp->setValue(value);
m_front = temp;
m_size++;
}
bool LinkedList::removeBack() {
if(isEmpty()) {
return(false);
}
else {
Node* temp;
Node* temp2;
temp = m_front;
temp2 = temp;
while(temp->getNext() != nullptr) {
temp = temp->getNext();
if(temp2->getNext()->getNext() == nullptr) {
temp2->setNext(nullptr);
}
temp2 = temp;
}
delete temp;
m_size--;
if(isEmpty()) {
m_front = nullptr;
}
}
return(true);
}
bool LinkedList::removeFront() {
if(isEmpty()) {
return(false);
}
Node* temp;
temp = m_front;
m_front = m_front->getNext();
delete temp;
m_size--;
return(true);
}
void LinkedList::printMenu()
{
std::cout << "\n\nSelect from the following menu:\n"
<< "1) Add to the front of the list\n"
<< "2) Add to the end of the list\n"
<< "3) Remove from front of the list\n"
<< "4) Remove from back of the list\n"
<< "5) Print the list\n"
<< "6) Search for value\n"
<< "7) Exit\n"
<< "Enter your choice: ";
}