#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; }