I'm trying to implement a program for finding a starting node of circular linked list. My code is-
struct node { char data; struct node *link; } ; char FindStartNode(struct node **q) { struct node *r,*t; r = *q; t = *q; while(t->link != NULL) { r = r->link; t = t->link->link; if(r == t) { break; } } if(t == NULL ) return NULL; r = *q; while(r != t) { t = t->link; r = r->link; } return t->data; } int main() { struct node *p; p = NULL; char num; Append(&p,'A'); Append(&p,'B'); Append(&p,'C'); Append(&p,'D'); Append(&p,'E'); Append(&p,'C'); Display(p); num = FindStartNode(&p); printf("\nStarting node of the cycle linked list is:- %c",num); _getch(); return 0; } int Append(struct node **q, char data) { struct node *r,*t; r = (struct node *)malloc(sizeof(struct node)); r->data = data; r->link = NULL; if(*q == NULL) *q = r; else { t = *q; while(t->link != NULL) { t = t->link; } t->link = r; } return 0; } int Display(struct node *q) { while(q != NULL) { printf("%c\t",q->data); q = q->link; } return 0; } ths is my code. I'm not getting any value in return t->data part or I'm unable to find the start node of cycle ink list.Any help?
NULLdereference that seems to be leading to this question, then it appears that you might get stuck in the lastwhileloop withrendlessly chasingt.*qhas a pointer to some node, and the one you want is the one before it ?Floyd's cycle-finding algorithmAKAthe tortoise and the hare algorithm. You can read about it here.