Question

In: Computer Science

Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...

Please debug the code and answer the questions:

#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;
}

(a) What is the output of this program?

(b) Is there a bug / fault you see from the output console? If so, explain why, fix the bug, and describe how you fixed the bug?

Solutions

Expert Solution

Note: If you like my efforts then please do upvote this answer.

--------------------------------------------------------------------------------------------------

Initially when you run this code it will show the segmentation fault.

So, I am answering the part (b) first.

(b) Yes, there is a bug/fault. The reason of the fault is that there is no null check in the code for cycle detection..We can fix the code by adding these two lines in the code. See the highlighted bold lines in the code.

#include <stdio.h>
typedef struct node {
int value;
struct node *next;
} node;
int ll_has_cycle(node *first) {
if(first == NULL) //Newly added line
return 0; //Newly added line

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;
}

---------------------------------------------------------------------------------------------------

(a) After correcting the fault the output of the code will be:


Related Solutions

#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct node* insert(struct node* list,int d ); struct node* del(struct node* list,int d ); void print( struct node *list); void freeList(struct node* list); void copy ( struct node *q, struct node **s ); int main( ) {     int number = 0, choice = 0;     struct node *pList=NULL;     struct node *nList = NULL;         while(choice!= 4)     {                 printf("Do you want to (1)insert, (2)delete, (3)Copy (4)quit.\n");...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
Flow this Code to answer question below #include <iostream> using std::cout; struct Node {     int...
Flow this Code to answer question below #include <iostream> using std::cout; struct Node {     int data;     Node *link;                  Node(int data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs         this->data = data;                                   link = p;     } }; class linked_list { private:     Node *head,*current; public:      //constructor     linked_list() {         head = nullptr;//the head pointer         current = nullptr;//acts as...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT