template <class T> class LinkedList { protected: struct Node { int data; T value
ID: 3539141 • Letter: T
Question
template <class T>
class LinkedList
{
protected:
struct Node
{
int data;
T value;
Node<T>*next;
Node<t>*prev;
Node( T value1, Node *next = NULL)
{
value = value1;
next = next1;
}
};
Node<T> *head;// Head Pointer
Node<T> *tail; // Tail Pointer
private:
static Node* copyList( Node *alist); // Creates a new List
static void destroyedList(Node *alist); // Destroys a list by deallcating its nodes
public:
int data;
LinkedList() { head = NULL;} // Default Constructor
LinkedList(const Node &rec) // Copy Constructor
{
head = copyList( rec.head); // Sets head to a new LinkedList
}
LinkedList(const Node & otherList)
{
Node<T>* node = otherList.head;
Node<T>** tail = &head;
while (node)
{
*tail = new Node<T>(node->value);
tail = &tail->next;
node = node->next;
}
}
LinkedList& operator=(const Node &org) // Operator
{
destroyedList(head);
head = copyList(org.head);
}
~LinkedList(); // Destructor
static Node *insert( T value);// insert function
static Node *remove( T value); // remove function
void display();// Displays the list
;
};
/*
If the list is empty then we return NULL, but if not the function creates a copy of the
head node, and attches it to a created copy of the tail and returns the list.
*/
template<class T>
LinkedList::copyList(Node *alist)
{
if (alist == NULL)
return NULL;
else
{
Node *tailCopy = copyList(alist->next);
return new Node(alist->data,tailCopy);
}
}
/*
The destroyList functions destroys the tail and hen deallocates the data of the head node.
*/
template<class T>
Node::destroyedList(Node *alist)
{
if(alist !=NULL)
{
Node *tail = alist->next;
delete alist;
destroyList(tail);
}
}
/*
Inserts a node into the LinkedList
*/
template<class T>
void LinkedList<T>::insert(T value)
{
Node *nodePtr, *previousNodePtr;
if( head == NULL || head -> data >=value)
{
head = new Node(value,head)
else
while(nodePtr !=NULL && nodePtr ->value < data)
{
Node *tmp = new Node(value);
tmp->prev = head;
tmp->next = tail->next;
tmp->prev->next = tmp;
tmp->next->prev = tmp;
head = tmp;
}
previousNodePtr ->next = new Node(value,nodePtr);
}
}
/*
Removes a given node from the LinkedList.
*/
template<class T>
void LinkedList<T>::remove(T value)
{
Node *nodePtr, *previousNodePtr;
if(!head) return;
if( head -> data == value)
{
nodePtr = head;
head =head->next;
delete nodePtr;
}
else
{
nodePtr = head;
while( nodePtr != NULL && nodePtr->value != data)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr)
{
previousNodePtr-> = nodePtr -> next;
delete nodePtr;
}
}
}
//Displays the Linked List
template<class T>
void LinkedList<T>::display()
{
Node *nodePtr = head;
while(nodePtr)
{
cout<< nodePtr-> value << " ";
nodePtr = nodePtr->next;
}
}
/*
Deallocates the memory used by the list
*/
template<class T>
LinkedList<T>::~LinkedList()
{
Node *nodePtr = head;
while(nodePtr != NULL)
{
Node *trash = nodePtr;
nodePtr = nodePtr->next;
delete trash;
}
}
Explanation / Answer
Hey! I can help you out in this programming question and others if you just add me on gmail...my gmail id is hassanakbar125@gmAIL.COM.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.