Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C++ Write the definition of the function deleteElem() as a member function of the class AnyList as follows: - Parameter: An int storing an
in C++
Write the definition of the function deleteElem() as a member function of the class AnyList as follows: - Parameter: An int storing an element to delete - The function searches for the value passed by the parameter and, if found, it deletes it if the element is not found, the function prints the error message "Element \# is not in the list. " where \# is replaced by the element - The function traverses the list using a WHILE loop. To make your function efficient, make sure it stops when it finds the element-use a Boolean value) - If the list is empty, the function prints the error message "The list is empty" - Assumption: All elements are unique. Make sure you do NOT write redundant code such as (found = true) or similar. Pseudocode Algorithm deleteElem if list is empty print error message else if it is the first node else delete first node and reset appropriate variables traverse the list until either element is found or end of list if elem is found if elem not found delete the node print message \#include ciostream> using namespace std; // Definition function deleteElem 1/ Your code here... void Anylist: : deleteElem (int num) f if ( count =) \{ cerr "The list is empty. In"; j else \{ Node* current = first; bool found = false; if (first->getData() = num) t first = first->getNext () ; delete current; current = nullptr: found = true; . count: ] else i while (current getNext() I= nullptr) f if (current->getData() = num ) \{ delete current; current = nullptr; found = true; 3 current = current getNext () : \} count ; 3 if ( 1 found) t cerr "Element " num " is not in the list. In"; J \} \} class AnyList public: Anylist(); void insertFront(int); void print() const; void clearlist(); AnyList(); private: // Pointer to point to the first node in the list. Node *first; // Variable to keep track of number of nodes in the list. int count; UST li List is empty. LIST 1: List is espty. Delete 4. Delete 4.. The list is emoty. The list is eepty. Resulting list: List is enpty. Resulting List: List is eopty. LIST 2:2 LIST 2:2 Delete 2... Delete 2... Resulting list: List is enpty. Rosulting list: List is empty. LIST 3: 5 LIST 3:5 Delete 1... Delete 1... Elebent 1 is not in the List. Element 1 is not in the list. Resulting List: 5 Resulting list: 5 LIST 4:63 LIST 4:63 Delete 6 Delete 6 Resulting List: 3 . Resulting list: 3 LIST 5:7 5 LIST 5 : 75 Delete 5.. Delete 5.. flement 5 is not in the list. Resulting list: 7 Resulting list: 75 LIST 6: 19 HST 6:19 Delete 2... Delete 2. Element 2 is not in the list. Element 2 is not in the tist. Resulting list: 19 Resulting List: 19 LIST 7: 84295671 HST 7:84.295671 Delete 8 ..: Delete B. Resulting list: 4.295671 Resulting Vist: 4.2.95.6.71 Delete 85 Delete is. Element 85 is not in the List. LIST 9: 674523791562945771.294532 Delete 62 Resulting list: 674523.79.15945771294532 LST 10: 458732.197764548322.91 Delete 100... Elenent 100 is not in the list. Resulting List: 458732197764832291 Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started