Answered step by step
Verified Expert Solution
Question
1 Approved Answer
new.h #ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #include #include using namespace std; template class ListNode { public: T value; ListNode *next; ListNode(T v, ListNode *n = NULL)
new.h
#ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED #includeModifying new class. Given the header file new hfor the template class new". Add a method to the class for reversing the elements on the list, noting the following Plan the implementation first do not just jump into the program Do not delete nodes or create new ones use the existing ones. Do not modify the values in the nodes just modify the pointers instead. Make sure the head and tail pointers and count are correctly updated. Make sure your implementation works for empty lists, as well as those with only one node. Then show a test program that inserts integers into your list, displays it, reverses it, and displays the result ustrate the ability to reverse an empty list, reverse a single node list, and reverse larger lists and display them#include using namespace std; template class ListNode { public: T value; ListNode *next; ListNode(T v, ListNode *n = NULL) { value = v; next = n; } }; template class List { private: ListNode *head, *tail; int listSize; public: List() { head = tail = NULL; listSize = 0; } List(const List &other); const List & operator=(const List & other); ~List(); bool insertAtFront(T); bool insertSecond(T); bool insertAtEnd(T); bool deleteFromFront(); T getFront() const; bool isEmpty() const { return listSize == 0; } int getSize() const { return listSize; } void display(ostream &) const; }; template class Stack: public List { public: bool push(T value) { return List ::insertAtFront(value); } bool pop() { return List ::deleteFromFront(); } T top() const { return List ::getFront(); } }; template class Queue : public List { public: bool enqueue(T v) { return insertAtEnd(v); } bool dequeue() { return List ::deleteFromFront(); } T front() { return List ::getFront(); } }; template List ::List(const List &other) { listSize = other.listSize; if (listSize == 0) { head = tail = NULL; } else { head = tail = new ListNode (other.head->value, NULL); ListNode *temp = other.head->next; while (temp != NULL) { tail->next = new ListNode (temp->value, NULL); tail = tail->next; temp = temp->next; } } } template const List & List ::operator=(const List &other) { while (head != NULL) { tail = head; head = head->next; delete tail; } listSize = other.listSize; if (listSize == 0) { head = tail = NULL; } else { head = tail = new ListNode (other.head->value, NULL); ListNode *temp = other.head->next; while (temp != NULL) { tail->next = new ListNode (temp->value, NULL); tail = tail->next; temp = temp->next; } } return other; } template List ::~List() { while (head != NULL) { tail = head; head = head->next; delete tail; } } template bool List ::insertAtFront(T v) { ListNode *temp = new ListNode (v, head); if (temp == NULL) return false; head = temp; if (tail == NULL) tail = head; listSize++; return true; } template bool List ::insertSecond(T v) { if (head == NULL) return false; ListNode *temp = new ListNode (v, head->next); if (temp == NULL) return false; head->next = temp; if (tail == head) tail = temp; listSize++; return true; } template bool List ::insertAtEnd(T v) { ListNode *temp = new ListNode (v, NULL); if (head == NULL) head = temp; else tail->next = temp; tail = temp; listSize++; return true; } template bool List ::deleteFromFront() { ListNode *temp = head; if (head == NULL) return false; if (tail == head) tail = NULL; head = head->next; delete temp; listSize--; return true; } template T List ::getFront() const { if (head == NULL) { cout value; } template void List ::display(ostream &os) const { os "; ListNode *temp = head; while (temp != NULL) { os value "; temp = temp->next; } os ostream & operator &theList) { theList.display(os); return os; } #endif // LIST_H_INCLUDED
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