new.h Modifying new class. Given the header file new hfor the template class new
ID: 3812740 • Letter: N
Question
new.h
Modifying 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 themExplanation / Answer
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include <iostream>
#include <cstdlib>
using namespace std;
template <class T>
class ListNode
{
public:
T value;
ListNode *next;
ListNode(T v, ListNode *n = NULL) { value = v; next = n; }
};
template <class T>
class List
{
private:
ListNode<T> *head, *tail;
int listSize;
public:
List() { head = tail = NULL; listSize = 0; }
List(const List<T> &other);
const List<T> & operator=(const List<T>& 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;
void reverse(); // THIS CODE IS ADDED
};
template <class T>
class Stack: public List<T>
{
public:
bool push(T value) { return List<T>::insertAtFront(value); }
bool pop() { return List<T>::deleteFromFront(); }
T top() const { return List<T>::getFront(); }
};
template <class T>
class Queue : public List<T>
{
public:
bool enqueue(T v) { return insertAtEnd(v); }
bool dequeue() { return List<T>::deleteFromFront(); }
T front() { return List<T>::getFront(); }
};
//THIS CODE IS ADDED
template <class T>
void List<T>::reverse()
{
ListNode<T>* revHead;
//check if the regular list is empty
if(head == NULL)
return;
//else start reversing
ListNode<T>* current = head;
tail = head;
while(current != NULL)
{
if(revHead == NULL)
revHead = current;
else
{
ListNode<T>* tempHead = revHead;
current->next = tempHead;
revHead = current;
head = current;
}
current = current->next;
}//end while
}
template <class T>
List<T>::List(const List<T> &other)
{
listSize = other.listSize;
if (listSize == 0)
{
head = tail = NULL;
} else {
head = tail = new ListNode<T>(other.head->value, NULL);
ListNode<T> *temp = other.head->next;
while (temp != NULL)
{
tail->next = new ListNode<T>(temp->value, NULL);
tail = tail->next;
temp = temp->next;
}
}
}
template <class T>
const List<T> & List<T>::operator=(const List<T> &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<T>(other.head->value, NULL);
ListNode<T> *temp = other.head->next;
while (temp != NULL)
{
tail->next = new ListNode<T>(temp->value, NULL);
tail = tail->next;
temp = temp->next;
}
}
return other;
}
template <class T>
List<T>::~List()
{
while (head != NULL)
{
tail = head;
head = head->next;
delete tail;
}
}
template <class T>
bool List<T>::insertAtFront(T v)
{
ListNode<T> *temp = new ListNode<T>(v, head);
if (temp == NULL) return false;
head = temp;
if (tail == NULL) tail = head;
listSize++;
return true;
}
template <class T>
bool List<T>::insertSecond(T v)
{
if (head == NULL) return false;
ListNode<T> *temp = new ListNode<T>(v, head->next);
if (temp == NULL) return false;
head->next = temp;
if (tail == head) tail = temp;
listSize++;
return true;
}
template <class T>
bool List<T>::insertAtEnd(T v)
{
ListNode<T> *temp = new ListNode<T>(v, NULL);
if (head == NULL) head = temp;
else tail->next = temp;
tail = temp;
listSize++;
return true;
}
template <class T>
bool List<T>::deleteFromFront()
{
ListNode<T> *temp = head;
if (head == NULL) return false;
if (tail == head) tail = NULL;
head = head->next;
delete temp;
listSize--;
return true;
}
template <class T>
T List<T>::getFront() const
{
if (head == NULL) {
cout << endl << "Cannot get front of empty list!" << endl;
exit(0);
}
return head->value;
}
template <class T>
void List<T>::display(ostream &os) const
{
os << "Head -> ";
ListNode<T> *temp = head;
while (temp != NULL)
{
os << temp->value << " -> ";
temp = temp->next;
}
os << "NULL";
}
template <class T>
ostream & operator<<(ostream &os, List<T> &theList)
{
theList.display(os);
return os;
}
#endif // LIST_H_INCLUDED
int main(){
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.