Question

In: Computer Science

Write the deleteNode() member function, which is also given in our textbook implementation. This function takes...

Write the deleteNode() member function, which is also given in our textbook implementation. This function takes a const T& as its parameter, which is the value of an item to search for and delete. Thus the first part of this function is similar to the search() function, in that you first have to perform a search to second the item. But once found, the node containing the item should be removed from the list (and you should free the memory of the node). This function is only guaranteed to find the first instance of the item and delete it, if the item appears multiple times in the list, the ones after the first one will still be there after the first item is removed by this function. This function should return a LinkedListItemNotFoundException if the item asked for is not found. The LinkedListItemNotFoundException class has already been defined for you in the starting template header file.

Here is the cpp for main and LinkedList - https://paste.ofcode.org/37gZg5p7fiR243AD4sD5ttU

Solutions

Expert Solution

deletenode() function to be implemented:

template <class T>
void LinkedList<T>:: deleteNodet(const T& Item)
{

    /*Itr pointer will be used to iterate the whole linked list
     to find the element which is to be deleted.*/
   
   Node<T>* Itr = first;
  
      //If the first element is storing the value to be deleted.
    //Make first point to the second element in the Linked List
    // and free the memory.
    if (Itr != NULL && Itr->info == Item)
    {
        first = Itr->link;  
        free(Itr);             
        return;
    }

  
    /*This pointer will point to the element which will be present just before the
     element which is to be deleted.*/
   Node<T>* previous;

    //Iterate the whole linked list to find the element which is to be deleted.
    //Update previous pointer accordingly.
   while (Itr != NULL && Itr->info != Item)
    {
        previous = Itr;
        Itr = Itr->link;
    }

  
   // If element is not found in the whole linked list.
   if (Itr == NULL)
        throw(LinkedListItemNotFoundException());

      
    //If value to be deleted is found at Last Element
    //Make last point to previous and free the memory.
     if(Itr==last && Itr->info ==Item)
     {
         last=previous;
         free(Itr);
         return;
     }

      
    //If element is found at any other node. Un-link that node for the linked list.
   previous->link =Itr->link;

   //Free the memory of the node.
   free(Itr);
   
}

Explanation:

In order to delete an element from a Linked List follow the following steps:-

  1. Declare a pointer which will be used to iterate the Linked List.
  2. Check if the first node stores the element which is to be deleted.If yes, then make the first point to the second element in the Linked List and free the memory occupied by first node.
  3. If No, then declare another Pointer which will keep track of the element previous to the one which is to be deleted.
  4. Iterate the whole Linked List to find the element: Item.
  5. If the element is not found anywhere in the linked list then throw the LinkedListItemNotFoundException.
  6. If the element is stored in the last node then make the last point to the previous node and free up the memory.
  7. If the element is stored by any other node then just separate the node from the linked list.

Separating the node from the linked list will be more clear by the following diagram:-


Related Solutions

Write a member function that deletes all repetitions from the bag. In your implementation, assume that...
Write a member function that deletes all repetitions from the bag. In your implementation, assume that items can be compared for equality using ==. Use the following header for the function: void remove_repetitions() Here is a brief outline of an algorithm: - A node pointer p steps through the bag - For each Item, define a new pointer q equal to p - While the q is not the last Item in the bag ◼ If the next Item has...
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter,...
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing. This constructor should do the following: Declare and open an input file stream variable using the file name string passed in as a parameter. Check to make sure the file was opened successfully. If not, print an...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
*In C++ Please! Give the full implementation of a constant member function that returns the second...
*In C++ Please! Give the full implementation of a constant member function that returns the second element from the top of the stack without actually changing the stack. Write separate solutions for the two different stack versions. (C++ program for array implementation of stack with insert function and display of the second element from top)
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and...
IN SCHEME Write a function subtract which takes a list of numbers as a parameter and returns a list of the differences. So, a list containing the second minus the first, third minus the second, etc. (subtract '(3 4 10 14 5)) '(1 6 4 -9)
Given the following C function prototype which accepts an integer argument, complete the implementation of the...
Given the following C function prototype which accepts an integer argument, complete the implementation of the function in C language to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. USE the following function int countOnes(int number) Write a full c program that has the header required and...
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT