In: Computer Science
Write a member function named deleteItemAtIndex(). You will need to search till you get to the index'thed node in the list. But at that point you should remove the node (and don't forget to delete it, to free up its memory). If the asked for index does not exist, as usual you should thow a LinkedListItemNotFound exception. Here is the main and LinkedList cpp - https://paste.ofcode.org/37gZg5p7fiR243AD4sD5ttU
Any help is appreciated, I don't have much time left
template<class T>
bool LinkeList<T>::deleteItemAtIndex(const int& index)
{
Node<T>* itr = first;
//interator variable itr
Node<T>* temp;
//Temp to store node to be freed.
int count = 0;
try
// for
catching if we try to got null->next i.e length of list is <
index to be deleted.
{
if(index==0)
//special case when deleting first element to
assign first to its next element.
{
temp = first;
first = first->link;
delete temp;
return true;
}
else
{
while(count!=index-1)
// we will make itr variable point to the
previous index of the node to be deleted.
{
itr = itr
-> link;
count++;
}
temp =
itr->link;
// Now storing address of the node to be deleted
in temp.
itr->link =
itr->link->link; // detaching the link and making
it point to temp->link;
delete temp;
return true;
// return true if node is successfully
deleted.
}
catch(exception &E)
//If out of memory error occurs implies length
of list < index of the node to be deleted.
{
throw
LinkedListItemNotFound;
}
}