You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>
int main()
{
void *ptr=malloc(8);// 8bytes allocated.
int cf=0;// char flag is set to 0.
int intf=0;//int flag is 0.
int ff=0;//flaot flag is 0.
int df=0;//double flag is 0.
while(1)
{
int choice;// which option u want.
//options below.
printf("1.Add element\n");
printf("2.Remove element\n");
printf("3.Display element\n");
printf("4.Exit from the program\n");
printf("Choice --->");
scanf("%d",&choice);
// add element.
if(choice==1)
{
int type;
printf("Enter the type you have to insert:\n");
printf("1.int\n");
printf("2.char\n");
printf("3.float\n");
printf("4.double\n");
printf("Choice --->");
scanf("%d",&type);
// integer adding
if((type==1)&&(intf==0)&&(df==0)&&(ff==0))// check if double ,float and int
is present or not.
{
printf("Enter the int :");
scanf("%d",((int*)(ptr+1)));
intf=1;// set int flag to 1;
}
// char adding
if((type==2)&&(cf==0))// if char is not there
{
printf("Enter the char :");
getchar();
scanf("%c",((char*)(ptr+0)));
cf=1;//set char falg.
}
//float adding
if((type==3)&&(ff==0)&&(intf==0)&&(df==0))// check for int,float,double
flag
{
printf("Enter the float :");
scanf("%f",((float*)(ptr+1)));
ff=1;// set float flag.
}
//double adding
if((type==4)&&(ff==0)&&(intf==0)&&(df==0))//check for int,double,float.
{
printf("Enter the double :");
scanf("%lf",((double*)(ptr+0)));
df=1;// set double falg.
}
}
//remove the element
if(choice==2)
{
if(cf)
{
printf("0->%c\n",*((char*)(ptr+0)));
}
if(intf)
{
printf("1->%d\n",*((int*)(ptr+1)));
}
if(ff)
{
printf("1->%f\n",*((float*)(ptr+1)));
}
if(df)
{
printf("0->%lf\n",*((double*)(ptr+0)));
}
int index;
printf("Enter the index to be deleted\n");
scanf("%d",&index);// put the index value.
if(index==0)
{
printf("index 0 successfully deleted.\n");
if(cf==1)
{
cf=0;// set charflag to 0.
}
if(df==1)
{
df=0;// set double flag to 0.
}
}
if(index==1)
{
printf("index 1 successfully deleted.\n");
if(ff==1)
{
ff=0;// set float flag to 0.
}
if(intf==1)
{
intf=0;//set int flag to 0.
}
}

}
// display the element.
if(choice==3)
{
printf("------------------------------------------\n");
if(cf)
{
printf("0->%c(char)\n",*((char*)(ptr+0)));
}
if(intf)
{
printf("1->%d(int)\n",*((int*)(ptr+1)));
}
if(ff)
{
printf("1->%f(float)\n",*((float*)(ptr+1)));
}
if(df)
{
printf("0->%lf(double)\n",*((double*)(ptr+0)));
}
printf("-----------------------------------------\n");
}
if(choice==4)
{
break;
}
}
}

You might also like