-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
236 lines (202 loc) · 5.55 KB
/
main.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// This program is a demonstration of what was learned in my progrmming course
// DataStructures and Algorithms @ Borough of Manhattan Community College &
// Pace University
// This program's usage is to create a simple Data Structure known as a linked list
// that will allow users to allocate memory (using pointers) to create an efficient
// way of storing DATA using minimum CPU processing, minimum RAM usage and minimum Storage usage.
#include <iostream>
using namespace std;
// template<class T> is used to create an abstract data-type(ADT)
// this allows users to create a list with any data-type
// used is c++ or user defined classes
template <class T> struct Node {
T value;
Node<T> *next;
};
template <class t>
class Linklist {
public:
Node<t> *head;
public:
// default constructor
Linklist() { head = NULL; }
// Overloaded Constructor
// user defines initial value in the list upon creation
Linklist(t item) {
Node<t> *node = new Node<t>;
node->value = item;
head = node;
node->next = NULL;
}
// Destructor used to destroy
// the memory allocation of the list
~Linklist() {
while (head != NULL) {
Node<t> *tmp = head;
head = head->next;
delete tmp;
}
} // linklist destructor end
// size member function gets the size of the list
int size() const {
int count = 0;
Node<t> *tmp = head;
while (tmp != NULL) {
count++;
tmp = tmp->next;
}
return count;
} // size function end
// if the head of the list is empty, return true
bool isEmpty() const { return head == NULL; }
// first node is the head of the list
Node<t> *firstNode() const { return head; }
// Gives the last node in the linked list
Node<t> *lastNode() const {
if (head == NULL) {
return NULL;
}
// if the address of tmp->next is = to NULL then you have reached the end of
// the list therefore the Node tmp object would point to the last Node in
// the list
Node<t> *tmp = head;
while (tmp->next != NULL) {
tmp = tmp->next;
}
return tmp;
} // end of function lastNode
// Enter the position you would like to search for
// to get the node
Node<t> *getNode(int pos) const {
if (pos < 0 || head == NULL) {
return NULL;
}
int i = 0;
Node<t> *tmp = head;
while (tmp->next != NULL && i < pos) {
tmp = tmp->next;
i++;
}
return tmp;
} // end of getNode
// find the node in the list with the same value in the
// the argument
Node<t> *findNode(t val) const {
Node<t> *tmp = head;
while (tmp != NULL) {
if (tmp->value == val) {
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
// Get the value given the position
t getvalue(int pos) {
Node<t> *node = getNode(pos);
if (node != NULL) {
return node->value;
} else {
cout << "Value does not exist" << endl;
}
} // end getvalue
// Sets the value given the position
void setvalue(int pos, t val) {
Node<t> *node = getNode(pos);
if (node != NULL) {
node->value = val;
} else {
cout << "position does not exist" << endl;
}
}
// inserts the value given the position
void insert(int pos, t val) {
Node<t> *node = new Node<t>;
node->value = val;
if (pos < 0 || head == NULL) {
node->next = head;
head = node;
}
int i = 0;
Node<t> *tmp = head;
while (tmp->next != NULL && i < pos - 1) {
tmp = tmp->next;
i++;
}
node->next = tmp->next;
tmp->next = node;
cout << "node inserted" << endl;
} // end insert
// Creates and pushes a list item to the last position
// of the list
void push(t val){
this->insert(this->size(),val);
}
// Removes the whole node given the position
t remove(int pos) {
if (pos < 0 || head == NULL) {
cout << "position does not exist" << endl;
}
if (pos == 0) {
Node<t> *tmp = head;
head = head->next;
t value = tmp->value;
delete tmp;
return value;
}
int i = 0;
Node<t> *tmp = head;
while (tmp->next != NULL && i < (pos - 1)) {
tmp = tmp->next;
i++;
}
if (tmp != NULL) {
Node<t> *node = tmp->next;
if (node == NULL) {
cout << "end of list" << endl;
} else {
tmp->next = node->next;
t value = node->value;
delete node;
return value;
}
} else {
cout << "error";
return 0;
}
} // end remove
// Prints the contents of the list
void print() {
Node<t> *tmp = head;
while (tmp != NULL) {
cout << tmp->value << " ";
tmp = tmp->next;
}
}
}; // Linklist class end
int main() {
Linklist<string> list("I");
list.insert(1,"like");
list.insert(2, "vanilla");
list.insert(3, "ice");
list.insert(4, "cream");
list.push("and");
list.push("Coding");
cout << endl << "Here is the size of the string list: " << list.size() << endl;
cout << "string list contents: ";
list.print();
cout << endl;
list.setvalue(2, "Chocolate");
cout << "Removed 1 item string list new contents: ";
list.print();
cout << endl;
if (list.isEmpty())
{cout << endl << "True or False list is empty? true" << endl;}
else {cout<< endl << "True or False list is empty? false" << endl;}
cout << endl << "Get Value: "<< list.getvalue(2) << endl;
cout << "First node content: "<<list.firstNode()->value << endl;
cout << "Last node content: "<<list.lastNode()->value << endl;
cout << "Get node content: "<<list.getNode(4)->value << endl;
cout << "Find node address: "<<list.findNode("ice") << endl;
return 0;
}