A stack is a non-primitive linear data structure. It is an ordered list in which addition and deletion is done from only one end known as top of stack (TOS).This is the reason why stack is also called last in first out (LIFO). An example of a stack is a pile of plates on a dining table. The last plate that is put on the pile is the first one to be used. Similarly, the element inserted last, will be the first to be deleted.
Terminology used to insert or delete an element from a stack. a) PUSH: Push is referred to insert an element into a stack. While pushing an element, stack may be full and no more elements can be pushed known as stack Overflow. b) POP: Pop is referred to delete an element from a stack. While deleting an element a situation may occur of no more insertion means stack contains no element, known as stack underflow.
Algorithm for push an element into the stack:
Step 1: Initialize
Set top = -1
Step 2: Repeat Step 3 to 5 until top < maxsize-1
Step 3: Read item
Step 4: set top = top + 1
Step 5: set stack [top] = item
Step 6: print "Stack Overflow"
Algorithm for pop an element from the stack:
Step 1: Repeat Step 2 to 4 until top ≥ 0
Step 2: set item = stack [top]
Step 3: set top = top – 1
Step 4: Print "Deleted Number is", item.
Step 5: Print "Stack Underflow".
1. Reversing Stack.
2. Copy one stack into another