implement a template class of a singly linked list.
1.Define a template class Node that consist two data members: A template data and a Node pointer next. You may define the necessary member functions for the template class.
2.Define a template class MyList in the file MyList.h. The template class consist two data members: A Node pointer head and a Node pointer tail. The pointer head points to the first node of a linked list. The pointer tail points to the last node of the linked list. Initially the linked list is empty. Define the following member functions for the class MyList
1.Default constructor.
2.Copy constructor.
3.Destructor.
4.push_front function takes a template data as a parameter and adds it at the front of a linked list (in front of the first node if it is not empty).
5.push_back function takes a template data as a parameter and adds it at the tail of a linked list (after the last node if it is not empty).
6.isEmpty function returns true if a linked list is empty.
7.front function returns the data in the first node of a linked list.
8.back function returns the data in the last node of a linked list.
9.pop_front function removes the first node in a linked list. It does not return any data.
10.pop_back function removes the last node in a linked list. It does not return any data.
11.Define nested class iterator in the class MyList which consist data members: A Node pointer current and a Node pointer last. Define the following member functions and overloading operators for the iterator class.
11.1 Default constructor.
11.2 Dereference operator * returns the current node’s data.
11.3 Post increment operator ++ moves the current pointer points to the next node in the list.
11.4 Overloading comparison operator “not equals” compares the iterator with another iterator. Returns true if both iterators’ current pointers are not the same.
12. begin function returns an iterator with pointer current points to the first node of the list, and pointer last points to the tail of the list.
13. end function returns an iterator with pointer current set to NULL and pointer last points to the tail of the list.
14. print function takes a std::ostream reference parameter and prints all data stored in the linked list to the ostream by using the iterator define above.
Implement member functions for the template classes Node, MyList and iterator in the file MyList.h
template<class T>
class List
{
public:
class Iterator;
class ConstIterator;
List() : head(NULL), tail(NULL), size(0) {} ~List();
Iterator<T> begin();
Iterator<T> end();
void insert(const T& data);
void insert(const T& data, const Iterator<T>& iterator);
void remove(const Iterator<T>& iterator);
int getSize() const;
Iterator<T> find();
void sort();
private:
class Node<T>;
Node<T>* head;
Node<T>* tail;
int size;
};
template<class T>
class List<class T>::Iterator
{
public:
Iterator(); ~Iterator();
T& operator ++ ();
T operator ++ (int);
T& operator -- ();
T operator -- (int);
bool operator == (const Iterator<T>& iterator) const;
bool operator != (const Iterator<T>& iterator) const;
T& operator * ();
private:
List<T>* list;
Node<T>* node;
};
template<class T>
class List<class T>::ConstIterator
{
public:
ConstIterator(); ~ConstIterator();
T& operator ++ ();
T operator ++ (int);
T& operator -- ();
T operator -- (int);
bool operator == (const ConstIterator<T>& iterator) const;
bool operator != (const ConstIterator<T>& iterator) const;
T& operator * ();
private:
const List<T> * list;
const Node<T> * node;
};
template<class T>
class List<class T>::Node
{
public:
Node(const T& _data, const Node* _next = NULL) : data(_data), next(_next) {} ~Node();
private:
T data;
Node* next;
};
What I have tried:
Its my 50% code. kindly help me to make another 50%.