Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
#include<stdio.h>

typedef struct node_s {
      void *data;
      struct node_s *next;
} NODE;

int list_has_cycle(NODE *...
Upcoming SlideShare
Loading in …5
×

Deteccion de Ciclos

517 views

Published on

Published in: Sports, Technology
  • Be the first to comment

  • Be the first to like this

Deteccion de Ciclos

  1. 1. #include<stdio.h> typedef struct node_s { void *data; struct node_s *next; } NODE; int list_has_cycle(NODE *list) { NODE *fast=list; while(1) { if(!(fast=fast->next)) return 0; if(fast==list) return 1; if(!(fast=fast->next)) return 0; if(fast==list) return 1; list=list->next; } return 0; } int main() { NODE n1, n2, n3, n4, n5; n1.next=&n2; n2.next=&n3; n3.next=&n4; n4.next=&n5; n5.next=NULL; printf("Test without cycle: "); if(list_has_cycle(&n1)) printf("cyclen"); else printf("no cyclen"); n5.next=&n3; printf("Test with cycle: "); if(list_has_cycle(&n1)) printf("cyclen"); else printf("no cyclen"); return 0; }

×