Data Structure Unit2 Ppt
Data Structure Unit2 Ppt
Greater Noida
Stack and Queues
Unit: 02
Subject Name
Data Structure Mrs. Nidhi Sharma
(AOE0662) NIET, Greater Noida.
Electronics and
Course Details
(B Tech 4th Sem) Communication
Engineering.
Research Interests
VLSI Design, Communication System, Internet of Things.
UNIT III: Basic terminology used with Tree, Binary Trees, Binary Tree Representation: Array Representation and Pointer (Linked List)
Representation, Binary Search Tree, Strictly Binary Tree, Complete Binary Tree, An Extended Binary Trees. Tree Traversal algorithms: In-order,
Pre-order and Post-order. Constructing Binary Tree from given Tree Traversal, Operation of Insertion, Deletion, Searching & Modification of data
in Binary Search tree, Introduction of Binary Heaps, Threaded Binary trees, Traversing Threaded Binary trees, AVL Tree, B-Tree.
UNIT IV: Graphs: Terminology used with Graph, Data Structure for Graph Representations: Adjacency
matrices, Adjacency List. Graph Traversal: Depth First Search and Breadth First Search. Connected
Component, Spanning Trees, Minimum Cost Spanning Trees: Prim’ s and Kruskal’s algorithm. Shortest Path
algorithms: Dijkstra Algorithm.
UNIT-V: Searching: Concept of Searching, Sequential search, Index Sequential Search, Binary Search. Concept of
Hashing. Sorting: Insertion Sort, Selection, Bubble Sort, Quick Sort, Merge Sort, Heap Sort and Radix Sort. File Structure:
Concepts of files, records and files, Sequential, Indexed and Random File Organization, indexing structure for index files,
multi-Key file organization and Access Methods.
Ms. Nidhi Sharma AOE0662 Data Stuctures Unit1
BRANCH WISE APPLICATION
When we talked about it is very vast majority
used today’s and the biggest example of it is
social media. People all around the world
connect with each other through social media.
From the origin of the primary programming
languages to the fashionable programming
languages presently in use, computer
programing has evolved quite lot. it’s currently
become additional powerful, efficient, and
advanced. However, the basic ideas and use of
information structure and algorithms in laptop
programming haven’t changed. It must be the
core of computer programming from the
beginning. you may have detectedAOE0662 Data
StucturesA getting used in the main within the
field of computer science. However, the
employment ofAOE0662 Data StucturesA isn’t
restricted to the sector of computing. we will
conjointly notice the construct ofAOE0662 Data
StucturesA being used in day to day life.
Nidhi Sharma AOE0662(DS) Unit-1
Course Objective
https://www.youtube.com/watch?v=ZAU5IlCQBls
• Students will be able to understand the concept of stack and queue with
there memory representations.
6 TOP = 5
5 E
4 D
3
C
2 B
1 A
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Stack Abstract Data Type
ADT Stack {
Data/Attributes/Values:
int size; Top Type items;
Functions/Operations:
CreateStack (int size); --create stack of size Void
Push(int n); - - if stack is not full
Type Pop(); - - if stack is not empty return top item
Int isEmpty(); - - return true if empty otherwise false
Int isFull(); - - return true if full otherwise false }
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
PUSH Operation
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
POP Operation
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Topic Objective
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack: Prefix and Postfix
Expressions
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Video Link
https://youtu.be/o-B4qNnwujY
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
FAQs
a) (A-B)*(D/E)
b) (A+B↑D)/(E-F)+G
c) A*(B+D)/E-F*(G+H/K)
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Assignment
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Recap
Arithmetic Expressions:
• Prefix notation: Operator symbol is placed before its two operands
• Postfix notation: Operator is placed after the two operands and also
called “reverse Polish notation”.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Recap
Arithmetic Expressions:
• Prefix notation: Operator symbol is placed before its two operands
• Postfix notation: Operator is placed after the two operands and also
called “reverse Polish notation”.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack:Infix to Postfix conversion
using STACK
Suppose Q is an arithmetic expression written in infix notation. This algorithm
finds the equivalent postfix expression P.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Video Link (Infix to postfix)
https://youtu.be/krn8Uv1C_F4
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Daily Quiz (Infix to Postfix using STACK)
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
FAQs
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack:Evaluation of Postfix
expression
This algorithm finds the VALUE of an arithmetic expression P written
in postfix notation.
1. Add a right parenthesis ”(” at the end of P.
2. Scan P from left to right and repeat Steps 3 and 4 for each element
of P until the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator X is encountered, then:
a) Remove the two top elements of STACK, where A is the
top element and B is the next-to-top element.
b) Evaluate B X A.
c) Place the result of (b) back on STACK
5. Set VALUE equal to the top element on STACK.
6. Exit.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack:Evaluation of Postfix
expression
Arithmetic expression P written in postfix notation is:
P: 5, 6, 2, +, *, 12, 4, /, -
Symbol Scanned STACK
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37
)
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Revision Qs
Q1: Consider an array A which records the number of cars sold each
year from 1970-2001. Suppose base address of array is 100 and w is
4 words per memory cell. Find out the location of A[1990].
Ans: 180
Q2:What is the value of the postfix expression 9 3 1 4 + – *?
1. 1
2. 40
3. 74
4. -18
Ans: 4.
Q3: : Consider the linear array A[-5:5], B[1920:1995], C[50]. Find the
number of elements in each array.
Ans: 11,76,50
Q4:Substring(‘Good Morning’, 4, 7) = ‘d_Morni’
Hint: SUBSTRING(string, initial, length)
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Revision Qs
Revision Qs
Q5:Here is an infix expression: 12/(7-3)+2*(1+5). Suppose that we are
using the usual stack algorithm to convert the expression from infix to
postfix notation. The maximum number of symbols that will appear on the stack AT
ONE TIME during the conversion of this expression?
1. 5
2. 2
3. 3
4. 4
Q6:The postfix form of the expression (A+ B)*(C*D- E)*F / G is?
Ans : AB+CD*E-*F*G/
Q7: An array X[6][19] is stored in the memory with each element requiring 2
bytes of storage. If the base address of array is 2020, calculate the location of
X[3][5] when the array X is stored in column major order. Note: X[6][19] means
valid row indices are 0 to 5 and valid column indices are 0 to 18
Hint: X[I][J]=B+W[(I-lr)+R(J-lc)]
Ans: 2086
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Revision Qs solution
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Revision Qs solution
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Topic Objective
Types of Recursions:
fun()
{ Fun1() Fun2()
------------- { {
------------
----------
fun() Fun2() Fun1()
}
} }
Fun() Fun()
{ {
Fun(); …….
……. ……
…… …….
……. Fun();
Head &Tail Recursion()
}
}
Fun()
{
Fun()
...….
……
…….
Fun();
}
Nidhi Sharma AOE0662 Data
Stuctures Unit 2
Tail recursion
Recursion
• There is base condition, that stops further calling of the
function
Recursive Procedure:
Suppose P is a procedure containing either a call statement to
itself or call statement to a second procedure containing either a
call statement back to the original procedure P.
Then P is called a recursive procedure. So that the program will
not continue to run indefinitely, a recursive procedure must have
the following two properties:
1. There must be certain criteria, called base criteria, for which
the procedure does not call itself.
2. Each time the procedure does call itself(directly or
indirectly), it must be closer to the base criteria.
A recursive procedure with these two properties is said to be
well- defined.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Tail recursion
Recursive Function:
A Function is said to be recursively defined if the function
definition refers to itself .Again, in order for the definition not to
be circular, it must have the following two properties:
1. There must be certain argument, called base values, for
which the function does not refer to itself.
2. Each time the function does refer to itself the argument of
the function must be closer to the base value.
A recursive function with these two properties is said to be well-
defined.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Fibonacci numbers
FIBONACCI(FIB, N)
This procedure calculates FN and returns the value in the first
parameter FIB.
1. If N = 0 or N =1, then: Set FIB:=N, and return.
2. Call FIBONACCI(FIBA, N-2).
3. Call FIBONACCI(FIBB, N-1).
4. Set FIB:= FIBA+FIBB.
5. Return.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Fibonacci numbers
Fibonacci series program in C if (c <= 1)
#include <stdio.h> next = c;
int main() else
{ {
int n, first = 0, second = 1, next, c; next = first + second;
first = second;
printf("Enter the number of terms\ second = next;
n"); }
scanf("%d", &n); printf("%d\n", next);
}
printf("First %d terms of Fibonacci
series are:\n", n); return 0;
}
for (c = 0; c < n; c++)
{
• Cons
• Recursive functions are generally slower than non-recursive
functions.
• May require a lot of memory to hold intermediate results on the
system stack.
• It is difficult to think recursively so one must be very careful
when writing
recursive functions.
Nidhi Sharma AOE0662 Data
Stuctures Unit 2
Removal of recursion Problem solving using iteration
and recursion with examples of binary search
int main() {
int num, i, key, position;
int low, high, list[size];
printf("\nEnter the total number of elements");
scanf("%d", &num);
printf("\nEnter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("\nEnter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("\nNumber present at %d", (position + 1));
} else
printf("\n The number is not present in the list");
return (0);
} Nidhi Sharma AOE0662 Data Stuctures Unit 2
Removal of recursion Problem solving using iteration
and recursion with examples of binary search
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Hanoi towers. Tradeoffs between iteration and
recursion
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Hanoi towers. Tradeoffs between iteration and
recursion
1) Move the top n-1 disks from peg A to peg B.
2) Move the top disk from peg A to peg C
3) Move the top n-1 disks from peg B to peg C.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Hanoi towers. Tradeoffs between iteration and
recursion
TOWER (N, BEG,AUX,END)
This procedure gives a recursive solution to the Towers of Hanoi
problem for N disks.
1. If N = 1, then:
a) Write: BEG END.
b) Return.
2. Call TOWER(N-1,BEG,END,AUX)
3. Write: BEG END.
4. Call TOWER (N-1, AUX, BEG, END).
5. Return.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Video Link
https://youtu.be/YsFGWYBLTb0
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Daily Quiz
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
FAQs
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Recap
Two important steps for infix to postfix using stack:
1. If an operator X is encountered, then:
a) Repeatedly pop from STACK and add to P each operator(on the top of
STACK) which has the same precedence as or higher precedence than X.
b) Add X to STACK.
2. If a right Parenthesis is encountered, then:
a) Repeatedly pop from STACK and add to P each operator(on the top
of STACK) until a left parenthesis is encountered.
b) Remove the left parenthesis.
STACK
XXX YYY ZZZ
1 2 3 4 5 6 7 8
TOP 3 MAXSTK 8
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Array and Linked Implementation of Stack
TOP(START)
XXX YYY ZZZ www X
INFO LINK
Top of Stack Bottom of Stack
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Array and Linked Implementation of Stack
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Array and Linked Implementation of Stack
PUSH_LINKSTACK(INFO,LINK,TOP,AVAIL, ITEM)
This procedure Pushes an ITEM into a linked stack.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Array and Linked Implementation of Stack
POP_LINKSTACK(INFO,LINK,TOP,AVAIL,ITEM)
This procedure deletes the top element of a linked stack and
assigns it to the variable ITEM.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Video Link
https://youtu.be/RlYwmQSoTUo
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Daily Quiz
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Weekly Assignment 1
1. What is stack? Write an algorithm to POP an element from stack.
2. Convert Infix to Postfix using Stack.
a) (A-B)*(D/E)
b) A*(B+D)/ E – F * (G + H/K).
3. Write an algorithm to PUSH an element in stack.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Queue
Queue
Definition:
A Queue is a linear list of elements in which deletions can take
place only at one end, called the front, and insertions can take
place only at the other end, called the rear.
• https://video.search.yahoo.com/search/video?fr=mcafee&p=operation+in
+queue+video#id=1&vid=e43cc8a4f3352c2a415dce4eef6d6be8&action=cl
ick
Operations on queue:
Types Of Queue
1. Circular Queue
2. Priority Queue
3. Deque
4. Multiple Queue
• Problem:
– Wastage of memory in standard queue in DEQUEUE
operation
Priority Queue:
https://video.search.yahoo.com/search/video?fr=mcafee&p=big+oh+notation
+data+structure+video#id=2&vid=0da5ae6151918fd479169873ccb78a6b&act
ion=click
Priority Queue:
Priority Queue is an extension of Queue with following properties.
1. Every item has a priority associated with it.
2. An element with high priority is dequeued before an element with low
priority.
3. If two elements have the same priority, they are served according to
their order in the queue.
9. If the elements “A”, “B”, “C” and “D” are placed in a queue and are
deleted one at a time, in what order will they be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC
https://noidainstituteofengtech-
my.sharepoint.com/:b:/g/personal/
nidhisharma_niet_co_in/
EYRnv18D_TlNso22HXXGW3sBtHvazqQLJWEd7y
xtzwhrwA?e=RL0Vcs
05/12/2025Nidhi Sharma AOE0662 Data Structures Unit 1
Expected Questions for University Exam
Faculty : Nidhi Sharma KOE 035 Data Structure and algorithms Unit-
05/12/2025 3
Recap
• A stack is a list of elements in which an element may be inserted or
deleted only at one end, called the top of the stack. (LIFO-Last In First
Out)
Operations on queue:
Priority Queue:
Priority Queue is an extension of Queue with following properties.
1. Every item has a priority associated with it.
2. An element with high priority is dequeued before an element with low
priority.
3. If two elements have the same priority, they are served according to
their order in the queue.
Thank You