Question

In: Computer Science

Write a member function named deleteItemAtIndex(). You will need to search till you get to the...

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

Solutions

Expert Solution

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


Related Solutions

Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
C++ Assume you need to test a function named inOrder. The function inOrder receives three int...
C++ Assume you need to test a function named inOrder. The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if inOrder is correct and returns false otherwise. ....
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
Write a GLM function named getInverse that returns the inverse of A. If A does not...
Write a GLM function named getInverse that returns the inverse of A. If A does not have an inverse, the function returns the identify matrix. Assume A is 3x3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT