Zz L.

asked • 05/01/22

How to implement a template linked list in C++?

I need to implement a Template Linked List in C++ , and I'm asked to use three Fixed file(which is cannot changed):

ContainerIfc.h

class BADINDEX {};

template <class T>
class ContainerIfc {
public:
virtual ContainerIfc <T>& pushFront(T) =0;
virtual ContainerIfc <T>& pushBack(T) =0;
virtual ContainerIfc <T>& popFront(T&) =0; // throws BADINDEX
virtual ContainerIfc <T>& popBack(T&) =0; // throws BADINDEX
virtual int getSize() =0;
virtual bool isEmpty() =0;
virtual T front() =0; // throws BADINDEX
virtual T back() =0; // throws BADINDEX
virtual T& operator [](int) =0; // throws BADINDEX
virtual void erase() =0;
};

Node.h

template <class T>
class Node {
public:
T data;
Node<T> *next;

Node(T e) {
data = e;
next = NULL;
}
};

MyList.h

#include "ContainerIfc.h"
#include "Node.h"

template <class T>
class MyList : public ContainerIfc <T> {
public:
MyList();
~ MyList();
MyList(const MyList&);
MyList <T>& operator = (const MyList&);
MyList <T>& pushFront(T);
MyList <T>& pushBack(T);
MyList <T>& popFront(T&);
MyList <T>& popBack(T&);
int getSize();
bool isEmpty();
T front();
T back();
T& operator [](int);
void erase();

protected:
Node<T> *head;
};

I need to complete the experiment of all Functions in Mylist and I cannot change the content in ContainerIfc.h and Node.h, and I also need to write a main.cpp to test all functions in MyList.h. The main thing is that I can't make changes in Containerifc.h and Node.h, which makes me very uncomfortable and don't know how to do it.

1 Expert Answer

By:

Donald W. answered • 05/02/22

Tutor
5.0 (214)

Senior Software Engineer with over 25 years of industry experience

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.