0% found this document useful (0 votes)
13 views

Circular Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Circular Linked List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include<stdio.

h>
#include<stdlib.h>

typedef struct nodetype


{
int info;
struct nodetype *next;
}node;

void insertleft(node**,node**);
void insertright(node**,node**);
void insertkey(node**,node**);
void insertpos(node**,node**);
void deletekey(node**,node**);
void deletepos(node**,node**);
void display(node*);
void display2(node *);
void count(node *);

int main()
{
int ch;
node *head=NULL,*tail=NULL;
do
{
printf("enter::\n1 to insert in right\n2 to insert in left\n3 to
insert by position\n4 to insert after a key\n5 to delete by key\n6 to
delete by position\n7 to display\n8 to display2\n9 to count nodes\n:::");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertright(&head,&tail);
break;

case 2:
insertleft(&head,&tail);
break;

case 3:
insertpos(&head,&tail);
break;

case 4:
insertkey(&head,&tail);
break;

case 5:
deletekey(&head,&tail);
break;

case 6:
deletepos(&head,&tail);
break;

case 7:
display(head);
break;

case 8:
display2(head);
break;

case 9:
count(head);
break;

default:
printf("wrong choice\n");
}
printf("enter 1 to continue:");
scanf("%d",&ch);
}while(ch==1);

return 0;
}

void insertright(node **head,node **tail)


{
node *temp=NULL;
int x;
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
{
printf("memory not allocated\n");
return;
}
else{
printf("enter the element=");
scanf("%d",&x);
temp->info=x;
if(*head==NULL)
{
temp->next=temp;
*head=*tail=temp;
}
else{
temp->next=*head;
(*tail)->next=temp;
*tail=temp;
}
}
}

void insertleft(node **head,node **tail)


{
node *temp=NULL;
int x;
temp=(node*)malloc(sizeof(node));
if(temp==NULL)
{
printf("memory not allocated\n");
return;
}
else
{
printf("enter the element=");
scanf("%d",&x);
temp->info=x;
if(*head==NULL)
{
temp->next=temp;
*head=*tail=temp;
}
else
{
temp->next=*head;
*head=temp;
(*tail)->next=*head;
}
}
}
void insertkey(node **head,node **tail)
{
node *temp=NULL,*new=NULL;
int key,x;
if(*head==NULL)
{
printf("linked list is empty\n");
}
else{
printf("enter the key:");
scanf("%d",&key);
temp=*head;
while(temp->info!=key&&temp->next!=*head)
{
temp=temp->next;
}
if(temp->next==*head&&temp->info!=key)
{
printf("key not found\n");
return;
}
else{
printf("key is founded\n");
printf("enter the element to insert");
scanf("%d",&x);
new=(node*)malloc(sizeof(node));
if(new==NULL)
{
printf("memory not allocated\n");
return;
}
else{
new->info=x;
if(temp->info==key&&temp->next==*head)
{
new->next=*head;
temp->next=new;
*tail=new;
}
else if(temp->info==key&&temp->next!=*head)
{
new->next=temp->next;
temp->next=new;
}
}
}
}
}

void insertpos(node **head,node **tail)


{
int pos,x,c=0;
node *tp=NULL,*tc=NULL,*new=NULL;
printf("enter the posotion:");
scanf("%d",&pos);
tc=*head;
while(tc->next!=*head)
{
c++;
tc=tc->next;
}
c++;
int t=c;
if(pos<=0||pos>c+1)
{
printf("position is invalid\n");
return;
}
else{
printf("enter the element:");
scanf("%d",&x);
new=(node*)malloc(sizeof(node*));
if(new==NULL)
{
printf("memory not allocated\n");
return;
}
else
{
new->info=x;
if(pos==1&&*head!=NULL)
{
new->next=*head;
*head=new;
(*tail)->next=*head;
}
else if(pos==1&&*head==NULL)
{
new->next=new;
*head=*tail=new;
}
else if(pos==t)
{
new->next=*head;
(*tail)->next=new;
*tail=new;
}
else{
c=1;
tp=*head;
tc=(*head)->next;
while(tc->next!=*head)
{
c++;
if(c==pos)
{
break;
}
tp=tc;
tc=tc->next;
}
new->next=tc;
tp->next=new;

}
}
}
}

void deletekey(node **head,node **tail)


{
int key;
node *tc=NULL,*tp=NULL;
if(*head==NULL)
{
printf("linked list is empty\n");
return;
}
printf("enter the key:");
scanf("%d",&key);
tc=*head;
while(tc->info!=key&&tc->next!=*head)
{
tp=tc;
tc=tc->next;
}
if(tc->info!=key&&tc->next==*head)
{
printf("key not founded\n");
return;
}
else{
printf("key is founded\n");
if(tc==*head)
{
*head=(*head)->next;
(*tail)->next=*head;
free(tc);
}

else if(tc==*tail)
{
tp->next=*head;
*tail=tp;
free(tc);
}

else
{
tp->next=tc->next;
free(tc);
}
}
}

void deletepos(node **head,node **tail)


{
int pos,c=0;
node *tc=NULL,*tp=NULL;
printf("enter the position");
scanf("%d",&pos);
tc=*head;
if(*head==NULL)
{
printf("linked list is empty\n");
return;
}
while(tc->next!=*head)
{
c++;
tc=tc->next;
}
c++;
if(pos<=0||pos>c)
{
printf("invalid poistion\n");
return;
}
else{
if(pos==1)
{
node *temp=*head;
*head=(*head)->next;
(*tail)->next=*head;
free(temp);

}
else if(pos==c)
{
tc=(*head)->next;
tp=tc;
while(tc->next!=*head)
{
tp=tc;
tc=tc->next;
}
tp->next=*head;
*tail=tp;
free(tc);
}

else if(pos>1&&pos<c){
int h=1;
tc=(*head)->next;
tp=tc;
while(tc!=NULL)
{
h++;
if(pos==h)
{
break;
}
tp=tc;
tc=tc->next;
}
tp->next=tc->next;
free(tc);
}
}
}

void display(node *head)


{
node *temp=NULL;
temp=head;
do{
printf("%d\t",temp->info);
temp=temp->next;
}while(temp!=head);

void display2(node *head)


{
node *temp=NULL;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
node *temp2=NULL;
temp2=temp;
do{
printf("%d\t",temp->info);
temp=temp->next;
}while(temp!=temp2);

void count(node *head)


{
node *temp=NULL;
temp=head;
int c=0;
do{
c++;
temp=temp->next;
}while(temp!=head);
printf("%d is the count\n",c);
}

You might also like