Welcome To Uday Satya Blog

19 . DELETE BETWEEN LL


/* ****************************************************
  19 . TO DELETE ELEMENT INBETWN TWO ELEMENTS
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
#include<*malloc.h>
void main()
{
struct link
{
int info;
struct link*ptr;
};
int ele;
struct link*head,*cur,*temp;
char ch='y';
clrscr();
head=(struct link*)malloc(sizeof(struct link));
printf("\n enter an head element : ");
scanf("%d",&head->info);
cur=head;
cur->ptr=NULL;
do
{
temp=(struct link*)malloc(sizeof(struct link));
printf("\n\n enter number to be inserted : ");
scanf("%d",&temp->info);
printf("\n\n do you want to insert more (yes,no) : ");
ch=getch();
temp->ptr=NULL;
cur->ptr=temp;
cur=cur->ptr;
}while(ch=='y');
cur=head;
printf("\n\n elements before deletion : ");
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
}
cur=head;
temp=cur->ptr;
printf("\n\n enter the element to be deleted : ");
scanf("%d",&ele);
while(temp->info!=ele&&cur!=NULL)
{
cur=cur->ptr;
temp=temp->ptr;
}
if(temp->info==ele)
{
if(temp->ptr==NULL)
cur->ptr=NULL;
else
cur->ptr=temp->ptr;
}
cur=head;
while(cur!=NULL)
{
printf("%d\t",cur->info);
cur=cur->ptr;
getch();
}
}

output

 enter an head element : 1

 enter number to be inserted : 2

 do you want to insert more (yes,no) :

 enter number to be inserted : 3

 do you want to insert more (yes,no) :

 elements before deletion : 1    2       3

 enter the element to be deleted : 2

 elements after deletion : 1       3

0 comments:

Post a Comment