C Program for Detect and Remove cycle in a single linked list

By | 07.04.2017

Detect and Remove cycle in a single linked list


Write a C Program for Detect and Remove cycle in a single linked list. Here’s simple Program for Detect and Remove cycle in a single linked list in C Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C Program for Detect and Remove cycle in a single linked list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program for Detect and Remove cycle in a single linked list */

#include<stdio.h>
#include<stdlib.h>

struct node
{
        int info;
        struct node *link;
};

struct node *create_list(struct node *start);
void display(struct node *start);
struct node *addatbeg(struct node *start,int data);
struct node *addatend(struct node *start,int data);
struct node *findCycle(struct node *start);
int lengthList(struct node *start, struct node *C);

int main()
{
        struct node *start=NULL;
        struct node *p,*psave=NULL, *prev, *ptr;
        int len,i;

        start=create_list(start);
        if(start==NULL)
                exit(1);

    /*Insert a cycle at node containing 0*/
        prev=p=start;
        while(p!=NULL)
        {
                if(p->info==0)
                        psave=p;
                prev=p;
                p=p->link;
        }
        if(psave!=NULL)
                prev->link=psave;

        ptr = findCycle(start);

        if(ptr==NULL)
                printf("List is NULL terminated\n");
        else
        {
                printf("\nThere is a cycle in the list\n");
                len = lengthList(start, ptr);
                printf("Length of the list is : %d\n",len);
                ptr=start;
                for(i=1;i<len;i++)
                        ptr=ptr->link;
                ptr->link=NULL;
                printf("Cycle Removed, now the list is NULL terminated\n");
        }
        display(start);

        return 0;

}/*End of main()*/

/*Returns NULL if the list is NULL terminated otherwise returns the node where both pointers meet*/
struct node *findCycle(struct node *start)
{
        struct node *slowP, *fastP;
        if(start->link==NULL) /*only one element*/
                return NULL;
        slowP=fastP=start;
        while(fastP!=NULL && fastP->link!=NULL)
        {
                slowP=slowP->link;
                fastP=fastP->link->link;
                if( slowP==fastP )
                        return slowP;
        }
        return NULL;
}/*End of findCycle()*/

int lengthList(struct node *start, struct node *C)
{
        struct node *p1, *p2;
        int len_Cycle, len_remainingList;

        printf("Node at which the cycle was detected is %d\n",C->info);

        p1=p2=C;
        len_Cycle=0;
        do
        {
                len_Cycle++;
                p2=p2->link;
        }while(p1!=p2);
        printf("Length of cycle is : %d\n",len_Cycle);

        len_remainingList=0;
        p1=start;
        while(p1!=p2)
        {
                len_remainingList++;
                p1=p1->link;
                p2=p2->link;
        }
        printf("Node that causes cycle is : %d\n",p1->info);
        printf("Number of nodes not included in the cycle are : %d\n",len_remainingList);

        return len_Cycle + len_remainingList;
}/*End of lengthList()*/

struct node *create_list(struct node *start)
{
        int i,n,data;
        printf("Enter the number of nodes : ");
        scanf("%d",&n);
        start=NULL;
        if(n==0)
                return start;

        printf("\nEnter the element to be inserted : ");
        scanf("%d",&data);
        start=addatbeg(start,data);

        for(i=2;i<=n;i++)
        {
                printf("\nEnter the element to be inserted : ");
                scanf("%d",&data);
                start=addatend(start,data);
        }
        return start;
}/*End of create_list()*/

void display(struct node *start)
{
        struct node *p;
        if(start==NULL)
        {
                printf("\nList is empty\n");
                return;
        }
        p=start;
        printf("\nList is :\n");
        while(p!=NULL)
        {
                printf("%d ",p->info);
                p=p->link;
        }
        printf("\n\n");
}/*End of display() */

struct node *addatbeg(struct node *start,int data)
{
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        tmp->link=start;
        start=tmp;
        return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start,int data)
{
        struct node *p,*tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        p=start;
        while(p->link!=NULL)
                p=p->link;
        p->link=tmp;
        tmp->link=NULL;
        return start;
}/*End of addatend()*/

OUTPUT : :


/* C Program for Detect and Remove cycle in a single linked list */

************** OUTPUT ****************

Enter the number of nodes : 6

Enter the element to be inserted : 1

Enter the element to be inserted : 2

Enter the element to be inserted : 3

Enter the element to be inserted : 0

Enter the element to be inserted : 4

Enter the element to be inserted : 5

There is a cycle in the list
Node at which the cycle was detected is 0
Length of cycle is : 3
Node that causes cycle is : 0
Number of nodes not included in the cycle are : 3
Length of the list is : 6
Cycle Removed, now the list is NULL terminated

List is :
1 2 3 0 4 5

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments