template <typename T> void DoublyLinkedList<T>::deleteAtIndex(const unsigned int
ID: 3903487 • Letter: T
Question
template <typename T>
void DoublyLinkedList<T>::deleteAtIndex(const unsigned int index)
{
if (front == nullptr)
{
cout << "the list is empty" << endl;
return;
}
if (index > count)
{
cout << "index is out of bounds" << endl;
return;
}
if (index == 0)
{
shared_ptr<Node<T>> temp(front);
front = front->forward;
delete temp;
if (count == 0)
{
front = nullptr;
back = nullptr;
count--;
return;
}
count--;
return;
}
if (index == count-1)
{
shared_ptr<Node<T>> temp(nullptr);
shared_ptr<Node<T>> cur(front);
int tracker = 0;
while (tracker < index)
{
temp = cur;
cur = cur->forward;
tracker++;
}
delete cur;
back = temp;
back->backward = nullptr;
count--;
return;
}
shared_ptr<Node<T>> temp(nullptr);
shared_ptr<Node<T>> cur(front);
int tracker = 0;
while (tracker < index)
{
temp = cur;
cur = cur->forward;
tracker++;
}
temp->forward = cur->forward;
delete cur;
count--;
}
delete icrp frontullptri back-ackward nullptrExplanation / Answer
Call the destructor method. See https://msdn.microsoft.com/en-us/library/bb982026.aspx for more details
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.