aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework 3/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CST 126/Homework 3/main.cpp')
-rw-r--r--CST 126/Homework 3/main.cpp193
1 files changed, 193 insertions, 0 deletions
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 <iostream>
+
+template <typename T>
+struct Node {
+ T data;
+ Node* next;
+
+ Node(T val) : data(val), next(nullptr) {}
+};
+
+template <typename T>
+class LinkedList {
+private:
+ Node<T>* head;
+
+public:
+ LinkedList() : head(nullptr) {}
+
+ void append(T val) {
+ Node<T>* newNode = new Node<T>(val);
+ if (!head) {
+ head = newNode;
+ return;
+ }
+ Node<T>* current = head;
+ while (current->next) {
+ current = current->next;
+ }
+ current->next = newNode;
+ }
+
+ void prepend(T val) {
+ Node<T>* newNode = new Node<T>(val);
+ newNode->next = head;
+ head = newNode;
+ }
+
+ void removeFirst() {
+ if (!head) return;
+ Node<T>* temp = head;
+ head = head->next;
+ delete temp;
+ }
+
+ void removeLast() {
+ if (!head) return;
+ if (!head->next) {
+ delete head;
+ head = nullptr;
+ return;
+ }
+ Node<T>* current = head;
+ while (current->next->next) {
+ current = current->next;
+ }
+ delete current->next;
+ current->next = nullptr;
+ }
+
+ void insertAfter(T key, T val) {
+ Node<T>* current = head;
+ while (current) {
+ if (current->data == key) {
+ Node<T>* newNode = new Node<T>(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<T>* current = head;
+ while (current->next) {
+ if (current->next->data == key) {
+ Node<T>* newNode = new Node<T>(val);
+ newNode->next = current->next;
+ current->next = newNode;
+ return;
+ }
+ current = current->next;
+ }
+ }
+
+ void clear() {
+ while (head) {
+ Node<T>* 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<T>* 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<int> 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;
+}