Question
Hey, I need a Linked List program written in C++ for my main function. Here's main.cpp. I need LinkedList.h. Any help is appreciated!!: #include LinkedList.h
Hey, I need a Linked List program written in C++ for my main function.
Here's main.cpp. I need LinkedList.h. Any help is appreciated!!:
#include "LinkedList.h" #include
void TestAddHead(); void TestAddTail(); void TestBrackets(); void TestRemove(); void TestRemoveHeadTail(); void TestOtherRemoval(); void TestAddingArrays(); void TestFindAll(); void TestFind(); void TestInsertAt();
int main() { int testNum; cin >> testNum; if (testNum == 1) TestAddHead(); else if (testNum == 2) TestAddTail(); else if (testNum == 3) TestBrackets(); else if (testNum == 4) TestRemove(); else if (testNum == 5) TestRemoveHeadTail(); else if (testNum == 6) TestOtherRemoval(); else if (testNum == 7) TestAddingArrays(); else if (testNum == 8) TestFindAll(); else if (testNum == 9) TestFind(); else if (testNum == 10) TestInsertAt();
return 0; }
/*======== TESTING FUNCTIONS ========*/ void TestAddHead() { cout << "=====Testing AddHead() functionality====" << endl; LinkedList
void TestAddTail() { cout << "=====Testing AddTail() functionality====" << endl; LinkedList
void TestBrackets() { cout << "=====Testing operator[] to access value of nodes=====" << endl; LinkedList
const int value = list[0]; cout << "Value of node[0]: " << val << " Expected value: " << value << endl; const int value2 = list[1]; cout << "Value of node[1]: " << val2 << " Expected value: " << value2 << endl; int value3 = list[2]; cout << "Value of node[2]: " << val3 << " Expected value: " << value3 << endl;
cout << "Using brackets operator in a loop to change node values..." << endl; for (unsigned int i = 0; i < list.NodeCount(); i++) list[i] = i;
list.PrintForward(); }
void TestRemove() { cout << "=====Testing Remove() functionality=====" << endl; LinkedList
cout << "Initial list: " << endl; data.PrintForward(); string val = "RemoveMe"; int count = data.Remove(val); cout << " Removing " << val << " from the list." << endl; cout << "Removed " << count << " nodes from the list. " << endl; data.PrintForward(); cout << " Nodes 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
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
cout << "Initial list: " << endl; data.PrintForward(); cout << " Removing using RemoveAt()..." << endl; data.RemoveAt(1); data.RemoveAt(2); data.RemoveAt(3);
data.PrintForward(); cout << " Attempting 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 << " Clearing 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 TestAddingArrays() { cout << "=====Testing AddNodesHead() and AddNodesTail() =====" << endl;
string values[5]; values[0] = "*"; values[1] = "**"; values[2] = "***"; values[3] = "****"; values[4] = "*****";
LinkedList
list.AddNodesHead(values, 5); list.AddNodesTail(values, 5); list.PrintForward(); }
void TestFindAll() { cout << "=====Testing FindAll() functionality=====" << endl; LinkedList
cout << "Initial list: " << endl; data.PrintForward();
vector
cout << " Nodes found: " << nodes.size() << endl; for (unsigned int i = 0; i < nodes.size(); i++) { cout << "Node #" << (i + 1) << ": "; cout << "Value: " << nodes[i]->data << endl; cout << "Prev value: "; if (nodes[i]->prev != nullptr) cout << nodes[i]->prev->data; else cout << "nullptr";
cout << " Next value: "; if (nodes[i]->next != nullptr) cout << nodes[i]->next->data; else cout << "nullptr"; cout << endl; } }
void TestFind() { cout << "=====Testing Find()/GetNode() and InsertBefore()/InsertAfter() functionality=====" << endl; LinkedList
LinkedList
if (node != nullptr) { cout << "Node found! Value: " << node->data << endl; cout << "Prev value: " << node->prev->data << endl; cout << "Next value: " << node->next->data << endl; } else cout << "Error! Returned node was a nullptr.";
cout << " Inserting 2048 before node and 4096 after node." << endl; data.InsertBefore(node, 2048); data.InsertAfter(node, 4096); data.PrintForward();
cout << " Using GetNode() to get the 5th node in the list..." << endl;
node = data.GetNode(4); if (node != nullptr) { cout << "Node found! Value: " << node->data << endl; cout << "Prev value: " << node->prev->data << endl; cout << "Next value: " << node->next->data << endl; } else cout << "Error! Returned node was a nullptr.";
cout << " Searching for node with value of 200..." << endl; node = data.Find(200); if (node == nullptr) cout << "Node not found, nullptr returned." << endl; }
void TestInsertAt() { cout << "=====Testing InsertAt() functionality=====" << endl; LinkedList
cout << "Initial list: " << endl; data.PrintForward(); cout << "Node count: " << data.NodeCount() << endl;
cout << " Inserting words into the list with InsertAt()..." << endl;
data.InsertAt("One", 0); data.InsertAt("of", 3); data.InsertAt("lists", 5); data.InsertAt("insert", 10); data.InsertAt("nodes", 11); data.InsertAt("list.", 15);
data.PrintForward(); cout << "Node count: " << data.NodeCount() << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started