Hi soumava,
there is nothing complexity u feel about stacks
it's a simple mechanism. it follows LIFO(last in first out) . i wrote a program,which is very simple logic. it did't check on c editor if u have found any errors correct urself. if u unable to clear errors post reply.
here is a simple logic like the following steps
1. suppose we take bundle of books which are arranged one has on another top(here it is stack)
2. if u want take one book from there,i.e., u must take first book,which is coming late and which was on top (here poping element from stack)
3.if another one wants to put another book on the bundle of books, the they must put that book on the top of bundle(here pushing element)
i send u a code u concentrate on it care fully. i hope it will help
CODE
#include<stdio.h>
#include<conio.h>
//structure for stack
struct stack
{
int stack[20];
int top=-1;
int bottom=0;
}
//fuction to check the stack is empty or not
int IsEmpty(struct *stack)
{
if(top==-1 || top==bottom) return 1;
else return 0;
}
//function to check the stack is overflow or not
int IsOverflow(struct *stack)
{
if(top==20) return 1;
else return 0;
}
//function to push the element into stack
push(struct *stack, int element)
{
if(IsOverflow(stack)==1)
{
printf("The given stack is over flow please try again...");
}
else
{
stack[++top]=element;
printf("%d Element was pushed into stack..",stack[top]);
}
}
//function to pop the element from the stack
pop(struct *stack)
{
if(IsEmpty(stack)==1)
{
printf("Sorry Your stack has no elemnts to pop..");
}
else
{
printf("The element %d was Poped",stack[top--]);
}
}
//function to display stack elements
display(struct *s)
{
int i=0;
while(i<=top)
printf("%d",stack[i++]);
}
main()
{
struct stack stack;
//push elements to your stack like
push(stack,5);
.....etc
//pop elements as like
pop(stack);//it poped 5 value from stack
...etc.
display(stack);//to display elements
}