#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *next;
};
void display(struct node *r)
{
printf("elements of list are : ");
while (r)
{
printf("%d ---> ", r->data);
r = r->next;
}
printf("NULL\n");
}
void display_cylic(struct node *r)
{
struct node *head = r;
printf("elements of list are : ");
do
{
printf("%d ---> ", r->data);
r = r->next;
} while (r != head);
printf("%d\n", head->data);
}
struct node *create(struct node *head)
{
struct node *t, *p;
int n;
if (head == NULL)
{
t = (struct node *)malloc(sizeof(struct node));
printf("enter the node : \n");
scanf("%d", &t->data);
t->next = NULL;
head = t;
}
if (head != NULL)
{
do
{
t = (struct node *)malloc(sizeof(struct node));
printf("enter the node: \n");
scanf("%d", &t->data);
t->next = NULL;
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = t;
printf("do you want to add more node press 1 for yes, 0 for no. ");
scanf("%d", &n);
} while (n);
}
return head;
}
struct node *create_cyclic(struct node *head)
{
struct node *t, *p;
int n;
if (head == NULL)
{
t = (struct node *)malloc(sizeof(struct node));
printf("enter the node : \n");
scanf("%d", &t->data);
t->next = NULL;
head = t;
}
if (head != NULL)
{
do
{
t = (struct node *)malloc(sizeof(struct node));
printf("enter the node: \n");
scanf("%d", &t->data);
t->next = NULL;
p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = t;
printf("do you want to add more node press 1 for yes, 0 for no. ");
scanf("%d", &n);
} while (n);
}
p->next->next = head;
return head;
}
void check_cylic(struct node *r)
{
struct node *head = r;
while (r!=NULL)
{
r = r->next;
if(r==head){
printf("the linked_list is cylic\n");
exit(0);
}
}
printf("the linked_list is not cylic\n");
}
int main()
{
int i, c, r;
struct node *head1 = NULL,*head2=NULL;
printf("####enter the data of linked_list.####\n");
head1 = create(head1);
display(head1);
check_cylic(head1);
head2 = create_cyclic(head2);
display_cylic(head2);
check_cylic(head2);
return 0;
}