in c++
Each of the member functions in WrongCode.cpp has errors in the way it performs a linked list operation. Find as many mistakes as you can
//--------------------one
void NumberList::appendNode(double num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new listNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode.
{
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//--------------------two
void NumberList::deleteNode(double num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
delete head;
else
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
//--------------------there
NumberList::~NumberList()
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
nodePtr->next = nullptr;
nodePtr = nextNode;
}
}
In the above problem, I have updated the code and it is marked in bold. which are the changes in the code.
void NumberList::appendNode(double num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new listNode;
newNode->value = num;
newNode->next = NULL; // This is to be added in order to end the list.
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode.
{
// Find the last node in the list.
// nodePtr = head; // This is the node pointer which is to
be added
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//--------------------two
void NumberList::deleteNode(double num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
nodePtr = head->next;
head = nodePtr;
delete nodePtr;
else
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
//--------------------there
NumberList::~NumberList()
{
ListNode *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
nodePtr->next = nullptr;
delete nodePtr; // This will delete the memory allocated for the node
nodePtr = nextNode;
}
}
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks
Get Answers For Free
Most questions answered within 1 hours.