C program plz thanks so much include typedef struct node i

C program plz! thanks so much !

#include <stdio.h>

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

int has_cycle(node *head)
{

}

void test_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, has_cycle says it has %s cycle\ \",
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, has_cycle says it has %s cycle\ \",
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, has_cycle says it has %s cycle\ \",
has_cycle(&nodes[11])?\"a\":\"no\");
  
nodes[18].next = &nodes[18];
printf(\"Checking fourth list for cycles. There should be a cycle, has_cycle says it has %s cycle\ \",
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, has_cycle says it has %s cycle\ \",
has_cycle(&nodes[19])?\"a\":\"no\");
  
printf(\"Checking length-zero list for cycles. There should be none, has_cycle says it has %s cycle\ \",
has_cycle(NULL)?\"a\":\"no\");
}

int
main(void)
{
test_has_cycle();
return 0;
}

(Exercise) Cyclic Linked list In cyclic ll.c, complete the function has cycle() to implement the following algorithm for checking if a singly-linked list has a cycle Recall that if p is a pointer to a struct, p->member refers to a member variable in the struct, and is equivalent to (*p).member 1) Start with two pointers at the head of the list 2) On each iteration, increment the first pointer by one node and the second pointer by two nodes. If it is not possible to do one or both of these because of a null pointer, then we know there is an end to the list and there is therefore no cycle 3) We know there is a cycle if a) The second pointer is the same as the first pointer b) The next node of the second pointer is pointed to by the first pointer The reason we know there is a cycle with the two conditions in 3 is that second pointer has wrapped around to the first one in the circle and we have detected it. After you have correctly implemented has cycle, the program you get when you compile cyclic ll.c will tell you that has cycle agrees with what the program expected it to output.

Solution


int has_cycle(node *head)
{
    struct node *s = head, *f = head;//two pointes

    while (s && f && f->next )
    {
        s = s->next;//increase by 1
        f = f->next->next;//increase by 2
        if (s == f)
        {
           return 1;
        }
    }
    return 0;
}

C program plz! thanks so much ! #include <stdio.h> typedef struct node { int value; struct node *next; } node; int has_cycle(node *head) { } void test_has
C program plz! thanks so much ! #include <stdio.h> typedef struct node { int value; struct node *next; } node; int has_cycle(node *head) { } void test_has

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site