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
- Recommended textbooks for youDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSONC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag…Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education