StackQueue MCH
StackQueue MCH
StackQueue MCH
Another plate
will be added on
The topmost plate
top of this plate
will be removed first
•In computer’s memory stacks can be represented as a
linear array.
•A variable TOP 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.
•Variable MAX used to store the maximum number of
elements that the stack can hold.
•If TOP = NULL, then the stack is empty and if TOP = MAX-
1, then the stack is full.
Used to insert an element in to the stack.
The new element is added at the topmost position of the stack.
If TOP=MAX-1, the stack is full and no more insertions possible, and an
OVERFLOW message is printed.
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4 5 6
0 1 2 3 4 TOP = 5 6 7 8 9
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4
0 1 2 TOP = 3 4 5 6 7 8 9
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
Here Peep operation will return 5, as it is the value of the topmost element of
the 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
Step 4: SET New_Node->NEXT = NULL
Step 5: SET TOP = New_Node
Step 6: ELSE
Step 7: SET New_node->NEXT = TOP
Step 8: SET TOP = New_Node
Step 9: [END OF IF]
Step 10: END
1 7 3 4 2 6 5 X
TOP
9 1 7 3 4 2 6 5 X
TOP, New_Node
Algorithm to POP an element from the stack
9 1 7 3 4 2 6 5 X
TOP, PTR
1 7 3 4 2 6 5 X
TOP
Applications
Conversion of Infix to Postfix Expression
Infix Notation: X + Y (Operators are written between their
operands)
Postfix Notation: X Y + (Operators are written after their
operands). Also known as Reverse Polish Notation.
Evaluation of a Postfix Expression
E.g. Expressions like 4 5 6 * + can be evaluated easily.
Recursion
A function calling itself again and again breaking a
problem down into smaller and smaller subproblems, until
we get a small enough problem that can be solved
trivially.
Infix to Postfix Conversion Using Stack
Infix Expression:
A+ (B*C-(D/E^F)*G)*H
Algorithm to convert Infix To Postfix
X be an input infix expression and Y the equivalent postfix output.
1. Push “(“onto Stack, and add “)” to the end of X.
2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack
is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
6. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which
has the same precedence as or higher precedence than operator.
7. Add operator to Stack.
8. End of If
9. If a right parenthesis is encountered ,then:
10. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a
left parenthesis is encountered.
11. Remove the left Parenthesis.
12. End of If
13. End of If
14. END.
Evaluating Postfix Expression using Stack
A() B()
{ {
…… ……
B() A()
…… ……
} }
Limitations of Recursion
Recursive algorithm should be logarithmic in their
execution.
Linear list (linked list) has O(n) efficiency. So, linked list is not
good for recursion.
Works best when algorithm uses a data structure that
supports recursion.
E.g.- Trees.
Runs slower than its non-recursive equivalent.
Number of recursive calls may lead to a possibility of out
of memory situation.
Iterative function uses linear time i.e. O(n), Recursive
function uses logarithmic time or exponential time, i.e.
O(log n) or O(kn).
Recursion vs. Iteration
Advantages of Iteration: Advantages of
Works well with linear Recursion:
data structure. Solution shorter, simpler,
Uses less memory. more understandable.
Handles large numbers. Does not use any loop.
Space requirement fixed Time to develop and
[recursion requires more remove bugs less than
storage space]. iteration.
Uses linear time, O(n) Trees naturally recursive.
[less than recursion]. Recursion works better
than iteration for fewer
number of recursive
calls.
Recursion Tree
First function call is assumed to be its root and all its
other nodes in sequence represent corresponding
recursive calls.
Each vertex/node assumed to be an instruction.
Leaves represent base case of recursion.
Key tool for analysis of recursive algorithm.
Memory and time requirement of recursive program
depend on height and size of recursion tree.
Total number of instructions handled by program
calculated from number of non-leaf vertices of tree.
Each recursive function calls the next level.
Recursion Tree - Examples
Recursion tree is a
chain if
If it is simple, linear and
has one vertex in each
level.
Each vertex has only one
child.
Each child corresponds
to one single recursive
call that occurs.
Transformation from
recursion to iteration
easy and saves time and
space.
Recursion Tree - Examples
Recursion tree with many
vertices:
Signify duplicate tasks.
Each vertex with more than
one child corresponds to a
recursive call.
One instruction printed for
each vertex in tree, except
for leaves.
Number of recursive calls in
a tree of n nodes is 2n –
1.
Sets up a stack to use while
traversing the tree.
Tail Recursion
Last executed statement of any recursive function
becomes a recursive call to that function.
Special type of recursion which guarantees that
nothing is left to be executed in the function after the
recursive call.
Recursive call should be the last statement and
there should be no earlier recursive calls, whether
direct or indirect.
Advantages:
Smart compilers can detect tail recursion and convert it to
iteration to optimize code.
Used to implement loops in languages that do not support
loop structures explicitly, e.g. – Prolog.
Tail Recursion-Example (Factorial of a
number)
int fact(int x) int fact(int x)
{ {
if(x==0) return fact_aux(x,1);
return 1; }
else
int fact_aux(int x, int rslt)
return x*fact(x-1);
{
}
if(x==1)
return rslt;
return fact_aux(x-
1,x*rslt);
}
Towers of Hanoi
64 disks of decreasing sizes from bottom to top.
3 towers, A(first), B(middle), C(end).
Moves all disks from tower A to C, following the
conditions below:
Only one disk can be moved at a time.
A larger disk must never be placed over smaller.
One tower can be used for intermediate storage of disks.
Towers of Hanoi
Algorithm
Hanoi(disk,source,dest,aux)
1. Begin
2. If disk=1, then
3. Move disk from source to dest
4. Else
5. Hanoi(disk-1,source,aux,dest) //Step 1
6. Move disk from source to dest //Step 2
7. Hanoi(disk-1,aux,dest,source) //Step 3
8. Endif
9. Return
10. End
QUEUES
Data structure which stores its elements in an ordered manner. Take for
example the analogies given below:
People moving on an escalator. The people who got on the escalator first
will be the first one to step out of it.
People waiting for bus. The first person standing in the line will be the first
one to get into the bus.
A queue is a FIFO (First In First Out) data structure in which the element that
was inserted first is the first one to be taken out.
The elements in a queue are added at one end called the rear and removed
from the other one end called front.
Every queue will have front and rear variables that will point
to the position from where deletions and insertions can be
done respectively.
Consider a queue shown in figure
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Here, front = -1 and rear = 6.
Now, deleting an element from the queue, the value of front will be incremented.
The queue after deletion will be as shown in figure
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Solutions:
1. Shifting elements to left so that the vacant space can be occupied and
utilized efficiently. Very time consuming (especially when the queue is
quite large).
2. Using a circular queue. In circular queue, the first index comes right
after the last index.
•During insertion, rear incremented as
Q[0]
Q[6] Q[1]
rear=(rear+1) mod MAX
Q[2]
•During deletion, front incremented as
Q[5]
front=(front+1) mod MAX
Q[4] Q[3] • After MAX-1, rear goes to 0, if space available.
If rear != MAX – 1, then the value will be inserted and rear will be incremented as
illustrated in figure
90 49 7 18 14 36 45 21
Front=0 1 2 3 4 5 6 7 rear= 8 9
If front!=0 and rear=MAX -1, then it means that the queue is not full. So, set rear =
0 and insert the new element there as shown in figure
7 18 14 36 45 21 99 72
0 1 Front=2 3 4 5 6 7 8 Rear=9
If the queue is not empty and after returning the value on front, if front =
MAX -1, then front is set to 0. This is shown in figure
72 63 9 18 27 39 81
0 1 2 3 4 rear= 5 6 7 8 front= 9
Algorithm to insert an element in circular queue
Step 1: INITIALIZE REAR to 0, FRONT to 0
Step 2: IF (FRONT=REAR+1) or (REAR=MAX-1 and FRONT=-1), then
Step 3: Write OVERFLOW, Go To Step 8
Step 4: ELSE
Step 5: SET REAR = (REAR+1)mod(MAX)
Step 6: SET QUEUE[REAR] = NUM
Step 7: END IF
Step 8: Exit
1 7 3 4 2 6 5 X
FRONT REAR
Insert 1 7 3 4 2 6 5 9 X
Operation REAR
FRONT
Delete 5
7 3 4 2 6 9 X
Operation
REAR
FRONT
Insertion and Deletion in Linked Queue
Algorithm to insert an element into a linked queue
Step 1: Allocate memory for the new node and name it as PTR
Step 2: SET PTR->DATA = VAL
Step 3: IF FRONT = NULL, then
Step 4: SET FRONT = REAR = PTR;
Step 5: SET FRONT->NEXT = REAR->NEXT = NULL
Step 6: ELSE
Step 7: SET REAR->NEXT = PTR
Step 8: SET REAR = PTR
Step 9: SET REAR->NEXT = NULL
Step 10: [END OF IF]
Step 11: END