//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...//
#include<stdio.h>
#include<stdlib.h>
int push(int,int* );
int* create_stack(int*);
int ov=-1;
int pop(int*);
int push(int top,int* lstack)
{
if(ov<5)//IT LIMITS THE MAX. NO. OF VALUES THAT COULD BE PUSHED INTO STACK ACCORDING TO THE MEMORY OF STACK...
{
printf("\nenter the value");
scanf("%d",&top);//TAKE THE VALUE TO BE PUSHED FROM EXTERN COMMAND LINE...//
ov++;//++MENT OV EVERY TIME WHEN VALUE IS PUSHED INTO STACK;;;;//
lstack=lstack+ov;//by this func i can REplace func_ at diffrent memories in stack//lstack+ov is made to be lstack....to put the values into it...//
*lstack=top;//PUT THE VALUE OF TOP INTO lstack+ov....//which is now lstack...//
printf("add(%d)=%d ",*(lstack),lstack);//print that value...//
}
return top;
}
pop(int* lstack)
{
if(ov<=5 && ov>=0)//pop the value FROM THE STACK IF THERE IS ANY OR NO. OF VALUES ARE PUSHED INTO STACK..//OV CAL HAVE 0 TO MAX. 5 VALUES INTO IT..//
{
lstack=lstack+ov;//PRINT CURRENT VALUE OF lstack+ov ...OV IS --MENTING CONTINOUSLY...PRESENTING THE VALUES PICKED FROM TOP MEAns from 5 4 3 2 1 ...not stacked from 0....so no need to shift the values like a queue..//
printf("\npop[%d]=%d",*(lstack),lstack);
ov--;
}
return 0;
}
int* create_stack(int* lstack)
{
lstack=malloc(sizeof(int*));//4 byte of memory is provided to int*...bcz whatever be the sizeof any structre ...pointer always takes 4 bytes initially...//
return lstack;
}
int main()
{
int top=0;
int* stack;
stack=create_stack(stack);//create stack in which value is to be pushed. or poped.//
if(stack)
{
printf("\nstack created succesfully=%d",sizeof(stack));
}
top=push(top,stack);//add the values in this push...//send empty push variable from main to get value in top..in push function to push that value into stack..//
printf("\n1st top=%d",top);//print 1st value that is pushed
top=push(top,stack);//send 2nd value to push
printf("\n2nd top=%d",top);
top=push(top,stack);//3rd value is pushed
printf("\n3rd top=%d",top);
top=push(top,stack);//4th value is pushed...so on..
printf("\n4th top=%d",top);
top=push(top,stack);
printf("\n5th top=%d",top);
top=push(top,stack);
printf("\n6th top=%d",top);
while(ov>=0)//if stack have some value ..then make attempt to pop..untill all the values are poped...1 by 1..//