これが私が循環リンクリストのために書いたコードへのリンクです。コードは下にも貼り付けられています。
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail, *temp,*sp,*fp;
void add(int value);
void iterative_reverse();
void print_list();
void findcycle();
int main()
{
head=(mynode *)0;
add(1);
add(2);
add(3);
//print_list();
findcycle();
return(0);
}
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->value=value;
temp->next=(mynode *)0;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
temp->next=head;
}
}
void findcycle()
{
if (head == NULL || head->next == NULL)
printf("null");
sp=head;
fp=head->next;
while (fp != NULL && fp->next != NULL)
{
if ((fp == sp) || (fp->next == sp))
printf("Cycle");
sp = sp->next;
fp = fp->next->next;
}
printf("Not a Cycle");
}
void print_list()
{
for(temp=head; temp!=tail; temp=temp->next)
printf("[%d]->",(temp->value));
}
I had initially written it for single and then changed few pointers to make it circular. I am doing some mistake in it which I am not able to track and hence getting a Timeout. Please suggest.
Thanks a lot.