Question
Modify this C++ program LinkedList class to make it a template class: Node.h: #pragma once class Node { private: int data; Node *next; public: Node();
Modify this C++ program LinkedList class to make it a template class:
Node.h:
#pragma once
class Node { private: int data; Node *next;
public: Node(); Node(int); Node(int, Node*); int getData(); void setData(int); Node *getNext(); void setNext(Node*);
};
Node.cpp
#include "Node.h"
Node::Node() { data = 0; next = nullptr; }
Node::Node(int newData) { data = newData; next = nullptr; }
Node::Node(int newData, Node* nextNode) { data = newData; next = nextNode; }
int Node::getData() { return data; }
void Node::setData(int newData) { data = newData; }
Node* Node::getNext() { return next; }
void Node::setNext(Node* nextNode) { next = nextNode; }
LinkedList.h
#pragma once #include "Node.h"
class LinkedList { private: Node* first; int manyNodes;
public: LinkedList(); bool isEmpty(); void addToBeginning(int); void addToEnd(int); void removeFromBeginning(); void removeFromEnd(); void removeDuplicates(); Node* find(int); void display(); };
LinkedList.cpp
#include "LinkedList.h" #include
LinkedList::LinkedList() { first = new Node(); manyNodes = 0; }
bool LinkedList::isEmpty() { return first->getNext() == nullptr; }
void LinkedList::addToBeginning(int newData) { Node *newNode = new Node(newData); newNode->setNext(first->getNext()); first->setNext(newNode); }
void LinkedList::addToEnd(int newData) { Node* temp = first;
while (temp->getNext() != nullptr) temp = temp->getNext();
temp->setNext(new Node(newData)); }
void LinkedList::removeFromBeginning() { if (!isEmpty()) { Node* temp = first->getNext(); first->setNext(temp->getNext()); delete temp; } }
void LinkedList::removeFromEnd() { Node *trailer = first; Node *temp = first->getNext();
if (!isEmpty()) { while (temp->getNext() != nullptr) { trailer = temp; temp = temp->getNext(); }
trailer->setNext(nullptr); delete temp; } }
void LinkedList::removeDuplicates() { Node *mainPtr = first->getNext();
while (mainPtr != nullptr) { int val = mainPtr->getData(); Node *trailer = mainPtr; Node *temp = mainPtr->getNext();
while (temp != nullptr) { if (temp->getData() == val) { trailer->setNext(temp->getNext()); temp->setNext(nullptr); delete temp; temp = trailer->getNext(); } else { trailer = temp; temp = temp->getNext(); } } mainPtr = mainPtr->getNext(); } }
Node* LinkedList::find(int key) { Node* temp = first->getNext(); while (temp != nullptr) { if (temp->getData() == key) return temp;
temp = temp->getNext(); }
return nullptr; }
void LinkedList::display() { Node* temp = first->getNext(); while (temp != nullptr) { cout << temp->getData() << " "; temp = temp->getNext(); } }
Then write a test program that creates a list of different data types such as int, doubles, char showing that they properly work.
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