Question

Modify the following code in order for it to perform the following operations: *This code is...

Modify the following code in order for it to perform the following operations:

*This code is meant to simulate Floyd's cycle finding algorithm*

A) Start with two pointers at the head of the list. We'll call the first one tortoise and the second one hare

B) Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic

C) Advance tortoise by one node. (A null pointer check is unnecessary. Why?)

D) If tortoise and hare point to the same node, the list is cyclic. Otherwise, go back to step 2.

C CODE:

____________________________________________________________________________________________

#include <stdio.h>

typedef struct node {
   int value;
   struct node *next;
} node;

int ll_has_cycle(node *head) {
   /* your code here */
}

void test_ll_has_cycle(void) {
   int i;
   node nodes[25]; //enough to run our tests
   for(i=0; i < sizeof(nodes)/sizeof(node); i++) {
       nodes[i].next = 0;
       nodes[i].value = 0;
   }
   nodes[0].next = &nodes[1];
   nodes[1].next = &nodes[2];
   nodes[2].next = &nodes[3];
   printf("Checking first list for cycles. There should be none, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[0])?"a":"no");

   nodes[4].next = &nodes[5];
   nodes[5].next = &nodes[6];
   nodes[6].next = &nodes[7];
   nodes[7].next = &nodes[8];
   nodes[8].next = &nodes[9];
   nodes[9].next = &nodes[10];
   nodes[10].next = &nodes[4];
   printf("Checking second list for cycles. There should be a cycle, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[4])?"a":"no");

   nodes[11].next = &nodes[12];
   nodes[12].next = &nodes[13];
   nodes[13].next = &nodes[14];
   nodes[14].next = &nodes[15];
   nodes[15].next = &nodes[16];
   nodes[16].next = &nodes[17];
   nodes[17].next = &nodes[14];
   printf("Checking third list for cycles. There should be a cycle, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[11])?"a":"no");

   nodes[18].next = &nodes[18];
   printf("Checking fourth list for cycles. There should be a cycle, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[18])?"a":"no");

   nodes[19].next = &nodes[20];
   nodes[20].next = &nodes[21];
   nodes[21].next = &nodes[22];
   nodes[22].next = &nodes[23];
   printf("Checking fifth list for cycles. There should be none, ll_has_cycle says it has %s cycle\n", ll_has_cycle(&nodes[19])?"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");
}

int main(void) {
test_ll_has_cycle();
return 0;
}
____________________________________________________________________________________________

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to detect loop in a linked list
#include<stdio.h>
#include<stdlib.h>

/* Link list node */
struct Node
{
   int data;
   struct Node* next;
};

void push(struct Node** head_ref, int new_data)
{
   /* allocate node */
   struct Node* new_node =
       (struct Node*) malloc(sizeof(struct Node));

   /* put in the data */
   new_node->data = new_data;

   /* link the old list off the new node */
   new_node->next = (*head_ref);

   /* move the head to point to the new node */
   (*head_ref) = new_node;
}

int detectloop(struct Node *list)
{
   struct Node *slow_p = list, *fast_p = list;

   while (slow_p && fast_p && fast_p->next )
   {
       slow_p = slow_p->next;
       fast_p = fast_p->next->next;
       if (slow_p == fast_p)
       {
       printf("Found Loop");
       return 1;
       }
   }
   return 0;
}

/* Drier program to test above function*/
int main()
{
   /* Start with the empty list */
   struct Node* head = NULL;

   push(&head, 20);
   push(&head, 4);
   push(&head, 15);
   push(&head, 10);

   /* Create a loop for testing */
   head->next->next->next->next = head;
   detectloop(head);

   return 0;
}

Output:
Found loop

Add a comment
Know the answer?
Add Answer to:
Modify the following code in order for it to perform the following operations: *This code is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Following changes need to be made, and the code for each is provided: (1) Modify newMovieReviewNode(),...

    Following changes need to be made, and the code for each is provided: (1) Modify newMovieReviewNode(), this time the newly allocated review should get an empty linked list of scores ReviewNode *newMovieReviewNode() { /* * This function allocates a new, empty ReviewNode, and initializes the * contents of the MovieReview for this node to empty values. * The fields in the MovieReview should be set to: * movie_title="" * movie_studio="" * year = -1 * BO_total = -1 * score...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • Please explain step by step what is going on each step.please clearly explain line by line...

    Please explain step by step what is going on each step.please clearly explain line by line the working of code . Also provide the output. I would rate positively. Thank you so much . #include <stdio.h> #include <stdlib.h> struct  node  {       int data;       struct  node  *next;   };   struct  node  *head = NULL; void  printList() {       struct  node  *ptr  = head;       printf("\nhead:");            while(ptr !=  NULL) {             printf("node  addr:%p   \tdata:%d   \tnext addr:%p\n”, ptr,ptr->data,ptr->next);                            ptr = ptr->next;         }       printf("  [null]\n"); } void  insert(int  data) {       struct  node  *link = (struct node*)        malloc(sizeof(struct  node));       link->data  = data;       link->next  = head;       head  = link; } int main()  {...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT