Stacks
Stacks
Applications of Stack
•The simplest application of a stack is to reverse a word. You push a given word to stack - letter by
letter - and then pop letters from the stack.
•There are other uses also like:
•Parsing
•Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
Push Operations on Stack
Algorithm to push an item into
stack
1) IF TOP = MAX then Print
“Stack is full”;
2)Exit;
3)Otherwise TOP: = TOP + 1;
/*increment TOP*/
STACK (TOP):= ITEM; 3) End
of IF
4) Exit
Pop Operation from Stack
POP_STACK(STACK,TOP,ITEM)
Algorithm to pop an element
from stack.
Prefix notation (also known as "Polish notation"): Operators are written before their operands.
X+y -> +XY
Sr.No. Infix Prefix Postfix
Notation Notation Notation
3 a ∗ (b + ∗a+bc abc+∗
c)
5 (a + b) ∗ ∗+ab+ ab+cd
(c + d) cd +∗
6 ((a + b) ∗ - ∗ + a b ab+c∗
Operator Precedence
• When an operand is in between two different
operators, which operator will take the operand
first, is decided by the precedence of an
operator over others. For example −
Associativity describes the rule where operators with the same precedence appear in an expression. For example, in expression
a + b − c, both + and – have the same precedence, then which part of the expression will be evaluated first, is determined by
associativity of those operators. H
ere, both + and − are left associative, so the expression will be evaluated as (a + b) − c.
Precedence and associativity determines the order of evaluation of an expression. Following is an operator precedence and
associativity table (highest to lowest) −