Starting Vocational Training From 1-May-2024 Get Detail
Write a program to insert, delete, display and search operation with array
#include<iostream.h>
#include<conio.h>
#include<process.h>
int cur=-1;
const int size=5;
int arr[size];
void menu()
{
cout<<"
1. Insert element at Last";
cout<<"
2. Insert at beginning";
cout<<"
3. Insert Element in sorted order";
cout<<"
4. Delete By Position";
cout<<"
5. Delete By Value";
cout<<"
8. Display All Elements";
cout<<"
9. Exit";
}
void insertSort()
{
if(cur==size-1)
{
cout<<"
Array already full";
}
else
{
int x,i;
cout<<"
Enter any value";
cin>>x;
for(i=cur;i>=0;i--)
{
if(arr[i]>x)
arr[i+1]=arr[i];
else
break;
}
arr[i+1]=x;
cur=cur+1;
}
}
void insertLast()
{
if(cur==size-1)
{
cout<<"
No Space to enter new values";
}
else
{
cur=cur+1;
cout<<"
Enter any value";
cin>>arr[cur];
}
}
void insertFirst()
{
int i;
if(cur==size-1)
{
cout<<"
No Space";
}
else
{
for(int i=cur;i>=0;i--)
{
arr[i+1]=arr[i];
}
cout<<"Enter any value";
cin>>arr[0];
cur=cur+1;
}
}
void displayList()
{
int i;
cout<<"
";
for(i=0;i<=cur;i++)
{
cout<<arr[i]<<" ";
}
cout<<"
";
}
void deletePosition()
{
if(cur==-1)
cout<<"
Array already empty";
else
{
int x;
cout<<"
Enter any position";
cin>>x;
if(x-1>cur)
cout<<"
Invalid Position Given";
else
{
cout<<"
"<<arr[x-1]<<" has been deleted";
for(int i=x-1;i<=cur;i++)
{
arr[i]=arr[i+1];
}
cur=cur-1;
}
}
}
int searchElement(int y)
{
int i,flag=-1;
for(i=0;i<=cur;i++)
{
if(arr[i]==y)
{
flag=i;
break;
}
}
return(flag);
}
void deleteValue()
{
if(cur==-1)
cout<<"
Array already empty";
else
{
int x,pos;
cout<<"
Enter any value to be delete";
cin>>x;
pos= searchElement(x);
if(pos==-1)
{
cout<<"
Element Not Present in My List";
}
else
{
cout<<"
"<<arr[pos]<<" has been deleted";
for(int i=pos;i<=cur;i++)
{
arr[i]=arr[i+1];
}
cur=cur-1;
}
}
}
void main()
{
int ch;
clrscr();
while(1)
{
menu();
cout<<"
Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
insertLast();
break;
case 2:
insertFirst();
break;
case 3:
insertSort();
break;
case 4:
deletePosition();
break;
case 5:
deleteValue();
break;
case 8:
displayList();
break;
case 9:
exit(0);
}
}
}