//Insert and delete any number from array
#include<stdio.h>
#include<conio.h>
#define SIZE 10
int ch,top=-1,array[SIZE],elem;
void main()
{
clrscr();
while(1)
{
printf("\n*** Array Menu ***\n 1. Insert\n 2. Delete\n 3. Display\n 4. Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit();
default:
printf("You are out of choice!");
}
}
}
int ins()
{
int i,ch1,num,data;
printf("*** Array Insertion Option ***\n 1. At Begining\n 2. In Between\n 3. At End\n");
printf("Enter your choice ");
scanf("%d",&ch1);
if(top==SIZE-1)
printf("Array is full!");
else
{
switch(ch1)
{
case 1:
printf("Enter the item to be inserted into the array at begining");
scanf("%d",&elem);
for(i=SIZE-1;i>=0;i--)
array[i+1]=array[i];
top++;
array[0]=elem;
break;
case 2:
printf("Where do you want to insert an element ");
scanf("%d",&num);
while(num<0||num>SIZE); //ask until num is in range
printf("Enter data to insert");
scanf("%d",&data);
top++;
for(i=top;i>num-1;i--)
array[i+1]=array[i]; //shift
array[num]=data; //insert
break;
case 3:
printf("Enter the element at the end of array ");
scanf("%d",&num);
for(i=0;i<=SIZE-1;i++)
array[i-1]=array[i];
array[SIZE-1]=num;
top++;
break;
}
}
return 0;
}
int del()
{
int i,ch,num,data;
printf("*** Array Deletion Option ***\n 1. At Begining\n 2. In Between\n 3. At End\n");
printf("Enter your choice ");
scanf("%d",&ch);
if(top==-1)
printf("Array is empty!");
else
{
switch(ch)
{
case 1:
top--;
array[top]=top;
break;
case 2:
printf("Where do you want to delete an element ");
scanf("%d",&num);
while(num<0||num>SIZE);
printf("Enter data to be deleted");
scanf("%d",&data);
top--;
for(i=top;i>num-1;i--)
array[i+1]=array[i];
array[num]=data;
break;
case 3:
array[SIZE-1]=num;
break;
}
}
return 0;
}
int display()
{
int i;
if(top==-1)
printf("Array is empty!");
else
{
printf("Array displays\n");
for(i=0;i<=SIZE-1;i++)
{
printf("%3d",array[i]);
}
}
return 0;
}
Tags
MCA