Data Structure - [Circular Linked List]

♠ Posted by Unknown in , at 02:27

Example of Circular Linked List



The node in circular linked list is as like as linear linked list having two rooms one is for storing data and another is storing the reference of next node. Instead of linear list the last node is stored the reference of first node in the "next" room of it.




Image: Circular Linked List


Example: 

#include<stdio.h>
#include<malloc.h>
struct node
{
            int data;
            struct node *next;
};

main()
{
            struct node *front,*rear;
            int ans;
            front = rear = NULL;
            do
            {
                        printf("\n 1.insert node:\n");
                        printf("\n 2.delet node:\n");
                        printf("\n 3.display:\n");
                        printf("\n 4.exit\n");
                        printf("enter choice\n");
                        scanf("%d",&ans);
                        switch(ans)
                        {
                                    case 1:
                                                add(&front,&rear);
                                                break;
                                    case 2:
                                                delet(&front,&rear);
                                                break;
                                    case 3:
                                                display(front);
                                                break;
                                    case 4:
                                                break;
                        }
            }while(ans!=4);
            return;
}

add(struct node **f,struct node **r)
{
            struct node *q;
            int a;
            q=(struct node*)malloc(sizeof(struct node));
            printf("\n enter the value:");
            scanf("%d",&a);
            q->data=a;
            if(*f==NULL)
                        *f=q;
            else
                        (*r)->next=q;
            *r=q;
            (*r)->next=*f;
            return;
}


delet(struct node **f,struct node **r)
{
            struct node *q;
            int a;
            if(*f==NULL)
                        printf("\n list empty");
            else
            {
                        if(*f==*r)
                        {
                                    a=(*f)->data;
                                    free(*f);
                                    *f=NULL;
                                    *r=NULL;
                        }
                        else
                        {
                                    q=*f;
                                    a=q->data;
                                    (*f)=(*f)->next;
                                    (*r)->next=*f;
                                    free(q);
                        }
                        printf("\n deleted node %d",a);
                        return;
            }
       return;
}

display(struct node *f)
{
            struct node *q=f,*p=NULL;
            while(q!=p)
            {
                        printf("%2d\t",q->data);
                        q=q->next;
                        p=f;
            }
            return;
}

0 comments:

Post a Comment