Using C++ write a function that will remove all nodes that contain Mercedes from
ID: 3728183 • Letter: U
Question
Using C++ write a function that will remove all nodes that contain Mercedes from a link list. Note that one Mercedes may follow another one. Consider the case where the entire linked list may be filled with nodes that nothing but Mercedes and consider the case where the linked list may be empty. Using C++ write a function that will remove all nodes that contain Mercedes from a link list. Note that one Mercedes may follow another one. Consider the case where the entire linked list may be filled with nodes that nothing but Mercedes and consider the case where the linked list may be empty.Explanation / Answer
// Deletion of Mercedes code:
/*function to delet the node with Mercedes*/
void deleteNode(struct Node **head_ref)
{
// Store head node
Node* temp = new Node;
Node* prev = new Node;
temp = *head_ref;
string key = "Mercedes";
// Delete all occurrences of Mercedes at the start of the list
while (temp != NULL && (temp->data)== key)
{
*head_ref = temp->next; // Changed head
temp = *head_ref;
}
temp = *head_ref;
prev = *head_ref;
// Search the key Mercedes in the linked list
while (temp!=NULL){
if (temp->data== key)
{
prev->next = temp->next; //delete the node
temp = prev->next; // Change the temp
}
else{
prev = temp;
temp = temp->next;
}
}
}
// Full working C++ Code:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
/*Node to store the dat aof linked list*/
struct Node
{
string data;
struct Node *next;
};
/*function to delet the node with Mercedes*/
void deleteNode(struct Node **head_ref)
{
// Store head node
Node* temp = new Node;
Node* prev = new Node;
temp = *head_ref;
string key = "Mercedes";
// Delete all occurrences of Mercedes at the start of the list
while (temp != NULL && (temp->data)== key)
{
*head_ref = temp->next; // Changed head
temp = *head_ref;
}
temp = *head_ref;
prev = *head_ref;
// Search the key Mercedes in the linked list
while (temp!=NULL){
if (temp->data== key)
{
prev->next = temp->next; //delete the node
temp = prev->next; // Change the temp
}
else{
prev = temp;
temp = temp->next;
}
}
}
/*-----------------------------------Below code is just for testing purpose---------------------*/
/* push a new node at the front of the list*/
void push(struct Node** head_ref, string new_data)
{
/* allocate node */
Node* new_node = new Node;
/* put in the data */
new_node->data = new_data;
/* set the head of old list as the next node of new node */
new_node->next = (*head_ref);
/* change the head as new node*/
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "Benz");
push(&head, "Jaguar");
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "BMW");
push(&head, "Ferrari");
push(&head, "Mercedes");
push(&head, "BMW");
push(&head, "Mercedes");
push(&head, "Mercedes");
push(&head, "Mercedes");
deleteNode(&head);
struct Node* node = head;
/*Note that I am inserting the new element at the head so printing of elements is done opposite of insertion*/
cout<<"Elements of the Linked List:"<<endl;
while (node != NULL)
{
cout<< node->data<<endl;
node = node->next;
}
return 0;
}
// Sample Output:
Elements of the Linked List:
BMW
Ferrari
BMW
Jaguar
Benz
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.