Stack is a LIFO(Last in first out) structure. Stack can be represented using an one-dimensional array which can hold the elements of a stack. Another variable “top” is used to keep track of the index of the top most element.

Formally, a stack may be defined as follows:

 

typedef struct stack
{
  int data[SIZE];
  int top;

}

SIZE is a constant, maximum number of elements that can be stored.

Associated with the object stack there are several operations that are necessary:

    1. void initialize(stack *p): It initializes a stack as an empty stack, Initial value of top is set to -1.
    2. int empty(stack *p): Function checks whether the stack is empty. It return 0 or 1 depending on whether the stack is empty or not.
    3. int full(stack *p): This function checks whether the stack is full. A stack is said to be full when the associated array is completely filled up. Whenever the stack is full, top points to the last element of the array. This function returns 1 or 0 depending on whether the stack is full or not.
    4. int pop(stack *p): This function deletes topmost element from the stack and also returns it to the calling program. Deletion from an empty stack will cause underflow.

  1. void push(stack *p, int x): The function inserts the element x onto the stack pointed by P. Insertion will cause an overflow if the stack is full.