Question: C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to

C++

Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1, 4, 7 } (enclosing braces and comma + space between values). Finally, you'll implement insertAtPosition and removeAtPosition, each of which takes two parameters: the first of type T and the second of type int and inserts or removes a value equal to the first parameter at the position equal to the second parameter. Legal values for the second parameter are from 1 to the list length + 1 for insert and from 1 to the list length for the remove. Out of range values have no effect. BTW positions start with 1 unlike array indexes which start with 0.

#ifndef LINKEDLIST_H_INCLUDED #define LINKEDLIST_H_INCLUDED #include #include #include

using namespace std; template struct Node { T data; Node *Next, *Previous; };

template class Linkedlist { private: Node *First = NULL; Node *Last = NULL; Node *Middle = NULL; Node *TempPrevious; Node *TempNext; Node *P; int length;

public: Linkedlist() { First = NULL; Last = NULL; length = 0; }

void insertfirst(const T & data) T getfirst()const; T getlast()const; void setfirst(const T &d); void setlast(const T &d); void insertlast(const T & d); void removefirst(); //no parameters void removelast(); int getlength(const); bool isEmpty()const; void clear(); void insertatposition( const T &d, int position);

};

#endif // LINKEDLIST_H_INCLUDED

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!