Implement the Linked List using head pointer only (you are not allowed to use tail pointer). Interface (abstract class) of LinkedList class is given below. Your task is to provide the complete implementation for this question (a child class having name myLL is required, this myLL class will provide the complete implementation of the LinkedList class)

39 0

Get full Expert solution in seconds

$1.97 ONLY

Unlock Answer

C++

Implement the Linked List using head pointer only (you are not allowed to use tail pointer). Interface (abstract class) of LinkedList class is given below. Your task is to provide the complete implementation for this question (a child class having name myLL is required, this myLL class will provide the complete implementation of the LinkedList class)
Interface:
template<class T>
class LinkedList
{
protected:
Node<T>* head;
public:
LinkedList();
virtual void insertAtEnd(T) = 0;
virtual T deleteFromHead() = 0;
virtual bool isEmpty() = 0;
virtual void display() = 0;
};

EXPERT ANSWER

#include <iostream>

using namespace std;

template<class T>
struct Node{
T data;
Node<T> *next;
};

template<class T>
class LinkedList
{
protected:
Node<T>* head;
public:
LinkedList() { }
virtual void insertAtEnd(T) = 0;
virtual T deleteFromHead() = 0;
virtual bool isEmpty() = 0;
virtual void display() = 0;
};

template <class T>
class myLL : public LinkedList<T> {
public:
myLL() {
LinkedList<T>::head = NULL;
}

void insertAtEnd(T d) {
Node<T>* newNode = new Node<T>;
newNode->data = d;
newNode->next = NULL;

if(LinkedList<T>::head == NULL) {
LinkedList<T>::head = newNode;
return;
}

Node<T>* curr = LinkedList<T>::head;

while(curr->next != NULL) {
curr = curr->next;
}

curr->next = newNode;
// curr = newNode;
}

T deleteFromHead() {
T tmp = LinkedList<T>::head->data;

Node<T>* temp = LinkedList<T>::head;

LinkedList<T>::head = LinkedList<T>::head->next;

delete temp;

return tmp;
}

bool isEmpty() {
return LinkedList<T>::head == NULL;
}

void display() {
Node<T>* curr = LinkedList<T>::head;

while(curr != NULL) {
cout << curr->data << ” “;
curr = curr->next;
}
}
};

int main() {
myLL<int> list;

list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(3);
list.insertAtEnd(4);
list.insertAtEnd(5);

cout << “Delete Node : ” << list.deleteFromHead() << endl;
list.display();

return 0;
}

Output Sample: