In: Computer Science
Assume that
struct Node{
int item;
Node* link;
};
Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr.
Here is the function and screenshot of how to use it.
// function to remove node after pre_ptr node
void list_remove(Node *&pre_ptr)
{
// if there is no next node then do nothing
if (pre_ptr == nullptr || pre_ptr->link == nullptr)
return;
else
{
Node *temp = pre_ptr->link; // storing pointer to next node
pre_ptr->link = pre_ptr->link->link; // pointing the pointer to next->next node
delete (temp); // deleting temp node
}
return;
}
Here is the whole code
#include <bits/stdc++.h>
using namespace std;
// structure of Node
struct Node
{
int item;
Node *link;
};
// function to remove node after pre_ptr node
void list_remove(Node *&pre_ptr)
{
// if there is no next node then do nothing
if (pre_ptr == nullptr || pre_ptr->link == nullptr)
return;
else
{
Node *temp = pre_ptr->link; // storing pointer to next node
pre_ptr->link = pre_ptr->link->link; // pointing the pointer to next->next node
delete (temp); // deleting temp node
}
return;
}
int main()
{
Node *head = nullptr;
head = new Node();
head->item = 10l;
head->link = new Node();
head->link->item = 200;
head->link->link = nullptr;
head->link->link = new Node();
head->link->link->item = 300;
head->link->link->link = nullptr;
list_remove(head);
cout << head->link->item << endl;
}