C++ Consider the following code: // Linked Lists: UPDATE (replace data in a node
ID: 3908377 • Letter: C
Question
C++
Consider the following code: // Linked Lists: UPDATE (replace data in a node specified by a number) struct Data { int number; char ch; }; struct ListNode { Data data; struct ListNode *next; }; Assume that a linked list has been created and head points to a sentinel node. A sentinel node is an empty data node in the beginning of the list. It sometimes holds a sentinel value. The use of sentinel nodes is a popular trick to simplify the insert and delete operations. You may also assume that the list is not empty. The data in the sentinel node is -9999. Write a function named updateNode that is passed the pointer to the sentinel node, the node number of the node to be updated, and the newData. The function returns false in this case, true otherwise. A calling statement for this function is given below: Data newData; // assume it has data bool success = updateNode(head, number, newData);
Explanation / Answer
bool updateNode(ListNode *head, int number, Data newData){
ListNode* p = head;
int count = 0;
while(count < number){
p = p->next;
count++
}
if (p != NULL){
p->number = newData.number;
p->ch = newData.ch;
return true;
}
else {
return false
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.