[go: up one dir, main page]

0% found this document useful (0 votes)
8 views4 pages

Circular Linkedlist

The document contains a C program that implements a circular linked list with various operations such as creating a list, inserting and deleting nodes at different positions, and traversing the list. It provides a menu-driven interface for users to interact with the linked list. The program includes functions for each operation and uses a structure to define the nodes of the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Circular Linkedlist

The document contains a C program that implements a circular linked list with various operations such as creating a list, inserting and deleting nodes at different positions, and traversing the list. It provides a menu-driven interface for users to interact with the linked list. The program includes functions for each operation and uses a structure to define the nodes of the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
struct node
{
int info;
struct node *link;

};
struct node *last=NULL;
void main()
{
int choice;
while(1)
{
printf("\n1. create");
printf("\n2. insert_at_begin\n");
printf("\n3. insert_at_middle\n");
printf("\n4. insert_at_end\n");
printf("\n5. delete_at_begin\n");
printf("\n6. delete_at_middle\n");
printf("\n7. delete_at_end\n");
printf("\n8. traverse\n");
printf("\n9. exit\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;

case 2: insert_beg();
break;
case 3: insert_middle();
break;
case 4: insert_end();
break;
case 5: delete_beg();
break;
case 6:
delete_middle();
break;
case 7:
delete_last();
break;
case 8: traverse();
break;
case 9: exit(1);

}
void create()
{
struct node *temp;
int item;
char ch;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the data");
scanf("%d",&item);
temp->info=item;
temp->link=NULL;
if(last==NULL)
{
last=temp;
temp->link=last;
}
else
{
temp->link=last->link;
last->link=temp;
last=temp;
}
printf("Do u want to continue?");
ch=getche();
}while(ch=='y' || ch=='Y');
}
void traverse()
{

struct node *p;


if(last==NULL)
{
printf("No list exist");
return;
}
p=last->link;
while(p!=last)
{
printf("%d->",p->info);
p=p->link;

}
printf("%d",p->info);
}
void insert_beg()
{

struct node *temp;


int item;
temp=(struct node *)malloc(sizeof(struct node));
printf("ENter the data");
scanf("%d",&item);
temp->info=item;
temp->link=NULL;
temp->link=last->link;
last->link=temp;
}
void insert_middle()
{
int item,i;
int pos;
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("ENter the data");
scanf("%d",&item);
temp->info=item;
temp->link=NULL;
printf("Enter the position");
scanf("%d",&pos);
p=last->link;
for(i=1;i<pos-1;i++)
{
p=p->link;
}
temp->link=p->link;
p->link=temp;

void insert_end()
{
int item,i;
int pos;
struct node *temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("ENter the data");
scanf("%d",&item);
temp->info=item;
temp->link=NULL;

temp->link=last->link;
last->link=temp;
last=temp;
}

void delete_beg()
{
struct node *temp;
temp=last->link;
last->link=temp->link;
temp->link=NULL;
printf("Deleted item=%d",temp->info);
free(temp);

}
void delete_last()
{

struct node *temp,*p;


p=last;
while(p->link!=last)
{

p=p->link;
}
temp=p->link;
p->link=last->link;
last=p;
temp->link=NULL;
printf("Deleted item=%d",temp->info);
free(temp);
}

void delete_middle()
{
struct node *p,*temp;
int pos,i;
printf("Enter the position to delete");
scanf("%d",&pos);
p=last->link;
for(i=1;i<pos-1;i++)
{
p=p->link;
}
temp=p->link;
p->link=temp->link;
temp->link=NULL;
printf("deleted item=%d",temp->info);
free(temp);

You might also like