In this program we gonna use all case of insertions in a singly linked list. In our previous programs , we do the same but separately here we gonna do all together.
/*Pre-Processors used throughout the program*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
/*structure of the node */
struct node
{
int data;
struct node *ref;
};
/*Functions used throughout the program*/
void create(struct node **);
void display(struct node *);
void addfirst(struct node **);
void addlast(struct node **);
void addmid(struct node **);
void main()
{
struct node *head=NULL;
int i,num;
clrscr();
printf("\nEnter no. of elements for your node:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
addfirst(&head);
addlast(&head);
addmid(&head);
display(head);
getch();
}
void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&r->data);
r->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
}
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("\nOUTPUT:- %d",p->data);
p=p->ref;
}
}
void addfirst(struct node **q)
{
struct node *temp, *r;
temp=*q;
r=(struct node *)malloc(sizeof(struct node));
printf("\n\nEnter value for 1st element:- ");
scanf("%d",&r->data);
r->ref=temp;
*q=r;
}
void addlast(struct node **q)
{
struct node *temp, *l;
temp=*q;
l=(struct node *)malloc(sizeof(struct node));
printf("\n\nEnter value for last element:- ");
scanf("%d",&l->data);
l->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=l;
}
void addmid(struct node **q)
{
int i,position;
struct node *temp, *r;
r=(struct node *)malloc(sizeof(struct node));
if(r==NULL)
{
printf("\nUnable to allocate memory.");
}
else
{
printf("\nEnter the position:- ");
scanf("%d",&position);
printf("\n\nEnter value to be inserted at mid:- ");
scanf("%d",&r->data);
r->ref=NULL;
temp=*q;
for(i=2;i<=position-1;i++)
{
temp=temp->ref;
if(temp==NULL)
break;
}
if(temp!=NULL)
{
r->ref=temp->ref;
temp->ref=r;
printf("\nDATA INSERTED SUCCESSFULL");
}
else
{
printf("\nUNABLE TO INSERT DATA");
}
}
}
/*Pre-Processors used throughout the program*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
/*structure of the node */
struct node
{
int data;
struct node *ref;
};
/*Functions used throughout the program*/
void create(struct node **);
void display(struct node *);
void addfirst(struct node **);
void addlast(struct node **);
void addmid(struct node **);
void main()
{
struct node *head=NULL;
int i,num;
clrscr();
printf("\nEnter no. of elements for your node:- ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
create(&head);
}
addfirst(&head);
addlast(&head);
addmid(&head);
display(head);
getch();
}
void create(struct node **q)
{
struct node *temp, *r;
temp=*q;
if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&temp->data);
temp->ref=NULL;
*q=temp;
}
else
{
r=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value:- ");
scanf("%d",&r->data);
r->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=r;
}
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("\nOUTPUT:- %d",p->data);
p=p->ref;
}
}
void addfirst(struct node **q)
{
struct node *temp, *r;
temp=*q;
r=(struct node *)malloc(sizeof(struct node));
printf("\n\nEnter value for 1st element:- ");
scanf("%d",&r->data);
r->ref=temp;
*q=r;
}
void addlast(struct node **q)
{
struct node *temp, *l;
temp=*q;
l=(struct node *)malloc(sizeof(struct node));
printf("\n\nEnter value for last element:- ");
scanf("%d",&l->data);
l->ref=NULL;
while(temp->ref!=NULL)
{
temp=temp->ref;
}
temp->ref=l;
}
void addmid(struct node **q)
{
int i,position;
struct node *temp, *r;
r=(struct node *)malloc(sizeof(struct node));
if(r==NULL)
{
printf("\nUnable to allocate memory.");
}
else
{
printf("\nEnter the position:- ");
scanf("%d",&position);
printf("\n\nEnter value to be inserted at mid:- ");
scanf("%d",&r->data);
r->ref=NULL;
temp=*q;
for(i=2;i<=position-1;i++)
{
temp=temp->ref;
if(temp==NULL)
break;
}
if(temp!=NULL)
{
r->ref=temp->ref;
temp->ref=r;
printf("\nDATA INSERTED SUCCESSFULL");
}
else
{
printf("\nUNABLE TO INSERT DATA");
}
}
}
No comments:
Post a Comment