Question: In c + + 1 2 . 7 3 Doubly Linked List Modify the given code to: Declare member function: DeleteNode. Implement a member function

In c++
12.73 Doubly Linked List
Modify the given code to:
Declare member function: DeleteNode.
Implement a member function named DeleteNode that deletes the node only if currObj is not nullptr; otherwise, print "Invalid Input".
Modify the member function DeleteNode to make the objects point properly.
#include
#include
using namespace std;
class IntNode {
public:
IntNode(int dataInit =0, IntNode* nextLoc = nullptr);
void InsertAfter(IntNode* nodeLoc);
IntNode* GetNext();
IntNode* GetPrev();
int GetDataVal();
void PrintNodeData();
void PrintList(); /// Print the entire list.
IntNode* FindNode(int x);
/// void DeleteNode(); // ADD THIS
private:
int dataVal;
IntNode* nextNodePtr;
IntNode* prevNodePtr; // Now Doubly-Linked
};
// Constructor definition
IntNode::IntNode(int dataInit, IntNode* nextLoc){
// Initialize data value
this->dataVal = dataInit;
// Set next pointer
this->nextNodePtr = nextLoc;
// Update previous pointer if nextLoc is not nullptr
if (nextLoc){
nextLoc->prevNodePtr = this;
}
// Circularly link the last node to the head
else {
this->nextNodePtr = this;
this->prevNodePtr = this;
}
}
/* Insert node after this node.
* Before: this -- next
* After: this -- node -- next
*/
// Inserts a node after this node
void IntNode::InsertAfter(IntNode* nodeLoc){
// Store next node
IntNode* tmpNext = this->nextNodePtr;
// Update pointers to insert node
this->nextNodePtr = nodeLoc;
nodeLoc->prevNodePtr = this;
nodeLoc->nextNodePtr = tmpNext;
}
// Prints the data value of the node
void IntNode::PrintNodeData(){
cout << this->dataVal << endl;
}
// Prints the entire linked list
void IntNode::PrintList(){
IntNode* currObj = this->GetNext(); // Print the list
// Traverse and print the list until reaching the head
while (currObj != this){
currObj->PrintNodeData();
currObj = currObj->GetNext();
}
}
// Finds a node with given data value
IntNode* IntNode::FindNode(int x){
IntNode* currObj = this->GetNext(); // Print the list
// Traverse the list until finding a node with the target value or reaching the head
while (currObj != this){
if (currObj->GetDataVal()==x){
return currObj;
}
currObj = currObj->GetNext();
}
return nullptr;
}
// Grab location pointed by nextNodePtr
IntNode* IntNode::GetNext(){
return this->nextNodePtr;
}
// Grab location pointed by prevNodePtr
IntNode* IntNode::GetPrev(){
return this->prevNodePtr;
}
// Returns the dataVal
int IntNode::GetDataVal(){
return this->dataVal;
}
/*
void IntNode::DeleteNode(){
IntNode* deleteNode;
deleteNode = this;
/////1. Make the deleteNode previous IntNode point to the IntNode following the deleteNode
/////2. Make the deleteNode next IntNode point to the IntNode before the deleteNode
delete deleteNode; // Now free up the memory
}
*/
int main(){
IntNode* headObj = nullptr; // Create IntNode pointers
IntNode* currObj = nullptr;
IntNode* lastObj = nullptr;
int i, x;
headObj = new IntNode(-1); // Front of nodes list
lastObj = headObj;
for (i =0; i <10; ++i){//
cin >> x;
currObj = new IntNode(x);
lastObj->InsertAfter(currObj); // Append curr
lastObj = currObj; // Curr is the new last item
}
headObj->PrintList(); // Print the list
cin >> x; // Value to Delete
currObj = headObj->FindNode(x);
/// Add code to delete a node if currObj is not a nullptr else print "Invalid input"
headObj->PrintList();
return 0;
}
In c++

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!