/*AN IMPLEMENTATION REPRESENTING THE STACK ...CREATION OF STACK & ALSO WORKING OF STACK..*/
//STACK WORKS ON FILO ALGORITHM...FIRST IN LAST OUT...ALGORITHM....//MEANS IF WE ARE PUSHING THE VALUES FROM TOP ..THEN WE //ALSO HAVE TO POP THE VALUES FROM TOP....//
//THIS IS JUST LIKE NO. OF PLATES ARE PLACED (PUT) ON THE TOP & TOP...& PICKED FROM THE TOP...//
count++;//TO DISPLAY THE VALUE ALSO ....DO NOT TOUCH THE OV ....MEANS DO NOT CHANGE THE VALUE OF OV...SO COUNT IS ++MENTED REPRESENTING THE POSITION OF STACK...IS ++MENTING...//
}
}
int pop(int* pstack)//TO POP THE VALUE FROM STACK...HERE IS NO NEED OF SHIFTING THE VALUES...BCZ..IF WE ARE PUTTING THE VALUES FROM BOTTOM ...THEN ALSO PICKING THE VALUES FROM BOTTOM,.,,,SO NO NEED OF SHIFTING THE VALUES LIKE QUEUE....
{ if(ov>0)//IF OV >0 MEANS THERE IS SOME VALUE TO POP IN THE STACK ...
{
ov--;//1ST --MENT THE OV ...WHICH WAS ++MENTED WHILE PUSHING THE VALUE INTO STACK TO POP THE VALUE FROM STACK FROM //THE BOTTOM....
printf("ov=%d pop[%d]=%d ",ov,*(pstack+ov),pstack+ov);//PRINT THE POPED VALUE... 5..4...3...2...1//1 BY ONE EVERY TIME WHEN WE MAKE A CALL TO POP FUNCTION....& OV WILL BE --MEANTED ....SO NOW TO PUSH THE VALUE INTO STACK...OV IS AGAIN ++MENTED...//
}
return ov;
}
void push(int* fstack)
{
int top;
printf("\nenter the top");//ENTER THE VALUE WHICH IS TO BE PUSHED INTO STACK ...FROM 0 TO MAX. VALUE OF OV...
//OV IS PRESENTING POSITION OF VALUE IN STACK OR U CAN SAY POINTER...//
scanf("%d",&top);
*(fstack+ov)=top;//PUT THE VALUE INTO STACK..AT ITS EMPTY POSITION...EVERY TIME ...BCZ...OV++ MENTING EVERYTIME...
printf("ov=%d top=%d add=%d ",ov,*(fstack+ov),(fstack+ov));//JUST FOR CHECK PRINT VALUE WHICH IS PUT ..
ov++;//OV IS ++MENTING TO PUT THE VALUES TO NEXT TO NEXT TO NEXT ..SO ON...POSTION OF STACK(POINTER).... //AT LAST AFTER PUTTING ALL THE VALUES ...OV WILL BE AT POSITION WHERE THERE IS NO VALUE IS PUSHED...//
fstack=malloc(sizeof(int*));//MALLOC TO int* POINTER WILL BE DONE ...POINTER ALWAYS TAKES 4 BYTES AFTER MALLOCING..//EITHER IT IS OF char*||void*||int*||struct node*...//
return fstack;
}
int main()
{
int* stack;int choice;
printf("\ncase 1:creating stack");
printf("\ncase 2:PUSH");//PRESENTING MENU FOR STACK....
printf("\ncase 3:POP");
printf("\ncase 4:DISPLAY");
printf("\ncase 5:EXIT");
int flag=1;
do
{
printf("\nenter the choice");
scanf("%d",&choice);//ENTER THE CHOICE WEITHER U WANT TO PUSH THE VALUE INTO STACK OR U WANT TO POP THE VALUE FROM STACK....
switch(choice)
{
case 1:
stack=create_stack();//CREATING THE STACK IS NOTHING BUT MEMORY IS ALLOCATED TO A POINTER IN WHICH VALUES WILL BE PUT ...||PUSHED OR POP...
if(stack && flag==1)
{
printf("\nstack created succesfully");//ONCE STACK IS CREATED ..SO THERE IS NO NEED TO CREATE IT AGAIN....//
flag=0;
}
else if(flag==0)
{
printf("\nstack is already created");
}
break;
case 2:
push(stack);//PUT THE VALUE TO THAT POINTER ..FROM 0 POSITION ...SUPPOSE I HAD PUT 5 VALUES FROM 0 TO 4 POSITION ...//
break;
case 3:
pop(stack);//PICK THE VALUE FROM STACK NAMED POINTER FROM 4TH POSITON..THEN FROM 3RD..POS/// SO ON......MEANS PICK THE VALUES FROM STACK ...FROM ADDRESS WHERE LAST VALUE WAS PUT...//
break;
case 4:
display(stack,ov);//DISPLAY ALL LEFT VALUES IN STACK FROM 0 TO MAX. VALUE..//