You are on page 1of 6

Name- Rishabh Jha

Roll no.-1084
Class-cse-2a-g2

DSA LAB-8
LAB-8

Aim: To implement the concepts of Circular Linked List

Bloom's Taxonomy Level: BT1, BT2 and BT3

Q 1.Write a program to implement following functions to access and


traverse a Circular Linked List.
Print the following details in advance for user information.

Press -1 for adding an element at beginning.

Press -2 for adding an element at end.

Press 3- for printing all the element of CLL.

Press -4 for deleting an element at beginning.

Press -5 for deleting an element from end.

Press -6 for exit();

Code: #include<stdio.h>

#include<malloc.h>
#include<stdlib.h>

struct node{
int data;
struct node *next;
};

void display(struct node*); void linsert(struct node**); void finsert(struct node**); void
fdelete(struct node**); void ldelete(struct node**);

struct node* temp;

void main(){
struct node* first=NULL; int n=0;

printf("Press -1 for adding an element at beginning \n");


printf("Press -2 for adding an element at end \n");
printf("Press -3 for printing all the element of CLL \n");
printf("Press -4 for deleting an element at beginning \n");
printf("Press -5 for deleting an element from end \n");
printf("Press -6 for exit \n");
printf("Press -7 for adding an element at middle \n");
while(n!=6)
{
printf("Press the number = ");
scanf("%d",&n);

if(n==1){
finsert(&first);
}
else if(n==2){
linsert(&first);
}
else if(n==3){
display(first);
}
else if(n==4){
fdelete(&first);
}
else if(n==5){
ldelete(&first);
}
printf("\n");
}

void display(struct node *c){ struct node *first =c;

if(c != NULL){
do{
printf("%d ",c->data); c = c->next;
}while(c !=first);}
printf("\n");
}

void linsert(struct node** c){ int num=123;


printf("Enter the value = "); scanf("%d",&num);
struct node* temp=(struct node*)malloc(sizeof(struct node*));

struct node *last= *c;


//struct node* first = *c; temp->data=num;
//temp->next= *c;

if(*c==NULL){
temp->next=temp;
*c = temp; return;
}

temp->next= *c;

do{
last=last->next;
}while(last->next != *c ); last->next=temp;
}

void finsert(struct node** c){ int num=123;


printf("Enter the value = "); scanf("%d",&num);

struct node* temp=(struct node*)malloc(sizeof(struct node)); struct node *last= *c;


temp->data=num;

if(*c==NULL){
temp->next=temp;
*c = temp; return;
}
temp->next=*c;

do{
last=last->next;
}while(last->next != *c ); last->next=temp;
*c=temp;
}

void fdelete(struct node** c){ struct node *last= *c;

*c = (*c)->next;

do{
last=last->next;
}while(last->next->next != *c ); last->next = *c;
}

void ldelete(struct node** c){ struct node *temp = *c;


while(temp->next->next != *c){ temp=temp->next;
}
temp->next=*c;
}
OUTCOME:

You might also like