Data structures – Stack ADT: A stack is a linear data structure which follows LIFO (Last in first out) principle, in which both insertion and deletion occur at only one end of the list called the TOP.
OPERATIONS ON STACK :
The fundamental operations performed on stack are :
- Push (insertion)
- Pop (deletion)
The push operation is used to insert an element at the top of the stack. Pop is used to delete an element from the top of the stack.
Overflow: it is an attempt to insert an element when the stack is full .
Underflow: it is an attempt to delete an element when the stack is empty.
The general model is that there is some element that is at the top of the stack, and it is the only element that is visible.
IMPLEMENTATION OF STACK :
Since a stack is a list, any implementation will do. The two popular implementation performed are
- Linked list implementation of stacks.
- Array implementation of stacks.
Linked list implementation of stacks:
The first implementation of stack uses a singly linked list. Here PUSH and POP operations are performed at the TOP of the list.
Creating a stack :
Creating a stack is also simple. We merely create a header node, makeEmpty sets the next pointer to NULL.
Routine to check whether a stack is empty:
Routine to create an empty stack:
FatalError("Out of total space><><");
Error("It has to be CreateStack at first");
PUSH OPERATION :
The process of inserting a new element to the top of the stack. For any PUSH operation the top is incremented by ‘1’. They are also called input operations.
Routine to push onto a stack :
Push(ElementType K, Stack R)
FatalError("Out of the total storage");
A TOP operation examines the element at the front of the list , returning its value.
Routine to return top element in a stack:
Error("This is an Empty stack");
The process of deleting an element from the top of the stack is called POP operation. After every POP operation, the top pointer is decremented by ‘1’. They are also called output operations.,which delete the most recently inserted element.
Routine to pop from a stack:
Error("This is an Empty stack !!");