Stack is an LIFO (Last In First Out) data structure.
Stack is a linear data structure where insertion and deletion operations are performed from one end called TOP. A TOP is a pointer which points to the top element- the element which entered last in the stack.
Stack can be implemented with an array and a linked list. Here, We will see the implementation of stack using array in C with basic operations on stack.
Basic Operations on Stack
Insertion of element in the stack is called as PUSH and Deletion of element from the stack is called POP operation.
PUSH(Stack, N, Item)
This algorithm will insert Item at top to Stack having size N. Here initial index for stack is 0.
if TOP >= N - 1 then
TOP = TOP + 1
Stack[TOP] = Item
PUSH Operation on Stack
This algorithm will delete the top element from the Stack having size N. Here initial index for stack is 0.
if TOP = -1 then
Stack[TOP] = NULL
TOP = TOP - 1
POP Operation on Stack
Implementation of Stack using array in C program with PUSH and POP operations :