All functions should belong to a template class Functions code should be efficie
ID: 3554174 • Letter: A
Question
All functions should belong to a template class
Functions code should be efficient as well as correct.
Class identifier: node, info,next,back,head.
For regular lists, the head points to the first node and it is the only pointer in the list.
For circluar lists-doubly or singly- the head points to last element.
Do not assume the list has nodes and do not assume a client called serchItem before deleting, ifFull before inserting, isEmpty before dleting.......
5) Write a function to be included in an unsorted doubly linked list class, called deleteLast, that will delete the last node in the list.
-------------Rewrite all the function above using a circular sorted doubly linked list calss------------
Explanation / Answer
template<class T>
void unsorted_doubly_linked_list<T>::deleteLast()
{
if(isEmpty()) cout<<"Empty list. No last element to delete"<<endl;
else if(head->next == NULL) {delete head; head = NULL;}
else
{
node<T> *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->back->next = NULL; //Making the last before element to point to Null
delete temp;
}
template<class T>
void unsorted_circular_doubly_linked_list<T>::deleteLast()
{
if(isEmpty()) cout<<"Empty list. No last element to delete"<<endl;
else if(head->next == head) {delete head; head = NULL;}
else
{
node<T> *temp = head; //Since head points to the last element
temp->back->next = head->next; // Linking the next pointer of the last before element to the first element
head->next->back = temp->back; // Linking the back pointer of the first element to the last before element
delete temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.