Implement the following function as a new function for the linked list toolkit.
ID: 642936 • Letter: I
Question
Implement the following function as a new function for the linked list toolkit. (Use the usual node definition with member variables called data and link as defined above.)
bool all_42s(const node* head_ptr);
// Precondition: head_ptr is the head pointer of a linked list.
// The list might be empty or it might be non-empty.
// Postcondition: The return value is true if every node in the
// list contains 42 in the data part. NOTE: If the list is empty,
// then the function returns true.
Explanation / Answer
bool all_42s(struct node *head)
{
bool result=false;
int count=0,length=0;
struct node *header = head;
if (head!=NULL && head->next!=NULL)
{
while (header != NULL && header->next != NULL)
{
length++;
if(header.val==42){
count++;
}
header = header->next->next;
}
}
if(length==count){
res=true;
}
return res;
}
bool all_42s(const node* head_ptr);