-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedLists.cpp
More file actions
207 lines (173 loc) · 4.31 KB
/
linkedLists.cpp
File metadata and controls
207 lines (173 loc) · 4.31 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
206
207
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
class LinkedNode {
private:
public:
int intData;
LinkedNode *next;
};
class LinkedList {
private:
LinkedNode *head, *tail, *current;
int intSize;
public:
LinkedList() {
head = NULL;
tail = NULL;
current = NULL;
intSize = 0;
}
LinkedNode *front(void) { return head; }
LinkedNode *back(void) { return tail; }
int size(void) { return intSize; }
void push_front(LinkedNode *node) {
if (node != NULL) {
if (intSize == 0) { head = tail = node; }
else {
node->next = head;
head = node;
}
intSize++;
}
}
void push_back(LinkedNode *node) {
if (node != NULL) {
if (intSize == 0) { head = tail = node; }
else {
tail->next = node;
tail = node;
}
intSize++;
}
}
// Tnsert new node after index.
void push_index(LinkedNode *node, int intIndex) {
if ((node == NULL) || (intIndex < 0) || (intIndex >= intSize)) { return; }
else {
if (intSize == 0) {
push_front(node);
}
else {
current = at(intIndex);
node->next = current->next;
current->next = node;
intSize++;
}
}
}
// Note: pop'n nodes out will not delete the memory allocations.
// This only removes them from the linked list and returns the removed node address.
LinkedNode *pop_front (void) {
LinkedNode *nodeReturn = NULL;
if (intSize > 0) {
nodeReturn = head;
intSize--;
if (intSize == 0) { head = tail = current = NULL; }
else { head = head->next; }
}
return nodeReturn;
}
LinkedNode *pop_back (void) {
LinkedNode *nodeReturn = NULL;
if (intSize > 0) {
nodeReturn = tail;
intSize--;
if (intSize == 0) { head = tail = current = NULL; }
else {
tail = at(intSize - 1);
tail->next = NULL;
}
}
return nodeReturn;
}
LinkedNode *pop_index (int intIndex) {
LinkedNode *nodeReturn = NULL, *nodeParent = NULL;
if (intSize > 0) {
nodeReturn = at(intIndex);
intSize--;
if (intSize == 0) { head = tail = current = NULL; }
else {
if (intIndex == 0) {
head = head->next;
}
else {
nodeParent = at(intIndex - 1);
nodeParent->next = nodeReturn->next;
}
}
}
return nodeReturn;
}
// Swaps the two nodes.
void swap(int intIndexA, int intIndexB) {
LinkedNode *nodeA = NULL, *nodeB = NULL;
if ((intIndexA < 0) || (intIndexA >= intSize) || (intIndexB < 0) || (intIndexB >= intSize) || (intIndexA == intIndexB)) { return; }
else {
if (intIndexA == 0) {
nodeB = pop_index(intIndexB);
nodeA = pop_index(intIndexA);
push_front(nodeB);
push_index(nodeA, intIndexB - 1);
}
else {
nodeB = pop_index(intIndexB);
nodeA = pop_index(intIndexA);
push_index(nodeB, intIndexA - 1);
push_index(nodeA, intIndexB - 1);
}
}
}
// Just clears the whole linked list.
// Warning: No memory handling here, nodes are still out there.
void clear(void) {
intSize = 0;
head = tail = current = NULL;
}
LinkedNode *at (int intIndex) {
current = NULL;
if ((intIndex < 0) || (intIndex >= intSize) || (intSize == 0)) { return NULL; }
else {
current = head;
for (int intIteration = 0; intIteration < intIndex; intIteration++) {
current = current->next;
}
return current;
}
}
};
int main(void) {
LinkedNode *nodeNew = NULL;
LinkedNode *nodeHandler = NULL;
LinkedList linkList;
int intIndex = 0, intSize = 0;
srand((unsigned int)time(NULL));
for (intIndex = 0; intIndex < 10; intIndex++) {
nodeNew = new LinkedNode;
nodeNew->intData = rand() + 1;
linkList.push_back(nodeNew);
nodeNew = NULL;
}
cout << "Number of nodes in Linked List: " << linkList.size() << endl;
nodeHandler = linkList.front();
intSize = linkList.size();
for (intIndex = 0; intIndex < intSize; intIndex++) {
cout << "Node #[" << intIndex << "] Data[" << setw(6) << nodeHandler->intData << "]" << endl;
nodeHandler = nodeHandler->next;
}
nodeHandler = NULL;
// A little fun node swap
linkList.swap(1, 8);
// Need to delete out data before exiting.
intSize = linkList.size();
for (intIndex = 0; intIndex < intSize; intIndex++) {
nodeHandler = linkList.pop_front();
if (nodeHandler != NULL) {
delete nodeHandler;
nodeHandler = NULL;
}
}
return 0;
}