Single circular linked list using ‘C’
A roundabout connected list is like a separately connected list, however the last hub's next pointer directs back toward the primary hub, making a round structure. The essential tasks for a roundabout connected list are:
1. Inclusion toward the start: Make another hub with the given worth and make it the new top of the rundown. Change the pointers of the new hub and the last hub to keep up with the roundabout construction.
2. Inclusion toward the end: Make another hub with the given worth and make it the following hub of the last hub. Change the pointers of the new hub and the last hub to keep up with the round structure.
3. Inclusion at a particular position: Navigate the rundown until you arrive at the ideal position (counting from 0), make another hub with the given worth, and change the pointers to incorporate the new hub at the predetermined position. Change the pointers of the new hub, the past hub, and the following hub to keep up with the roundabout design.
Program:-
#include
<stdio.h>
#include
<stdlib.h>
struct node
{
int data;
struct node*next;
}*head;
int main()
{
int n,data,choose,x;
printf("enter no of nodes u
want");
scanf("%d",&n);
create(n);
display();
printf("\nenter 1 to add node first or last\n");
printf("enter 2 to add node at any
position\n");
scanf("%d",&choose);
if(choose==1){
printf("enter data of first node");
scanf("%d",&data);
first(data);
}
else
{
printf("enter data of node");
scanf("%d",&data);
printf("enter position");
scanf("%d",&x);
middle(x,data);
}
display();
return 0;
}
void
create(int n)
{
struct node*c,*temp;
int i,data;
head=(struct node*)malloc(sizeof(struct
node));
if(head==NULL)
{
printf("unable to allocate
memory");
}
else
{
printf("enter data of node
1:");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
c=(struct
node*)malloc(sizeof(struct node));
if(c==NULL)
{
printf("unable to allocate
memory");
}else
{
printf("enter data");
scanf("%d",&data);
c->data=data;
c->next=NULL;
temp->next=c;
temp=c;
}
}
temp->next=head;
}
}
void
display()
{
struct node*temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
do{
printf("%d
-",temp->data);
temp=temp->next;
}
while(temp!=head);
printf("%d",temp->data);
}
}
void
first(int data)
{
struct node*c,*temp;
c=(struct node*)malloc(sizeof(struct
node));
c->data=data;
c->next=head;
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next= c;
head=c;
}
void
middle(int x,int data)
{
struct node*c,*temp;
int i;
c=(struct node*)malloc(sizeof(struct
node));
c->data=data;
temp=head;
for(i=2;i<=x-1;i++)
{
temp=temp->next;
}
c->next=temp->next;
temp->next=c;
}
Output:-
at end or first position
.png)
0 Comments