Below are the class definitions for a doubly linked list of items where the item
ID: 3831953 • Letter: B
Question
Below are the class definitions for a doubly linked list of items where the items are integers. Write the method that will search for a particular item value (passed as a parameter) and remove that item from the list, if it is found, Your method should return true if the remove was successful, or false if the item was not in the list. class Node ( public: int item; Node * next Node * previous}; class Double {Node first; // first list node public: // constructor Double () {first = NULL;} // add your method remove here!};Explanation / Answer
Please find my implementation,
class Node{
public:
int item;
Node *next;
Node previous;
};
class DoubleLL{
Node *first;
public:
DoubleLL() {
first = NULL;
}
bool searchAndRemove(int data){
if(first == NULL){
return false;
}
if(first->item == data){
Node t = first;
first = first->next;
if(first != NULL)
first->previous = NULL;
delete t;
return true;
}
Node *temp = first->next;
while(temp != NULL){
if(temp->item == data)
break;
temp = temp->next;
}
// if last item
if(temp->next == NULL){
Node *t = temp;
temp = temp->previous;
temp->next = NULL;
delete t;
}else{
Node *t = temp;
temp= temp->previous;
temp->next = t->next;
t->previous = temp;
delete t;
}
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.