Stacks
Stacks
Stacks
Introduction
• Stack is an important data structure which stores its elements in
an ordered manner.
• Take an analogy of a pile of plates where one plate is placed on
top of the other. A plate can be removed only from the topmost
position. Hence, you can add and remove the plate only at/from
one position, that is, the topmost position.
Another plate will The topmost plate will
be added on top of be removed first
this plate
Stacks
• A stack is a linear data structure which uses the same principle,
i.e., the elements in a stack are added and removed only from
one end, which is called the top.
A B C D E
0 1 2 3 TOP = 4 5 6 7 8 9
A B C D E F
0 1 2 3 4 TOP =5 6 7 8 9
Pop Operation
• The pop operation is used to delete the topmost element from the
stack.
• However, before deleting the value, we must first check if
TOP=NULL, because if this is the case then it means the stack is
empty so no more deletions can further be done.
• If an attempt is made to delete a value from a stack that is already
empty, an UNDERFLOW message is printed.
A B C D E
0 1 2 3 TOP = 4 5 6 7 8 9
A B C D
0 1 2 TOP = 3 4 5 6 7
8 9
Peek Operation
• Peek is an operation that returns the value of the topmost
element of the stack without deleting it from the stack.
• However, the peep operation first checks if the stack is empty or
contains some elements.
• If TOP = NULL, then an appropriate message is printed else the
value is returned.
A B C D E
0 1 2 3 TOP = 4 5 6 7 8 9
1 7 3 4 2 6 5 X
TOP
Push Operation on a Linked Stack
Algorithm to PUSH an element in a linked stack
Step 1: Allocate memory for the new node and name it as New_Node
Step 2: SET New_Node->DATA = VAL
Step 3: IF TOP = NULL, then
SET New_Node->NEXT = NULL
SET TOP = New_Node
ELSE
SET New_node->NEXT = TOP
SET TOP = New_Node
[END OF IF]
Step 4: END
1 7 3 4 2 6 5 X
TOP
9 1 7 3 4 2 6 5 X
TOP
Pop Operation on a Linked Stack
Algorithm to POP an element from a stack
9 1 7 3 4 2 6 5 X
TOP
1 7 3 4 2 6 5 X
TOP
Applications of Stacks
• Reversing a list
• Parentheses checker
• Recursion
• Tower of Hanoi
Infix Notation
• Infix, Postfix and Prefix notations are three different but equivalent
notations of writing algebraic expressions.
• While writing an arithmetic expression using infix notation, the
operator is placed between the operands. For example, A+B; here,
plus operator is placed between the two operands A and B.
• Although it is easy to write expressions using infix notation,
computers find it difficult to parse as they need a lot of information
to evaluate the expression.
• Information is needed about operator precedence, associativity
rules, and brackets which overrides these rules.
• So, computers work more efficiently with expressions written using
prefix and postfix notations.
Postfix Notation
• Postfix notation was given by Jan Łukasiewicz who was a Polish
logician, mathematician, and philosopher. His aim was to develop
a parenthesis-free prefix notation (also known as Polish notation)
and a postfix notation which is better known as Reverse Polish
Notation or RPN.
• In postfix notation, the operator is placed after the operands. For
example, if an expression is written as A+B in infix notation, the
same expression can be written as AB+ in postfix notation.
• The order of evaluation of a postfix expression is always from left
to right.
Postfix Notation
• The expression (A + B) * C is written as:
A B C A B C
If there is only one ring, then simply move the ring from source to the destination
A B C
A B C A B C
A B C
A B C
A B C
A B C A B C
A B C
A B C A B C