Click here to Skip to main content
15,881,764 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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



C++
template<class T> 
class List 
{
    public:
        //Class declarations.
        class Iterator;
        class ConstIterator;

        //Constructors and Destructors.
        List() : head(NULL), tail(NULL), size(0) {} //Constructor
        ~List(); //Destructor

        //Methods
        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(); //Constructor
        ~Iterator(); //Destructor

        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(); //Constructor
        ~ConstIterator(); //Destructor

        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) {} //Constructor
        ~Node(); //Destructor 

    private:
        T data;
        Node* next;
};


What I have tried:

Its my 50% code. kindly help me to make another 50%.
Posted
Updated 14-Oct-19 8:46am
Comments
Sergey Alexandrovich Kryukov 25-Apr-16 0:14am    
No, sorry. "Help me to make another 50%" is not a question, this is a request to make 50% (but actually some 90-99.5%) of your job for you. You did not even point out any problems.
The only way to learn something through doing you assignments is doing it all by yourself.
However, if you have some questions on related topics, we will gladly answer.
—SA
Patrice T 25-Apr-16 2:48am    
Your HomeWork !

You should make your homework to learn coding. It will enhance your knowledge and skills and maybe you earn a lot of money someday.

Here is a fine tutorial about How to create Linked list using C/C++ to support your efforts.

;-)
 
Share this answer
 
Comments
CPallini 15-Oct-19 3:51am    
5.OOOOPS, a bit late. :-)
#include <iostream>
using namespace std;
template <typename t="">
struct node{
T data;
node* next;
};

class stack {
private :
node<t>* toptr;
node<t>* top;
public:
stack(){
toptr=NULL;
}
void push(int v)
{ node<t>* temp;
temp=new node<t>;
top->data* v;
temp->next=toptr;
top->data* v;
temp->next=toptr;
toptr=temp;
}
void pop() {
node<t>* temp=toptr;
toptr=toptr->next;
delete temp;
}
bool empty() const {
return toptr==NULL;
}
bool full() const {
node<t>* temp;
temp=new node<t>;
if(temp==NULL)
return true;
else
delete temp;
return false;
}
void make_empty() {
while (!empty)
{
pop();
}

}
~stack()
{
make_empty();
}
};
int main()
{
return 0;
}
 
Share this answer
 
Comments
Richard Deeming 14-Oct-19 14:52pm    
An unformatted, unexplained code-dump is not a solution to this already solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900