Use C The ADT UnsortedType List function DeleteItemItemType

Use C++

The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying to delete a word or key that is not in the list.

Example: Delete the word “letter” from the unsorted list.

Here are the lists contents before the DeleteItem(ItemType item) call:

super     formula travel     free        thick      Josephine            Clara      education

The question is ‘how can I exit gracefully from the DeleteItem(itemType Item) call when the word or key is not in the unsorted list and not get an endless loop but instead printing out a message that the word ‘letter’ is not in the loop? What code can be added?

void UnsortedType::DeleteItem(ItemType item)

{

NodeType* location;

NodeType* tempLocation;

location = listData;

if (item.ComparedTo(location->info) == EQUAL)

{

    tempLocation = location;

    listData = listData->next;

}

else

{

    while (!((item.ComparedTo((location->next)->info) == EQUAL)))

      location = location->next;

    tempLocation = location->next;

    location->next = (location->next)->next;

}

delete tempLocation;

length--;

}

Solution

search and delete with respect to location as current, head and tail
//define below 2 lines in LinkedList() function


   head = new NodeType<ItemType>;
   tail = new NodeType<ItemType>;

  


void UnorderedType<ItemType>::DeleteItem(ItemType item)
{
   NodeType<ItemType> *tempLocation, *location;
   bool stop = false;

   if(!isEmpty())
   {
       location = head;
       tempLocation = head->link;
       while (templocation != tail && !stop)
       {
           if (templocation->info == item)
               stop = true;
           else
           {
               location = templocation;
               templocation = templocation->link;
           }
       }
       if (!stop)
           cout << \"The node to delete is not in the list!\" << endl;
       else
       {
           location->link = templocation->link;
           delete templocation;
           count--;
       }
   }
   else
   {
       cout << \"The list is empty!\" << endl;
   }
}

Use C++ The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying to delete a word or key that is not in the list
Use C++ The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying to delete a word or key that is not in the list

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site