Code Box
-------------------------------------------
/* Program to delete an element from list
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,l,k,ins,temp,n,del,pos;
int num[50];
clrscr();
printf("\nEnter List Size(max 50):");
scanf("%d",&n);
printf("\nEnter elements :");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
clrscr();
printf("\nElements in list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}
printf("\nSelect :\n1.for insert into list\n2.for delete from list :");
scanf("%d",&l);
switch(l)
{
case 1:
printf("\nEnter element :");
scanf("%d",&ins);
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
n=n+1;
for(i=pos;i<n;i++)
{ temp=num[i];
num[i]=ins;
ins=temp;
}
break;
case 2:
printf("\nSelect \n1.To enter number for delete\n2.for position of number :");
scanf("%d",&j);
switch(j)
{
case 1:
printf("\nEnter element :");
scanf("%d",&del);
for(i=0;i<n;i++)
{
if(num[i]==del)
{for(k=i+1;k<n;k++)
{
num[k-1]=num[k];
}
}
}
break;
case 2:
printf("\nPosition :");
scanf("%d",&pos);
pos=pos-1;
for(i=pos;i<n;i++)
{
num[i]=num[i+1];
}
break;
default :
printf("\nerror:check entry!!!!");
}
n=n-1;
break;
default :
printf("\nerror:check entry!!!!");
}
printf("\nElements in new list :");
for(i=0;i<n;i++)
{
printf(" %d",num[i]);
}
getch();
}
No comments:
Post a Comment