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
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:
IntNodeint dataInit IntNode nextLoc nullptr;
void InsertAfterIntNode nodeLoc;
IntNode GetNext;
IntNode GetPrev;
int GetDataVal;
void PrintNodeData;
void PrintList; Print the entire list.
IntNode FindNodeint x;
void DeleteNode; ADD THIS
private:
int dataVal;
IntNode nextNodePtr;
IntNode prevNodePtr; Now DoublyLinked
;
Constructor definition
IntNode::IntNodeint dataInit, IntNode nextLoc
Initialize data value
thisdataVal dataInit;
Set next pointer
thisnextNodePtr nextLoc;
Update previous pointer if nextLoc is not nullptr
if nextLoc
nextLocprevNodePtr this;
Circularly link the last node to the head
else
thisnextNodePtr this;
thisprevNodePtr this;
Insert node after this node.
Before: this next
After: this node next
Inserts a node after this node
void IntNode::InsertAfterIntNode nodeLoc
Store next node
IntNode tmpNext thisnextNodePtr;
Update pointers to insert node
thisnextNodePtr nodeLoc;
nodeLocprevNodePtr this;
nodeLocnextNodePtr tmpNext;
Prints the data value of the node
void IntNode::PrintNodeData
cout thisdataVal endl;
Prints the entire linked list
void IntNode::PrintList
IntNode currObj thisGetNext; Print the list
Traverse and print the list until reaching the head
while currObj this
currObjPrintNodeData;
currObj currObjGetNext;
Finds a node with given data value
IntNode IntNode::FindNodeint x
IntNode currObj thisGetNext; Print the list
Traverse the list until finding a node with the target value or reaching the head
while currObj this
if currObjGetDataValx
return currObj;
currObj currObjGetNext;
return nullptr;
Grab location pointed by nextNodePtr
IntNode IntNode::GetNext
return thisnextNodePtr;
Grab location pointed by prevNodePtr
IntNode IntNode::GetPrev
return thisprevNodePtr;
Returns the dataVal
int IntNode::GetDataVal
return thisdataVal;
void IntNode::DeleteNode
IntNode deleteNode;
deleteNode this;
Make the deleteNode previous IntNode point to the IntNode following the deleteNode
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; Front of nodes list
lastObj headObj;
for i ; i ; i
cin x;
currObj new IntNodex;
lastObjInsertAftercurrObj; Append curr
lastObj currObj; Curr is the new last item
headObjPrintList; Print the list
cin x; Value to Delete
currObj headObjFindNodex;
Add code to delete a node if currObj is not a nullptr else print "Invalid input"
headObjPrintList;
return ;
In c
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
