Stacks TS
Stacks TS
Stacks
Allow insertions and removals only at top of stack
Queues
Allow insertions at the back and removals from the front
Linked lists
Allow insertions and removals anywhere
Binary trees
High-speed searching and sorting of data and efficient
elimination of duplicate data items
Stacks
3
Introduction
Stack is an important data structure which stores its elements in an ordered
manner.
A stack is a linear data structure which uses the principle, i.e., the elements
in a stack are added and removed only from one end, which is called the
top.
4
Introduction
Real life examples of stack:
Suppose we have created stack of the book
How books are arranged in the stack?
Books are kept one above the other
Books which are inserted first is taken out last. (brown)
Book which is inserted lastly is served first. (light green)
Suppose at your home you have multiple chairs then you put
them together to form a vertical pile. From that vertical pile, the
chair which is placed last is always removed first.
Chair which was placed first will be removed
last.
5
Array representation of stacks
In computer’s memory stacks can be represented as a linear array.
Every stack has a variable TOP associated with it.
TOP is used to store the address of the topmost element of the
stack. It is this position from where the element will be added or
deleted.
There is another variable MAX which will be used to store the
maximum number of elements that the stack can hold.
If TOP = NULL, then it indicates that the stack is empty and if
TOP = MAX -1, then the stack is full.
6
Array representation of stacks
7
Operations of Stack
Push : inserting element onto stack.
Pop : removing element from stack.
Peep : returns the ith element from top element of the
stack.
Change : changes the ith element from top of stack to the
mentioned element.
8
Visual Representation of Stack
View 1: When stack is empty
When stack is empty then it does not contain any element inside it.
Whenever stack is empty, the position of topmost element is -1.
9
Visual Representation of Stack
View 2: When stack is not empty
Whenever we add very first element then topmost position will be increment
by 1. After adding first element, top = 0.
10
Visual Representation of Stack
Position of top and its value:
11
Push Operation
Procedure PUSH(S, TOP, X): This procedure inserts an
element X on the top of a stack which is represented by a
vector S containing N elements with a pointer TOP
denoting the top element in the stack.
12
Pop Operation
Procedure POP(S, TOP): This procedure removes the
element from the stack which is represented by a vector S
and returns the element.
13
Peek Operation
Procedure PEEP(S, TOP, I): Given the vector S
(consisting of N elements) representing a sequentially
allocated stack, and a variable TOP denoting the top
element of the stack, this function returns the value of the
ith element from the top of the stack. The element is not
deleted by this function.
14
Change Operation
Procedure CHANGE(S, TOP, X, I): Given the vector S
(consisting of N elements) representing a sequentially
allocated stack, and a pointer TOP denoting the top
element of the stack, this procedure changes the value of
the ith element from the top of the stack to the value
contained in X.
15
16
Applications of Stack
Expression conversion
Expression evaluation
Recursion
17
Expression conversion
Conversion from infix to postfix expression
Conversion from infix to prefix expression
Conversion from prefix to infix expression
Conversion from prefix to postfix expression
Conversion from postfix to prefix expression
Conversion from postfix to infix expression
18
Expressions
Expressions is a string of operands and operators. Operands are some
numeric values and operators are of two types: Unary and binary operators.
Unary operators are ‘+’ and ‘-’ and binary operators are ‘+’, ’-’, ‘*’, ‘/’ and
exponential. In general, there are three types of expressions:
Infix expression : operand1 operator operand2
Postfix expression : operand1 operand2 operator
Prefix expression : operator operand1 operand2
19
Conversion from infix to postfix (without
parenthesis)
20
Conversion from infix to postfix (with
parenthesis)
21
Conversion from infix to prefix (with
parenthesis)
Step 1: Reverse the infix expression and convert ‘(‘ to ‘)’ and ‘)’ to ‘(‘.
Step 2: Read this reversed expression from left to right one character at a
time.
Step 3: Rest of the steps remains same as in case of “conversion from infix
to postfix (with parenthesis)”.
Step 4: After all the elements are popped, reverse the expression obtained in
prefix expression.
22
Expression Evaluation
Evaluation of postfix expression
Evaluation of prefix expression
Evaluation of infix expression
23
Evaluation of postfix expression
Step 1: If char read from postfix expression is an operand, push operand to
stack.
Step 2: If char read from postfix expression is an operator, pop the first 2
operand in stack and implement the expression using the following
operations:
pop(opr1) then pop(opr2)
result = opr2 operator opr1
Step 3: Push the result of the evaluation to stack.
Step 4: Repeat steps 1 to steps 3 until end of postfix expression
Finally, At the end of the operation, only one value left in the stack. The
value is the result of postfix evaluation.
24
Evaluation of postfix expression
25
Evaluation of prefix expression
Step 1: Reverse the prefix expression.
Step 2: Read this reversed prefix expression from left to right one character
at a time.
Step 3: If char read from reversed prefix expression is an operand, push
operand to stack.
Step 4: If char read from reversed prefix expression is an operator, pop the
first 2 operand in stack and implement the expression using the following
operations:
pop(opr1) then pop(opr2)
result = opr1 operator opr2
Step 5: Push the result of the evaluation to stack.
Step 6: Repeat steps 3 to steps 5 until end of reversed prefix expression
Finally, At the end of the operation, only one value left in the stack. The
value is the result of prefix evaluation.
26
Recursion
27
Recursion
28
Recursion
Here, it is required to push the intermediate calculations till the terminal condition is
reached. In the above calculation for 5!, Steps 1 to 6 are the push operations. The
subsequent pop operations will evaluate the value of intermediate calculations till
the stack is exhausted.
Stack for parameter(s): To store the parameter with which the recursion is defined.
Stack for local variable(s): To hold the local variable that are used within the
definition.
Stack to store the return address.
29
Factorial with recursion using stack
30
Tower of Hanoi Problem
The Tower of Hanoi puzzle is solved by moving all the disks to
another tower by not violating the sequence of the arrangements.
Algorithm
Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from aux to dest
32
Example
33
THANK YOU!!
ANY QUESTIONS??
34