DSA_Unit3_Updated
DSA_Unit3_Updated
DATA STRUCTURES
AND ALGORITHMS
Unit-
3
Operations on Stack ADT – Create, Push, Pop, Top; Implementation of
Stack ADT – Array and Linked; Applications - Infix to Postfix Conversion,
Postfix Evaluation, Balancing symbols, Function Calls, Tower of Hanoi;
Operations on Queue ADT - Create, Enqueue and Dequeue; Implementation
of Queue ADT – Array and Linked; Types of Queue - Circular, Double
ended and Priority Queue, Applications – Scheduling
Syllabus
• S1- Stack ADT, Stack - Array Implementation
• S2- Stack Linked List Implementation, Applications of Stack-
Infix to Postfix Conversion
• S3- Applications of Stack-Postfix Evaluation, Balancing
symbols
• S6- Applications of Stack- Nested Function Calls,
Recursion concept using stack
• S7- Tower of Hanoi, Queue ADT
• S8- Queue implementation using array and Linked List
• S11- Circular Queue and implementation
• S12- Applications of Queue and double ended queue
• S13- Priority Queue and its applications
SESSION
1
STACK ADT
• Abstract Data Type (ADT)
Diagram Reference:https://www.codesdope.com/course/data-structures-stacks/
• Always new items are added at top of the stack and also
removed from the top of the stack only.
• Easy to implement
• Create fixed size one dimensional array
– insert or delete the elements into the
array using LIFO principle using the variable
'top‘
Stack - top
• About top
• The stack implemented using linked list can work for the
variable size of data. So, there is no need to fix the size at
the beginning of the implementation
Stack – Linked list
• Dynamic Memory allocation of Linked list is
followed
• The nodes are scattered and non-
contiguously in the memory
• Each node contains a pointer to its immediate
successor node in the stack
• Stack is said to be overflown if the space left
in the memory heap is not enough to create a
node
Stack – Linked list
• In linked list implementation of a stack, every new
element is inserted as 'top' element.
• That means every newly inserted element is pointed by
'top'.
• To remove an element from the stack, simply remove the
node which is pointed by 'top' by moving 'top' to its
previous node in the list.
• The field of the First element must be
always NULL.
next
Example Stack – Linked List
(A + B) / D /+ABD AB+D/
(A + B) / (D + E) /+AB+DE AB+DE+/
(A - B / C + E)/(A + B) / + - A / B C E + A B ABC/-E+AB+/
B ^2 - 4 * A * C -^B2**4AC B2^4A*C*-
Why postfix representation of the
expression?
• Infix expressions are readable and solvable by humans
because of easily distinguishable order of operators, but
compiler doesn't have integrated order of operators.
a + b *c +d
The expression is
(A+B) + (C-D) Yes having balanced
symbol
1. https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm
2. https://www.codesdope.com/course/data-structures-stacks/
3. http://www.firmcodes.com/write-a-c-program-to-implement-a-stack-using-an-array-and-
linked-list/
4. http://www.btechsmartclass.com/data_structures/stack-using-linked-list.html
5. http://www.exploredatabase.com/2018/01/stack-abstract-data-type-data-structure.html
6. https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/
7. https://www.hackerearth.com/practice/notes/stacks-and-queues/
8. https://github.com/niinpatel/Parenthesis-Matching-with-Reduce-Method
9. https://www.studytonight.com/data-structures/stack-data-structure
10. https://www.programming9.com/programs/c-programs/230-c-program-to-convert-infix-to-
postfix-expression-using-stack
SESSION
6
Applications of Stack: Function Call
and Return
• 2 types of Memory
– Stack
– Heap
• Stack memory stores the data (variables) related
to each function.
• Why is it called stack memory?
– The last function to be called is the first to return (LIFO)
– The data related to a function are pushed into the stack
when a function is called and popped when the
function returns
Function Calls
• Function calls another function
->We are executing function A. In the course
of its execution, function A calls another function
B. Function B in turn calls another function C,
which calls function D.
Function A
Function B
Function C
Function D
• When A calls B, A is pushed on top of the system stack. When
the
execution of B is complete, the system control will remove A
from
the stack and continue with its execution.
• When B calls C, B is pushed on top of the system stack. When
the
execution of C is complete, the system control will remove B from
the stack and continue with its execution.
• When C calls D, C is pushed on top of the system stack. When
the
execution of D is complete, the system control will remove C from
the stack and continue with its execution.
• When D calls E, D is pushed on top of the system stack.
When the
execution of E is complete, the system control will remove D
from
the stack and continue with its execution.
• When E has executed, D will be removed for execution.
• When C has executed, B will be removed for execution.
• When D has executed, C will be removed for execution.
• When B has executed, A will be removed for execution.
Nested Function Calls
• Consider the code snippet below:
main() foo() bar()
{ { {
... ... ...
foo(); bar(); }
...
bar(); }
}
Nested Function Calls and Returns in
Stack Memory
• Stack memory when the code
executes:
• Factorial
program
int
fact(4)
; Image Source: https://stackoverflow.com/questions/19865503/can-recursion-be-named-as-a-simple-function-call
Example : Factorial Of a Number
To calculate n!, we multiply the number with factorial of the
number that is 1 less than that number.
n! = n * (n–1)!
5! = 5 * 4! Where 4! = 4 * 3!
= 5 * 4 * 3! Where 3! = 3 * 2!
= 5 * 4 * 3 * 2! Where 2! = 2*1!
= 5 * 4 * 3 * 2 * 1! Where 1! = 1
=5*4*3*2*1
= 120
Base case and Recursive Case
• Base case
When n = 1, because if n = 1, the result will
be 1 as 1! = 1.
• Recursive case
Factorial function will call itself but with a
smaller value of n, this case can be given
as
factorial(n) = n × factorial (n–1)
Advantages of using a recursive
program
• Recursive solutions often tend to be
shorter and simpler than non-recursive ones.
• Code is clearer and easier to use.
• Recursion works similar to the original formula
to solve a problem.
• Recursion follows a divide and
conquer technique to solve problems.
• In some (limited) instances, recursion may be
more efficient.
Drawbacks/Disadvantages of using a
recursive program
• For some programmers and readers, recursion is a difficult
concept.
• Recursion is implemented using system stack. If the stack
space on the system is limited,
• Recursion to a deeper level will be difficult to implement.
• Aborting a recursive program in midstream can be a very
slow process.
• Using a recursive function takes more memory and time to
execute as compared to its nonrecursive counterpart.
• It is difficult to find bugs, particularly while using
global variables.
Factorial using recursion
Towers of Hanoi
• Figure below shows three rings mounted on pole
A. The problem is to move all these rings from
pole A to pole C while maintaining the same
order. The main issue is that the smaller disk
must always come above the larger disk
A-> Source pole
B-> Spare pole
C-> Destination pole
• To transfer all the three rings from A to C, we will
first shift the upper
two rings (n–1 rings) from the source pole to the
spare pole.
1) Source pole -> Destination pole
2) Source pole -> Spare pole
3) Destination pole -> Spare pole
• n–1 rings have been removed from pole A, the nth
ring can be easily moved from the source pole
(A) to the destination pole (C).
4) Source pole -> Destination pole
• The final step is to move the n–1 rings from the spare
pole (B) to
the destination pole (C).
5) Spare pole -> Source pole
6) Spare pole -> Destination pole
7)Source pole -> Destination
pole Base case: if n=1
Move the ring from A to C using B as
spare Recursive case:
Move n – 1 rings from A to B
using C as spare Move the one ring left on A
to C using B as
spare
Move n – 1 rings from B to C
using A as spare
Step by Step Illustration
Code Snippet
• Function Call:
move(n,'A', 'C', 'B');
• Function Definition:
Session
7
The Queue ADT
• With a queue, insertion is done at one end
whereas deletion is performed at the other
end.
• It is a First In First Out (FIFO) Structure. That is,
the first element to enter into the queue will
be the first element to get out of the queue.
https://www.tutorialride.com/data-structures/queue-in-data-
structure.htm
• No more insertion is possible (since rear
cannot be incremented anymore) even though
2 spaces are free.
• Workaround: Circular Queue, Shifting
elements after each dequeue
Linked List Implementation of Queue
Fro
nt