Hello friends,
Today we discuss about Circular Singly Linked List.
Create a circular singly linked list:
Function Create:
void create()
{
int i,num;
printf("\nHow many nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=temp=newnode;
}else{
temp->next=newnode;
newnode->next=head;
temp=temp->next;
}}}
Function Display:
void display()
{
struct node *disp=head;
while(disp->next!=head)
{
printf("%d->",disp->data);
disp=disp->next;
}
printf("%d->",disp->data);
}
Function Insert Node at Beginning:
void insert_beg()
{
struct node *p=head;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
while(p->next!=head)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
head=newnode;
}
Function Insert Node at specific position or insert inbetween:
void specific()
{
int i,pos;
struct node *posi=head;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
printf("\nEnter position:");
scanf("%d",&pos);
if(pos==1)
{
printf("\nUse Insert at Beginning Function");
}
for(i=1; i<pos-1; i++)
{ posi=posi->next;}
newnode->next=posi->next;
posi->next=newnode;
}
Function Insert Node at last Position:
void last()
{
struct node *last=head;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
while(last->next!=head)
{last=last->next;}
last->next=newnode;
newnode->next=head;
}
Full Program For Circular Singly Linked List Insertion:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *newnode,*head,*temp;
void create()
{
int i,num;
printf("\nHow many nodes you want to create:");
scanf("%d",&num);
for(i=1; i<=num; i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
if(head==NULL)
{
head=temp=newnode;
}else{
temp->next=newnode;
newnode->next=head;
temp=temp->next;
}}}
void insert_beg()
{
struct node *p=head;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
while(p->next!=head)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
head=newnode;
}
void specific()
{
int i,pos;
struct node *posi=head;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
printf("\nEnter position:");
scanf("%d",&pos);
if(pos==1)
{
printf("\nUse Insert at Beginning Function");
}
for(i=1; i<pos-1; i++)
{ posi=posi->next;}
newnode->next=posi->next;
posi->next=newnode;
}
void last()
{
struct node *last=head;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&newnode->data);
while(last->next!=head)
{last=last->next;}
last->next=newnode;
newnode->next=head;
}
void display()
{
struct node *disp=head;
while(disp->next!=head)
{
printf("%d->",disp->data);
disp=disp->next;
}
printf("%d->",disp->data);
}
void main()
{
create();
insert_beg();
specific();
last();
display();
getch();
}
For Execute Program Properly use Full Program.
No comments:
Post a Comment