Question

In: Computer Science

C++ 1. The function removeAt of the class arrayListType removes an element from the list by...

C++
1. The function removeAt of the class arrayListType removes an element from the list by shifting the elements ofthe list. However, if the element to be removed is at the beginning ofthe list and the list is fairly large, it could take a lot ofcomputer time. Because the list elements are in no particular order, you could simply remove the element by swapping the last element ofthe list with the item to be removed and reducing the length of the list. Rewrite the definition of the function removeAt using this technique.
2. The function remove of the class arrayListType removes only the first occurrence of an element. Add the function removeAll to the class arrayListType that would remove all occurrences of a given element. Also, write the definition ofthe function removeAll and a program to test this function.

From the textbook Data Structures using C++ by D. S. Malik

Solutions

Expert Solution

template <class elemType>
void arrayListType<elemType>::removeAt(int location) { 
        if (location < 0 || location >= length)
                cerr << "The location of the item to be removed " << "is out of range" << endl;
        else {
                // do not shift the list if we want to delete the first eleem
                if(location == 0) {
                        list[0] = list[--length];
                        return;
                }

                for (int i = location; i < length - 1; i++)
                        list[i] = list[i+1];
                length--;
        }
} //end removeAt
        
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const {
        int loc;
        bool found = false;

        for (loc = 0; loc < length; loc++)
        if (list[loc] == item) {
                found = true;
                break;
        }

        if (found)
                return loc;
        else
                return -1;
} //end seqSearch


template<class elemType>
void arrayListType<elemType>::removeAll(const elemType& removeItem) {
        int loc;

        if (length == 0)
                cerr << "Cannot delete from an empty list." << endl;
        else {
                loc = seqSearch(removeItem);
                if(loc == -1) {
                        cout << "The item to be deleted is not in the list." << endl;
                }
                while (loc != -1) {
                        removeAt(loc);
                        loc = seqSearch(removeItem);
                }
        }
} //end remove
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

write a c++ member function that removes the first instance of a specific element in a...
write a c++ member function that removes the first instance of a specific element in a linked list and then return the size of the list after the removal whether it was successful or not.
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a...
write a c++ member function that removes the FIRST OCCURENCE of a SPECIFIC ELEMENT in a linked list. After attemtped removal return the SIZE of the linked lost whether or not the removal was successful.
3.) The function remove of the class arrayListType removes only the first occurrence of an element....
3.) The function remove of the class arrayListType removes only the first occurrence of an element. Add the function removeAll as an abstract function to the class arrayListType, which would remove all occurrences of a given element. Also, write the definition of the function removeAll in the class unorderedArrayListType and write a program to test this function. 4.) Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element...
In C. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Let f: [0 1] → R be a function of the class c ^ 2 that...
Let f: [0 1] → R be a function of the class c ^ 2 that satisfies the differential equation f '' (x) = e^xf(x) for all x in (0,1). Show that if x0 is in (0,1) then f can not have a positive local maximum at x0 and can not have a negative local minimum at x0. If f (0) = f (1) = 0, prove that f = 0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT