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
SIZE is a constant, maximum number of elements that can be stored.
Associated with the object stack there are several operations that are necessary:
- void initialize(stack *p): It initializes a stack as an empty stack, Initial value of top is set to -1.
- 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.
- 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.
- 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.
- 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.