From 1431b3095b5098eab22e852cc0bc5fcee8c0b8f4 Mon Sep 17 00:00:00 2001 From: Logan Date: Thu, 13 Jun 2024 23:29:45 -0700 Subject: Added the functions and unit tests --- CST 126/Homework 3/Homework 3.vcxproj | 37 +++-- CST 126/Homework 3/Homework 3.vcxproj.filters | 5 + CST 126/Homework 3/main.cpp | 193 ++++++++++++++++++++++++++ 3 files changed, 215 insertions(+), 20 deletions(-) create mode 100644 CST 126/Homework 3/main.cpp diff --git a/CST 126/Homework 3/Homework 3.vcxproj b/CST 126/Homework 3/Homework 3.vcxproj index b1b2d80..4597b48 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj +++ b/CST 126/Homework 3/Homework 3.vcxproj @@ -17,7 +17,6 @@ Release x64 - 17.0 @@ -53,27 +52,24 @@ true Unicode - - + + + + + + + + + + + + + - - - - - - - - - - - - - - Level3 @@ -130,9 +126,10 @@ true - - + + + - + \ No newline at end of file diff --git a/CST 126/Homework 3/Homework 3.vcxproj.filters b/CST 126/Homework 3/Homework 3.vcxproj.filters index a8a6563..ce0c35c 100644 --- a/CST 126/Homework 3/Homework 3.vcxproj.filters +++ b/CST 126/Homework 3/Homework 3.vcxproj.filters @@ -14,4 +14,9 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + Source Files + + \ No newline at end of file diff --git a/CST 126/Homework 3/main.cpp b/CST 126/Homework 3/main.cpp new file mode 100644 index 0000000..73ebc4b --- /dev/null +++ b/CST 126/Homework 3/main.cpp @@ -0,0 +1,193 @@ +#include + +template +struct Node { + T data; + Node* next; + + Node(T val) : data(val), next(nullptr) {} +}; + +template +class LinkedList { +private: + Node* head; + +public: + LinkedList() : head(nullptr) {} + + void append(T val) { + Node* newNode = new Node(val); + if (!head) { + head = newNode; + return; + } + Node* current = head; + while (current->next) { + current = current->next; + } + current->next = newNode; + } + + void prepend(T val) { + Node* newNode = new Node(val); + newNode->next = head; + head = newNode; + } + + void removeFirst() { + if (!head) return; + Node* temp = head; + head = head->next; + delete temp; + } + + void removeLast() { + if (!head) return; + if (!head->next) { + delete head; + head = nullptr; + return; + } + Node* current = head; + while (current->next->next) { + current = current->next; + } + delete current->next; + current->next = nullptr; + } + + void insertAfter(T key, T val) { + Node* current = head; + while (current) { + if (current->data == key) { + Node* newNode = new Node(val); + newNode->next = current->next; + current->next = newNode; + return; + } + current = current->next; + } + } + + void insertBefore(T key, T val) { + if (!head) return; + if (head->data == key) { + prepend(val); + return; + } + Node* current = head; + while (current->next) { + if (current->next->data == key) { + Node* newNode = new Node(val); + newNode->next = current->next; + current->next = newNode; + return; + } + current = current->next; + } + } + + void clear() { + while (head) { + Node* temp = head; + head = head->next; + delete temp; + } + } + + T extract() { + if (!head) { + std::cerr << "List is empty!" << std::endl; + exit(1); + } + T data = head->data; + removeFirst(); + return data; + } + + void printList() { + Node* current = head; + while (current) { + std::cout << current->data << " "; + current = current->next; + } + std::cout << std::endl; + } + + ~LinkedList() { + clear(); + } +}; + +void runTests() { + std::cout << "Rainy Day Tests:\n"; + LinkedList list; + + list.append(1); + list.append(2); + list.append(3); + std::cout << "Appended List: "; + list.printList(); + + list.prepend(0); + std::cout << "Prepended List: "; + list.printList(); + + list.removeFirst(); + std::cout << "After RemoveFirst: "; + list.printList(); + + list.removeLast(); + std::cout << "After RemoveLast: "; + list.printList(); + + list.insertAfter(2, 4); + std::cout << "After InsertAfter: "; + list.printList(); + + list.insertBefore(3, 5); + std::cout << "After InsertBefore: "; + list.printList(); + + list.clear(); + std::cout << "After Clear: "; + list.printList(); + + std::cout << "\nSunny Day Tests:\n"; + + list.append(10); + list.append(20); + list.append(30); + std::cout << "Appended List: "; + list.printList(); + + list.prepend(5); + std::cout << "Prepended List: "; + list.printList(); + + list.removeFirst(); + std::cout << "After RemoveFirst: "; + list.printList(); + + list.removeLast(); + std::cout << "After RemoveLast: "; + list.printList(); + + list.insertAfter(10, 15); + std::cout << "After InsertAfter: "; + list.printList(); + + list.insertBefore(30, 25); + std::cout << "After InsertBefore: "; + list.printList(); + + list.clear(); + std::cout << "After Clear: "; + list.printList(); +} + +int main() { + runTests(); + return 0; +} -- cgit v1.2.3