Circular linked List

 In this blog, I have provided you with the code of how can be dynamically create a circular linked list and how you can traverse,insert and delete in the linked list at the head,tail and at any index.


IMPLEMENTATION IN C

#include<stdio.h>
#include<stdlib.h>
struct student{
    int marks;
    struct student*next;
};


int count;
void display(struct student*head){
    int i=0;
    struct student*start;
    start=head;
    printf("Data of student %d is %d\n",i+1,start->marks);
        i++;
        start=start->next;
    while(start!=head){
        printf("Data of student %d is %d\n",i+1,start->marks);
        i++;
        start=start->next;
        
    }
    count=i;
}


struct student* insertathead(struct student*head,int data){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    
        while(temp->next!=head){
            temp=temp->next;
        }
        newnode->marks=data;
        temp->next=newnode;
        newnode->next=head;
        head=newnode;
        return head;
    
}


struct student* insertattail(struct student*head,int data){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    
        while(temp->next!=head){
            temp=temp->next;
        }
        newnode->marks=data;
        temp->next=newnode;
        newnode->next=head;
        return head;
    
}




struct student* insertatindex(struct student*head,int index,int data){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    int i=0;
    while(i!=index-1){
        temp=temp->next;
        i++;
    }
    newnode->marks=data;
    newnode->next=temp->next;
    temp->next=newnode;
    return head;
    
}


struct student* deleteathead(struct student*head){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    newnode=head;
        while(temp->next!=head){
            temp=temp->next;
        }
        temp->next=newnode->next;
        head=temp->next;
        return head;
    
}


struct student* deleteattail(struct student*head){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    struct student*ptr=head;
        while(temp->next!=head){
            temp=temp->next;
        }
        newnode=temp;
        while(ptr->next->next!=head){
            ptr=ptr->next;
        }
        ptr->next=newnode->next;
        return head;
    
}


struct student* deleteatindex(struct student*head,int index){
    struct student*newnode=(struct student*)malloc(sizeof(struct student));
    struct student*temp=head;
    struct student*ptr=head;
    int i;
        while(i!=index-1){
            temp=temp->next;
            i++;
        }
        newnode=temp;
        while(i!=index-1){
            ptr=ptr->next;
            i++;
        }
        ptr->next=newnode->next;
        return head;
    
}


int main(){
struct student*s1,*s2,*s3,*s4;
s1=(struct student*)malloc(sizeof(struct student));
s2=(struct student*)malloc(sizeof(struct student));
s3=(struct student*)malloc(sizeof(struct student));
s4=(struct student*)malloc(sizeof(struct student));

s1->marks=85;
s1->next=s2;
s2->marks=78;
s2->next=s3;
s3->marks=88;
s3->next=s4;
s4->marks=95;
s4->next=s1;
display(s1);
s1=insertathead(s1,45);
printf("\nAfter Insertion At Head:-\n\n");
display(s1);
s1=insertattail(s1,55);
printf("\nAfter Insertion At Tail:-\n\n");
display(s1);
s1=insertatindex(s1,2,25);
printf("\nAfter Insertion At Index:-\n\n");
display(s1);
s1=deleteathead(s1);
printf("\nAfter Deletion At Head:-\n\n");
display(s1);
s1=deleteattail(s1);
printf("\nAfter Deletion At Tail:-\n\n");
display(s1);
s1=deleteatindex(s1,2);
printf("\nAfter Deletion At Index:-\n\n");
display(s1);
return 0;
}
 

Comments