Question

In: Computer Science

Use C++ The ADT UnsortedType List function ‘DeleteItem(ItemType item)’ creates an endless loop error when trying...

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--;

}

Solutions

Expert 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;
   }
}


Related Solutions

in c++ >>When is beneficial to use entry condition loop (for loop) and when is it...
in c++ >>When is beneficial to use entry condition loop (for loop) and when is it beneficial to use exit condition loop (while loop). Give an example for each loops.
Create an ADT class that creates a friend contact list. The program should be able to...
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
C# language. Answer in a discussion format. What is a loop? When do you use a...
C# language. Answer in a discussion format. What is a loop? When do you use a loop versus a selection statement? What are some examples when an infinite loop is appropriate? Describe one of the following three types of loops supported in C#: while loop for loop do loop (or do-while loop) Describe the steps necessary to make a while loop end correctly: Explain the difference between incrementing and decrementing loop control variables. Explain the benefits of using both pretest...
For a closed- loop leveling measurement, if the misclosure error C is 9 mm, the travel...
For a closed- loop leveling measurement, if the misclosure error C is 9 mm, the travel distance ( the perimeter of the leveling loop) K is 2000 meters, please determine the order of leveling accuracy?
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT