Professional Documents
Culture Documents
• Example top = 2
Display - Algorithm
Array Representation of Stacks
• Here, size is 6
• Top is value 30 i.e index 2.
• The variable top change from 0 to size-1
• Stack is empty when top = -1.
• Stack is full when top = size-1.
Applications of stacks
• Recursion,
• Fibonacci series,
• Tower of Hanoi problem,
• Conversion of expressions,
• Evaluation of postfix expression,
• Iteration v/s recursion
Recursion
• The recursion is a process by which a function calls itself. We use
recursion to solve bigger problem into smaller sub-problems. One
thing we have to keep in mind, that if each sub-problem is following
same kind of patterns, then only we can use the recursive
approach.
• A recursive function has two different parts. The base case and the
recursive case. The base case is used to terminate the task of
recurring. If base case is not defined, then the function will recur
infinite number of times.
• Types of recursion
• There are two type of recursion
1.Direct Recursion
2.Indirect Recursion
Direct Recursion:
• If a function recursively calls itself( means the same function) then it is called
direct recursion.
• //example of direct recursion
void directRecusrion()
{
// code....
directRecsion();
// code...
}
Indirect Recursion
• If a function call itself with the help of other function. Means a function func1 call
another function func2 which again calls func1 then it is indirect recursion.
• // example of indirect recursion
void indirectRecursion1()
{
// code...
indirectRecursion2();
// code...
}
void indirectRecursion2()
{
// code...
indirectRecursion1();
// code...
}
calculating factorial of a number.
n! = n * (n–1)!
Base case is when n = 1, because if n = 1, the result will be 1 as 1! = 1.
Recursive case of the factorial function will call itself but with a smaller value of n,
factorial(n) = n × factorial (n–1)
From the above example, let us analyze the steps of
a recursive program.
START
Procedure Hanoi(disk, source, dest, temp)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, temp, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, temp, dest, source) // Step 3
END IF
END Procedure
STOP
Conversion of expressions
• Expression:
• The sequence of operators and operands that reduce to a single value after
evaluation is called an expression.
• Eg: a + b, +ab, ab+
• Representation of expression: Three ways are there-
Infix Expression: operand operator operand
Example: A + B
Prefix Expression : operator operand operand
Example: +A B
Postfix Expression : operand operand operator
Example: A B +
Conversion of expressions
• Infix Notation
• We write expression in infix notation, e.g. a - b + c, where operators are
used in-between operands.
• Prefix Notation
• In this notation, operator is prefixed to operands, i.e. operator is written
ahead of operands. For example, +ab. This is equivalent to its infix
notation a + b. Prefix notation is also known as Polish Notation.
• Postfix Notation
• This notation style is known as Reversed Polish Notation. In this
notation style, the operator is postfixed to the operands i.e., the
operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
following table briefly tries to show the
difference in all three notations
Sr.No. Infix Notation Prefix Notation Postfix Notation
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Precedence
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 −
is
• KL+MN*-OP^W*U/V/T*+Q+.
Evaluation of Arithmetic Expressions is based on the operator precedence and
associativity
Brackets/paranthesis: ( )
Exponentiation: ^
Multiply, Divide & modulo division: * , /, and %
Addition & subtraction: + and -
Input Stack
234*+ empty Push 2
34*+ 2 Push 3
4*+ 32 Push 4
*+ 432 Pop 4 and 3, and perform 4*3 = 12. Push 12 into
the stack.
25*+ 12 Push 2
5*+ 2 12 Push 5
*+ 5 2 12 Pop 5 and 2 from the stack and perform 5*2 = 10. Push
10 into the stack.
Through base case, where there will When the termination condition for
Termination
be no function call. the iterator ceases to be satisfied.
Used when code size needs to be Used when time complexity needs
Usage small, and time complexity is not an to be balanced against an expanded
issue. code size.
Functions:
Enqueue() : adding new element
Dequeue() : removing an element
Display(): traversing
Types of Queue
• Linear queue or ordinary queue
• Circular queue,
• Double ended queue,
• Priority queue.
Linear queue or ordinary queue
• In linear queue, data is add at one end called rear end
deleted at other side called front end.