-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_1.cpp
172 lines (148 loc) · 4.54 KB
/
project_1.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
#include <iostream>
#include <string>
#include <sstream>
#include "LinkedList.h"
using namespace std;
void TestRemove();
void TestRemoveHeadTail();
void TestOtherRemoval();
void TestRecursion();
bool TestClear();
int main_main()
{
int testNum;
cin >> testNum;
if (testNum == 1)
TestRemove();
else if (testNum == 2)
TestRemoveHeadTail();
else if (testNum == 3)
TestOtherRemoval();
else if (testNum == 4)
TestRecursion();
else if (testNum == 5)
if(TestClear()){
cout << "The Clear() function works properly" << endl;
}
return 0;
}
void TestRemove()
{
cout << "=====Testing Remove() functionality=====" << endl;
LinkedList<string> data;
string test = "Test RemoveMe to RemoveMe find RemoveMe all RemoveMe matching ";
test.append("RemoveMe nodes RemoveMe completed RemoveMe with RemoveMe no ");
test.append("RemoveMe \"RemoveMe\" RemoveMe nodes RemoveMe remaining.");
stringstream x(test);
string tmp;
while (getline(x, tmp, ' '))
data.AddTail(tmp);
cout << "Initial list: " << endl;
data.PrintForward();
string val = "RemoveMe";
int count = data.Remove(val);
cout << "\nRemoving " << val << " from the list." << endl;
cout << "Removed " << count << " nodes from the list.\n" << endl;
data.PrintForward();
cout << "\nNodes removed: " << count << endl;
count = data.Remove(val);
cout << "Removing " << val << " from the list again." << endl;
cout << "Nodes removed: " << count << endl;
}
void TestRemoveHeadTail()
{
cout << "=====Testing RemoveHead()/RemoveTail() functionality=====" << endl;
LinkedList<int> data;
for (unsigned int i = 0; i < 70; i += 5)
data.AddTail(i);
cout << "Initial list: " << endl;
data.PrintForward();
cout << "Removing 2 Tail and 2 Head Nodes..." << endl;
data.RemoveHead();
data.RemoveTail();
data.RemoveHead();
data.RemoveTail();
data.PrintForward();
}
void TestOtherRemoval()
{
cout << "=====Testing RemoveAt() and clearing with RemoveHead()/RemoveTail() functionality=====" << endl;
LinkedList<string> data;
data.AddTail("Batman");
data.AddTail("RemoveMe");
data.AddTail("Superman");
data.AddTail("RemoveMe");
data.AddTail("Wonder Woman");
data.AddTail("RemoveMe");
data.AddTail("The Flash");
cout << "Initial list: " << endl;
data.PrintForward();
cout << "\nRemoving using RemoveAt()..." << endl;
data.RemoveAt(1);
data.RemoveAt(2);
data.RemoveAt(3);
data.PrintForward();
cout << "\nAttempting to remove out of range using RemoveAt()..." << endl;
if (!data.RemoveAt(100))
cout << "Attempt to RemoveAt(100) failed." << endl;
else
cout << "Successfully removed node 100? Weird, there are only 4 nodes..." << endl;
cout << "\nClearing list using RemoveHead()..." << endl;
while (data.RemoveHead()){}
if (data.NodeCount() == 0)
cout << "List is empty!" << endl;
else
cout << "List not empty!" << endl;
cout << "Adding additional nodes..." << endl;
data.AddTail("Robin");
data.AddTail("Batgirl");
data.AddTail("Nightwing");
data.AddTail("Red Hood");
data.AddTail("Bluebird");
data.PrintForward();
cout << "Clearing list using RemoveTail()..." << endl;
while (data.RemoveTail()) {}
if (data.NodeCount() == 0)
cout << "List is empty!" << endl;
else
cout << "List not empty!" << endl;
}
void TestRecursion()
{
LinkedList<int> power2;
int val = 2;
for (int i = 0; i < 10; i++)
{
power2.AddTail(val);
val *= 2;
}
cout << "Initial list: " << endl;
power2.PrintForward();
cout << "Printing recursively forward from 64: " << endl;
LinkedList<int>::Node *node = power2.Find(64);
power2.PrintForwardRecursive(node);
cout << "Printing recursively in reverse from 512: " << endl;
node = power2.Find(512);
power2.PrintReverseRecursive(node);
}
bool TestClear(){
LinkedList<float> data;
data.AddHead(1.21f);
data.AddHead(3.1f);
data.AddHead(4.7f);
data.AddHead(12.23f);
data.AddHead(7.78f);
int nodeCountBefore = data.NodeCount();
if (nodeCountBefore == 0)
{
cout << "Data not properly added to lists, no data present to test clearing.";
return false;
}
data.Clear();
if (data.Head() != nullptr || data.Tail() != nullptr || data.NodeCount() != 0)
{
cout << "List not cleared properly. Did you forget a variable?";
return false;
}
return true;
}