SINGLY Linked List | Data Structure | C Program
Singly link list :
Code:
INSERT AT BEGINNING:
in_beg()
{
struct node *temp;
int item;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter a value :\n");
scanf("%d",&item);
temp->info=item;
temp->link=start;
start=temp;
}
INSERT AT GIVEN POSITION:
void de_mid()
{
struct node *temp1,*temp2;
int i,pos;
if(start==NULL)
printf("List is empty!!\n");
else
{
temp1=start;
printf("Enter the postion:\t");
scanf("%d",&pos);
for(i=1;i<=pos-1;i++)
temp2=temp1;
temp1=temp1->link;
temp2->link=temp1->link;
free(temp1);
}
}
INSERT AT END:
void in_end()
{
struct node *temp1,*temp2;
int item;
temp1=(struct node*)malloc(sizeof(struct node));
printf("Enter a number:\t");
scanf("%d",&item);
temp2=start;
temp1->info=item;
temp1->link=NULL;
if(start==NULL)
start=temp1;
else
{
while(temp2->link!=NULL)
temp2=temp2->link;
temp2->link=temp1;
}
}
DELETE AT BEGIN:
void de()
{
struct node *temp;
if(start==NULL)
printf("List empty!!\n");
else
{
temp=start;
start=start->link;
free(temp);
}
}
DELETE AT GIVEN POSITION:
void de_mid()
{
struct node *temp1,*temp2;
int i,pos;
if(start==NULL)
printf("List is empty!!\n");
else
{
temp1=start;
printf("Enter the postion:\t");
scanf("%d",&pos);
for(i=1;i<=pos-1;i++)
{ temp2=temp1;
temp1=temp1->link;
}
temp2->link=temp1->link;
free(temp1);
}
}
DELETE AT END:
void de_end()
{
struct node *temp1,*temp2;
if(start==NULL)
printf("List is empty!!\n");
else
{
temp1=start;
while(temp1->link!=NULL)
{
temp2=temp1;
temp1=temp1->link;
}
temp2->link=NULL;
free(temp1);
}
}
DISPLAY:
void di()
{
struct node *temp;
if(start==NULL)
printf("Linked list is empty!\n");
else
{
temp=start;
printf("The elements are:\n");
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}
---------------------------------------------------------
Data structure : Circular linklist
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)