Brijmohan lal Sahu - Facebook
ads
ads
Showing posts with label delete. Show all posts
Showing posts with label delete. Show all posts

Friday, October 31, 2014

Program to delete and insert an element in list



---------------------------------------------
/* Program to delete an element from list
*/
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,l,k,ins,temp,n,del,pos;
int num[50];
clrscr();

cout<<"\nEnter List Size(max 50):";
cin>>n;
cout<<"\nEnter elements :";
for(i=0;i<n;i++)
{
cin>>num[i];
}

clrscr();
cout<<"\nElements in list :";
for(i=0;i<n;i++)
{
cout<<" "<<num[i];
}
cout<<"\nSelect :\n1.for insert into list\n2.for delete from list :";
cin>>l;
switch(l)
{
case 1:

cout<<"\nEnter element :";
cin>>ins;
cout<<"\nPosition :";
cin>>pos;
pos=pos-1;
n=n+1;
for(i=pos;i<n;i++)
{ temp=num[i];
  num[i]=ins;
  ins=temp;
}


break;

case 2:

cout<<"\nSelect \n1.To enter number for delete\n2.for position of number :";
cin>>j;
switch(j)
{
case 1:
cout<<"\nEnter element :";
cin>>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:
cout<<"\nPosition :";
cin>>pos;
pos=pos-1;
for(i=pos;i<n;i++)
{
num[i]=num[i+1];
}
break;

default :
cout<<"\nerror:check entry!!!!";
}
n=n-1;

break;
default :
cout<<"\nerror:check entry!!!!";

}

cout<<"\nElements in new list :";
for(i=0;i<n;i++)
{
cout<<" "<<num[i];
}

getch();
}