- A stack is a list of elements in which an element may be inserted or deleted only at one end, usually called the top of the stack.
- Stacks work on the principle of LIFO (last in, first out) or FILO(first in last out).
- The items can be added or removed only from the top position of the stack.
- All insertions and deletions operation take place at the same end i.e. at the top, so the last element added to the stack will be the first element removed from the stack.
- When a stack is created, the stack base remains fixed while the stack top changes as elements are added and removed.
- The most accessible element is the top and the least accessible element is the bottom of the stack.
Operations in Stack
- There are two basic operations associated with stacks are –
- Push : When a new data item is added/inserted at the top of a stack is called Push operation.
- Pop : When an existing data item is removed/deleted from the top of a stack is called Pop operation.
Representation of Stack
- A stack can be represented into two form – (i) As an Array form (ii) As a Linked List form
- When a stack is represented in the form of array, its size is restricted/fixed before the execution.This is called the size of the stack. When the number of new elements to be added should not exceed the given/maximum size of the stack. If we attempt to add new element beyond the maximum size, we will encounter a stack overflow condition. Similarly, we cannot remove/delete elements below the base size of the stack. If such is the case, we will reach a stack underflow condition.
- Stack is used by compilers to check for balancing of parentheses, brackets and braces in an expression.
- Stack is used to evaluate a postfix expression.
- Stack is used to convert an infix expression into postfix/prefix form.
- In recursion, all intermediate arguments and return values are stored on the processor’s stack during processing.
- During a function call, the return address and arguments are pushed onto a stack and on return they are popped off.
493 total views, 1 views today