Debug the following program and answer the following questions. #include typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles. There should be a cycle, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[1])?"a":"no"); printf("Checking length-zero list for cycles. There should be none, ll_has_cycle says it has %s cycle\n", ll_has_cycle(NULL)?"a":"no"); printf("A node value is: %d", nodes[2].value); } int main(void) { test_ll_has_cycle(); return 0; } What is the output of this program? Is there a bug/fault you see from the output console? If so, please explain why, fix the bug, and describe how you fix the bug.
Debug the following
#include <stdio.h>
typedef struct node {
int value;
struct node *next;
} node;
int ll_has_cycle(node *first) {
node * head = first;
while (head->next) {
head = head->next;
if (head == first)
return 1;
}
return 0;
}
void test_ll_has_cycle(void) {
int i,j;
node nodes[5];
for(i=0; i < sizeof(nodes)/sizeof(node); i++) {
nodes[i].next = NULL;
nodes[i].value = i;
}
nodes[0].next = &nodes[1];
nodes[1].next = &nodes[2];
nodes[2].next = &nodes[1];
printf("Checking first list for cycles. There should be a cycle,
ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[1])?"a":"no");
printf("Checking length-zero list for cycles. There should be none,
ll_has_cycle says it has %s cycle\n", ll_has_cycle(NULL)?"a":"no");
printf("A node value is: %d", nodes[2].value);
}
int main(void) {
test_ll_has_cycle();
return 0;
}
- What is the output of this program?
- Is there a bug/fault you see from the output console? If so, please explain why, fix the bug, and describe how you fix the bug.

Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images









