/*DATA STRUCTURE->INSERTION->>PUT NEW ELEMENT INTO A PERTICULAR POSITION OF AN ARRAY*/
#include<stdio.h>
int assign(int,int,int*);
void display(int*);
int shifting(int,int*);
int main()
{
int insert,pos,x,choice;
int arr[8]={3,5,6,7,8,9,0};//i have an array in which i would like to place element at certain position
do
{
printf("\nINSERT THE NO:");//elements to be inserted
scanf("%d",&insert);
printf("\nGIVE ME THE POS:");//position at which that element is to be inserted
scanf("%d",&pos);
printf("\nENTER THE CHOICE:");//enter the value of choice to mention weither u have to continue with insertion or not
scanf("%d",&choice);
switch(choice)//choose the case
{
case 1:
shifting(pos,arr);//no use of break;//i had not used the break...so by selecting option 1 ..shifting ,insertion & display all three will complete only by applying case 1....& not using break....
//I HAVE POS WHERE ELEMENT TO BE INSERTED
case 2:
assign(pos,insert,arr);
case 3:
display(arr);
break;
case 0:
exit(0);
//sleep(1);
}
}while(choice != 0);
return 0;
}
int shifting(int pos,int* arr)
{
int x=6;
--x;//no of element in array 0 to 5 .
for(;pos<=x;x--)//I WILL SHIFT ALL OTHER ELEMENTS BY 1 RIGHT...UPTO THAT POSITION WHERE NEW ELEMENT IS TO BE INSERTED...
{
arr[x+1]=arr[x];//MOVE POS OF ELEMENT 5 to 6//4 TO 5//3 TO 4//2 TO 3//1 TO 2//0 TO 1// SUPPOSE I HAVE TO PLACE NEW ELEMENT AT 0 POSITION
}
return pos;//AFTER SHIFTING ALL THE ELEMNTS...RETURN THE POSITION WHERE NEW ELEMENT IS TO BE INSERTED..I.E. 0 IN OUR CASE
}
int assign(int pos,int insert,int *arr)
{
arr[pos]=insert;//arr[x]=insert;///NOW INSERT OR PUT THAT ELEMENT AT THAT PERTICULAR POSITION/..
return 0;
}
void display(int* arr)
{
int x;
for(x=0;x<6;x++)
{
printf("\narr[%d]=%d",x,arr[x]);//NOW DISPLAY WHOLE ARRAY AFTER INSERTION OF NEW ELEMENT IN ARRAY//BY APLYING A LOOP 1 BY 1...