0% found this document useful (0 votes)
12 views122 pages

Data Structure Unit2 Ppt

The document outlines the syllabus and course details for the Data Structures subject (AOE0662) taught by Ms. Nidhi Sharma at the Noida Institute of Engineering and Technology. It covers various topics including stacks, queues, trees, graphs, searching, and sorting algorithms, along with their applications. The document also includes course objectives, outcomes, evaluation schemes, and prerequisites for students.

Uploaded by

akshitkumar1616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views122 pages

Data Structure Unit2 Ppt

The document outlines the syllabus and course details for the Data Structures subject (AOE0662) taught by Ms. Nidhi Sharma at the Noida Institute of Engineering and Technology. It covers various topics including stacks, queues, trees, graphs, searching, and sorting algorithms, along with their applications. The document also includes course objectives, outcomes, evaluation schemes, and prerequisites for students.

Uploaded by

akshitkumar1616
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

Noida Institute of Engineering and Technology,

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.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


Brief Introduction

Ms. Nidhi Sharma

• PhD Amity University, Pursuing


• M.Tech AKTU, Lucknow, India
• B.Tech. Kurukshetra University, Kkr. , Haryana

Research Interests
VLSI Design, Communication System, Internet of Things.

Industrial Experiance: (2.5 Years)


Academic Experience: (17+ years)

Publication : 08 (Peer Reviewed journals), 05(International Conference), 01 National


conference.
05/12/2025
Ms. Nidhi Sharma AOE0662 Data Stuctures Unit1
NOIDA INSTITUTE OF ENGINEERING AND
TECHNOLOGY, GREATER NOIDA
Evaluation Scheme

Ms. Nidhi Sharma AOE0662 Data Stuctures Unit1


NOIDA INSTITUTE OF ENGINEERING AND
TECHNOLOGY, GREATER NOIDA
Course Contents / Syllabus
UNIT-I: Introduction: Basic Terminology, Elementary Data Organization, Built in Data Types in C/python. Algorithm, Efficiency of an
Algorithm, Time and Space Complexity, Asymptotic notations: Big Oh, Big Theta and Big Omega, Abstract Data Types (ADT) Arrays: Single
and Multidimensional Arrays, Representation of Arrays: Row Major Order, and Column Major Order, Index Formulae for 1-D,2-D,3-D and n-D
Array Application of arrays, Sparse Matrices and their representations. Linked lists: Array Implementation of Singly Linked Lists, Doubly Linked
List, Circularly Linked List, Operations on a Linked List. Insertion, Deletion, Traversal. Polynomial Representation and Addition Subtraction &
Multiplications of Single variable. .
UNIT II : Stacks: Abstract Data Type, Primitive Stack operations: Push & Pop, Array and Linked Implementation of Stack, Application of stack:
Prefix and Postfix Expressions, Evaluation of postfix expression, Iteration and Recursion- Principles of recursion, Tail recursion, Removal of
recursion Problem solving using iteration and recursion with examples of binary search, Fibonacci numbers, and Hanoi towers. Tradeoffs between
iteration and recursion.Queues: Operations on Queue: Create, Add, Delete, Full and Empty, Circular queues, Dequeue and Priority Queue.

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

 Students will be familiar with the concept of time and space


complexity and demonstrate linear data structure array and linked
lists.
 Students will be able to understand the concept of stack and queue
with there memory representations.
 Students will be able to know the non linear data structures- trees.
 Students will be able to know the non linear data structures- graphs
with their different traversal ways.
 Students will be able to analyze the concepts of searching and
sorting.

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT-4


Course Outcome

Course COURSE OUTCOMES Bloom’s


Outcome At the end of the semester, Knowledge
no. the students will be able to: Level (KL)
Understand and analyze the time and space K4, K2
AOE0662.1
complexity of an algorithm
Understand and implement fundamental K2, K3
AOE0662.2 algorithms(including sorting algorithms, graph
algorithms, and dynamic programming)
Discuss various algorithm design techniques for K1, K2
AOE0662.3
developing algorithms
Discuss various searching, sorting and graph K2, K3
AOE0662.4
traversal Algorithms
Understand operation on Queue, Priority Queue, K2
AOE0662.5
D-Queue.

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT- 1


CO-PO Mapping

CO PO PO PO PO PO PO PO PO PO PO1 PO1 PO1


1 2 3 4 5 6 7 8 9 0 1 2
AOE06 3 3 3 3 2 1 1 2 2 1 2 2
62.1
AOE06 3 3 3 2 2 1 1 1 2 2 1 2
62.2
AOE06 3 3 3 3 2 1 - 2 1 1 1 2
62.3
AOE06 3 3 2 3 2 1 1 2 2 1 1 2
62.4
AOE06 3 3 3 3 2 1 1 2 2 1 1 2
62.5
Avg 3 3 2.8 2.8 2 1 0.8 1.8 1.8 1.2 1.2 2

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT- 1


CO-PSO Mapping

CO PSO1 PSO2 PSO3


AOE0662.1 3 3 3
AOE0662.2 3 3 3
AOE0662.3 3 3 3
AOE0662.4 3 3 3
AOE0662.5 3 3 3
Avg 3 3 3

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT- 1


RESULT ANALYSIS

OVERALL RESULT : 90.37%


SUBJECT RESULT : 90.37%
FACULTY RESULT : 91.37%

Nidhi Sharma AOE0662 Data Stuctures Unit-1


QUESTION PAPER TEMPLATE

Nidhi Sharma AOE0662 Data StucturesD Unit-1


Prerequisite and Recap

• Basic mathematical calculation skills and logical


skills
• Basic knowledge of C language
• In the previous units students were learn about the
array, stack, queue and linked list.
• Students were understand the algorithms and their
solving techniques.

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT- 1


Brief subject Introduction withVideo

https://www.youtube.com/watch?v=ZAU5IlCQBls

05/12/2025 Nidhi Sharma AOE0662(DS) UNIT-4


Book referred

Nidhi Sharma AOE0662 Data Structures Unit 1


05/12/2025
Unit 2 Content

• Stacks: Abstract Principles of • Hanoi towers.


Data Type, recursion, Tail • Tradeoffs between iteration and
• Primitive Stack recursion, recursion. Representation of Queue
• Queues: Operations on Queue:
operations: Push • Removal of
& Pop, recursion Create, Add, Delete, Full and Empty
• Circular queues
• Array and Problem solving
• Dequeue
Linked using iteration
• Priority Queue. Video Links
Implementation and recursion
• Daily Quiz
of Stack, with examples
• Weekly Assignment
• Arithmetic of binary
• MCQs
Expressions search,
• Glossary Qs
• Application of • Fibonacci • Old Question paper
stack: Prefix numbers • Expected Questions for university
and Postfix Exam
Expressions, • Recap
• Evaluation of
postfix
expression,
05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2
Unit 2 Syllabus

Stack definition, Stack operations, PUSH operation, POP operation,


Arithmetic Expressions, Infix to Postfix conversion using stack,
Evaluation of Postfix expression, Implementation of Stack, PUSH and
POP linked representation, Representation of Queue, Operation in
queue, Priority queue, D-Queue, Singly and circularly linked list,
List operations, List implementations.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Unit Objective

• Students will be able to understand the concept of stack and queue with
there memory representations.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


05/12/2025
Prerequisite/Recap

• Fundamentals of Data Structure.


• Basics of Array.
• Basics of Linked List.
• Types of operation in data structure
• Traversing
• Insertion
• Deletion

Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2


Prerequisite and Recap

• What are linear data structures?


• Queue is an abstract data structure, somewhat similar to Stacks.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Topic Objective

• To Study Abstract Data Type.

• To learn about basic stack operations PUSH and POP.

• Understand array and linked list implementation of stack.

• Learn application of Stack.

• To learn about recursion and Tower of Hanoi.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


Introduction to Stack

• 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)

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 }

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Primitive Stack operations: Push & Pop

Two basis operations associated with stack:

a) “push”– to insert an element into a stack.


b) “pop”– to delete an element from a stack.

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
PUSH Operation

PUSH(STACK, TOP, MAXSTK, ITEM)


This procedure pushes an ITEM onto a stack.
1. If TOP = MAXSTK, then: Print: OVERFLOW, and Return.
[Check stack is already filled]
2. Set TOP:= TOP+1. [Increase TOP by one.]
3. Set STACK[TOP]:=ITEM . [Inserts ITEM in new TOP position.]
4. Return.

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
POP Operation

POP(STACK, TOP, ITEM)


This procedure deletes the top element of STACK and assigns it to the
variable ITEM.
1. If TOP=0, then: Print: UNDERFLOW, and Return.
2. Set ITEM := STACK [TOP]. [Assigns TOP element to ITEM.]
3. Set TOP:=TOP -1. [Decreases TOP by 1.]
4. Return.

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Topic Objective

• To Learn application of Stack.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


Application of stack: Prefix and Postfix
Expressions

• Evaluation of arithmetic expression using reverse Polish(Postfix)


notation.
• Operator precedence for five binary operations:
Highest : Exponentiation(↑)
Next highest : Multiplication(*) and Division(/)
Lowest : Addition(+) and Subtraction(-)
• Types of notations :
 Prefix
 Infix
 Postfix

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack: Prefix and Postfix
Expressions

 Prefix notation: Operator symbol is placed before its two operands.


E.g.: +AB, -CD, *EF, /GH.
Also called “Polish notation”

 Infix notation: Operator in between the two operands.


E.g.: A+B, C-D, E*F, G/H.

 Postfix notation: Operator is placed after the two operands and


also called “reverse Polish notation”.
E.g.: AB+ ,CD-, EF*,GH/

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

Q1:Translate, by inspection and hand, each infix expression into


its equivalent postfix expression:

a) (A-B)*(D/E)
b) (A+B↑D)/(E-F)+G
c) A*(B+D)/E-F*(G+H/K)

Q2:Consider the following arithmetic expression P, written in


postfix notation:
P: 12,7,3,-,/,2,1,5,+,*,+
d) Translate P, by inspection and hand, into its equivalent infix
expression.
e) Evaluate the infix expression.

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Assignment

Q1: What is Stack? Write an Algorithm to POP an element from


stack.
Q2: Convert the infix expression to postfix expression.
(A-B)/((D+E) * F)
Q3: Evaluate the following Postfix expression
AB+C*D/
for A=2,B=3,C=4,D=5.
Q4: Write an Algorithm to evaluate postfix expression. Evaluate
the following postfix expression
Abcde+*+-
Q5: Convert the infix expression to postfix expression
(A+B^D-E)/(E-F)+G

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
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)

Arithmetic Expressions:
• Prefix notation: Operator symbol is placed before its two operands

• Infix notation: Operator in between the 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

• 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)

Arithmetic Expressions:
• Prefix notation: Operator symbol is placed before its two operands

• Infix notation: Operator in between the 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.

1. Push “(” onto STACK, and add ”)” to the end of Q.


2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q
until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. 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.
6. If a right Parenthesis is encountered, then:
7. 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.
8. Exit.
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Application of stack:Infix to Postfix conversion
using STACK
Symbol Scanned STACK Expression P
A+(B*C-(D/E↑F)*G)*H) A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
( (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
↑ (+(-(/↑ ABC*DE
F (+(-(/↑ ABC*DEF
) (+(- ABC*DEF↑/
* (+(-* ABC*DEF↑/
G (+(-* A B C * D E F ↑/ G
) (+ A B C * D E F ↑/ G * -
* (+* A B C * D E F ↑/ G * -
H (+* A B C * D E F ↑/ G * - H
) A B C * D E F ↑/ G * - H * +

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)

1. Here is an infix expression: 4 + 3*(6*3-12). 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?
a) 1
b) 2
c) 3
d) 4
Answer: D

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
FAQs

1. Consider the following infix expression Q:


Q: A+ (B * C - (D / E ^ F ) * G ) * H
Translate Q into its equivalent postfix expression using STACK.

2. Convert Infix to Postfix using Stack.


a) (A-B)*(D/E)
b) A*(B+D)/ E – F * (G + H/K).

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 Faculty Name Subject code and abbr


eviation Unit Number
Revision Qs
Revision Qs
Q1: Consider the following parenthesis free arithmetic expression:
E: 6+2^3^2-4*5
Evaluate the expression E, a) assuming that exponentiation is performed from left
to right as are the other operations, and b) assuming that exponentiation is
performed from right to left.
Ans: 498
Q2: Using the algorithm translate each infix expression into its equivalent postfix
expression:
a) (A-B)/((D+E)*F) b) ((A+B)/D)^((E-F)*G)
Ans a) AB-DE+F*/ b) AB+D/EF-^G*
Q3: Evaluate each of the following parenthesis free arithmetic expressions:
b) 5 + 3^2 – 8/4*3+6 Ans: 14
c) 6+2^3+9/3-4*5 Ans: -3
Q4 The complexity of linear search algorithm is:
d) O(n)
e) O(Log n)
f) O(n log n)
g) O(n^2)
Ans: a) O(n)

05/12/2025 Nidhi Sharma KOE 035 DS Unit 2


Revision Qs
Revision Qs
Q5: Define FRONT and REAR pointers and write an algorithm to
delete an element from the Queue.
Q6: Consider the linear arrays AAA(5:52), BBB(-4:10) and
CCC(16).
a) Find the number of elements in each array. Ans: 48,15,16
b) Suppose Base(AAA) = 200 and w = 4 words per memory cell
for AAA. Find the address of AAA[14],AAA[45] and AAA[56].
Hint: LOC (LA[K]) = Base(LA) + W(K-LB)

Ans: 236,360,not possible

05/12/2025 Nidhi Sharma KOE 035 DS 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
Revision Qs solution

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Topic Objective

• To learn about recursion in detail.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


Tail recursion

Types of Recursions:

Recursion are mainly of two types depending on whether a


function calls itself from within itself or more than one
function call one another mutually. The first one is called direct
recursion and another one is called indirect recursion.

fun()
{ Fun1() Fun2()
------------- { {
------------
----------
fun() Fun2() Fun1()
}
} }

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Tail recursion

Head Recursion() Tail Recursion()

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

• Function call itself directly or indirectly, it should reach


towards base condition.

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Tail recursion

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++)
{

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 1


Removal of recursion Problem solving using iteration
and recursion with examples of binary search

Recursion Pros and Cons


• Pros
• The code may be much easier to write.
• To solve some problems which are naturally recursive such as
tower of Hanoi.

• 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

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Removal of recursion Problem solving using iteration
and recursion with examples of binary search
Binary Search Using Recursion

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

int binsearch(int a[], int x, int low, int high) {


int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}
}
Nidhi Sharma AOE0662 Data Stuctures Unit 2
Topic Objective

• To learn about recursion and Tower of Hanoi.

Nidhi Sharma AOE0662 Data Stuctures Unit 2


Hanoi towers. Tradeoffs between iteration and
recursion
Tower of Hanoi problem:
Suppose three Pegs, Labelled A(BEG),B(AUX) and C(END), are
given, and suppose on peg A there are placed a finite number n
of disks with decreasing size. The object of the game is to move
the disks from peg A to peg C using peg B as an auxiliary peg.
The rules of the game are as follows:
a) Only one disk may be moved at a time. Specifically, only the
top disk on any peg may be moved to any other peg.
b) At no time can a larger disk be placed on a smaller disk.

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

1. What is the value of the postfix expression 6 3 2 4 + – *:


a) 1
b) 40
c) 74
d) -18
Answer: D

05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
FAQs

Evaluate the following postfix expressions:


i) AB+C*D/ for A=2, B=3, C=4, D=5
ii) abcde+*+-
iii) P1: 5, 3, +, 2, *, 6, 9, 7, -, /, -
iv) P2: 3, 5, +, 6, 4, -, *, 4, 1, -, 2, ^, +
v) P3: 3, 1, +, 2, ^, 7, 4, - 2, *, +, 5, -

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.

Important step for evaluation of postfix using stack:


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
05/12/2025 Nidhi Sharma KOE 035 Data Structure and algorithms Unit-2
Array and Linked Implementation of Stack

Two ways to represent stack in the memory are:


1. Array representation of stack.
2. Linked representation of stack.

Array representation of stack:

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

Linked representation 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

TOP XXX YYY ZZZ X

TOP www XXX YYY ZZZ X

Fig1:Stack after PUSH operation

TOP XXX YYY ZZZ X

TOP YYY ZZZ X

Fig2:Stack after POP operation

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.

1. If AVAIL=NULL, then write OVERFLOW and Exit.


2. Set NEW:= AVAIL and AVAIL:=LINK[AVAIL].
3. Set INFO[NEW]:= ITEM
4. Set LINK[NEW]:= TOP
5. Set TOP = NEW.
6. Exit.

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.

1. If TOP= NULL then write : UNDERFLOW and Exit.


2. Set ITEM:=INFO[TOP].
3. Set TEMP:=TOP and TOP:=LINK[TOP].
4. Set LINK[TEMP] = AVAIL and AVAIL= TEMP.
5. Exit.

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

1. Which of the following is true about linked list implementation of stack?


a) In push operation, if new nodes are inserted at the beginning of linked
list, then in pop operation, nodes must be removed from end.
b) In push operation, if new nodes are inserted at the end, then in pop
operation, nodes must be removed from the beginning.
c) Both of the above
d) None of the above
Answer: D

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.

4. Consider the following arithmetic expression P, written in postfix Notation:


P: 12,7,3,-,/,2,1,5,+,*,+
c) Translate P, by inspection and hand, into its equivalent infix expression.
d) Evaluate the infix expression using stack.
5. Q: ((A+B)*D) ↑ (E-F)
Translate Q into its equivalent postfix expression P.

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.

Queues are also called first-in first-out (FIFO) lists.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Representation of Queue

Queue – Array Representation


Queue representation mean how the queue elements are stored in the memory.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Representation of Queue(CO3)

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Representation of Queue(CO3)

Queue – Linked Representation


Queue representation mean how the queue elements are stored in the memory.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty
Operations on queue:

− add (store) an item to the queue.


− remove (access) an item from the queue.

• In queue, we always remove(or access) data, from the front.


• while data is added (or storing) in the queue at rear.
• During removing the data FRONT pointer is incremented.
FRONT:= FRONT+1
• During addition of the data REAR pointer is incremented.
REAR:= REAR+1
• FRONT:=NULL and REAR:= NULL ----- Means The Queue Is empty
• FRONT:=1 and REAR:=N -----Means The Queue is Fully filled.
• FRONT:=REAR+1 -----Means Circular Queue is Fully Filled

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operation in Queue(CO3)

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty
Adding an element into the queue

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty
QINSERT(QUEUE,N,FRONT,REAR,ITEM)
This procedure insert an element ITEM into a queue.
1. If FRONT=1 and REAR=N, or if FRONT=REAR+1, then:
Write: OVERFLOW, and Return. [Queue is already filled?]
2. If FRONT=NULL, then:[Queue initially empty.]
Set FRONT:=1 and REAR:=1.
Else if REAR=N, then:
Set REAR:=1.
Else: Set REAR:= REAR+1. [Find new value of REAR.]
3. Set QUEUE[REAR] := ITEM. [This inserts new element.]
4. Return.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty
Removing an element from the queue

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty
QDELETE(QUEUE,N,FRONT,REAR,ITEM)
This procedure deletes an element from the Queue and assigns it
to the variable ITEM.
1. If FRONT=NULL, then: Write: UNDERFLOW, and Return.
[Queue is already empty?]
2. Set ITEM:=QUEUE[FRONT].
3. If FRONT=REAR, then:[Queue has only one element to start.]
Set FRONT:=NULL and REAR:=NULL.
Else if FRONT=N, then:
Set FRONT:=1.
Else: Set FRONT:= FRONT+1. [Find new value of FRONT.]
3. Return.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Operations on Queue: Create, Add, Delete, Full and
Empty

• https://video.search.yahoo.com/search/video?fr=mcafee&p=operation+in
+queue+video#id=1&vid=e43cc8a4f3352c2a415dce4eef6d6be8&action=cl
ick

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Daily Quiz

Q1: Elements are inserted into the queue:


a) At the front
b) At the rear
c) At the middle
d) None
Q2: Elements are deleted from the queue:
e) At the front
f) At the rear
g) At the middle
h) None
Q3: In Queue data structure
i) FIFO
j) LIFO
k) LILO
l) None

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


FAQs

Q1: What do you mean by a queue? How it is different from stack?


Q2: Explain with neat diagram the insertion and deletion of an element into
the queue.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Recap

Queue – Array Representation


Queue representation mean how the queue elements are stored in the
memory.

Operations on queue:

− add (store) an item to the queue.


− remove (access) an item from the queue.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Circular Queue

Types Of Queue

1. Circular Queue
2. Priority Queue
3. Deque
4. Multiple Queue

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

Why Circular Queue is needed?

• Problem:
– Wastage of memory in standard queue in DEQUEUE
operation

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

What is Circular Queue?

• The Arrangement of the elements Q[0], Q[1], ..,Q[n] in a circular


fashion with Q[1] following Q[n] is called Circular Queue.

• The last node is connected to first


node to make a circle.

• Initially, Both Front and Rear pointers points to


the beginning of the array.

• It is also known as “Ring Buffer”.


Nidhi Sharma AOE0662 Data
Stuctures Unit 2
Circular Queue

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

Insertion in circular queue


Step 1 - Check whether Circular queue is FULL. Front ==
(Rear+1)%Max

Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is


not possible!!!" and terminate the function.

Step 3 - If it is NOT FULL, then check Front == -1,


then Front =0.

Step 4 - Rear = (Rear+1)%max, set Cqueue[Rear] = value.

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

Deletion in circular queue


Step 1 - Check whether queue is EMPTY. (front == -1)

Step 2 - If it is EMPTY, then display “Circular Queue is EMPTY!!!


Deletion is not possible!!!" and terminate the function.

Step 3 - Val = Cqueue[front];


Step 4 – if(front==rear)
front = rear = -1;
Step 5 – front = (front+1)%Max;
Step 6 – return Val

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

Application of Circular Queue


Traffic light functioning is the best example for circular queues. The
colors in the traffic light follow a circular pattern.

In page replacement algorithms, a circular list of pages is maintained


and when a page needs to be replaced, the page in the front of the
queue will be chosen.

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Circular Queue

display() - Displays the elements of a Circular Queue


We can use the following steps to display the elements of a circular queue...

Step 1 - Check whether queue is EMPTY. (front == -1)


Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the
function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
Step 4 – for(i = front; i!=rear; i = (i+1)%Max)
Print Cqueue[i];
end
print(queue[rear]);
Step 5 - Exit

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
D-Queue

• Deque is a linear list in which elements can be added or


removed at either end but not in the middle. The term deque
is a contraction of the name double-ended queue.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Priority 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.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Linked representation of Priority Queue

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Array representation of Priority Queue

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Priority Queue, D-Queue

https://video.search.yahoo.com/search/video?fr=mcafee&p=big+oh+notation
+data+structure+video#id=2&vid=0da5ae6151918fd479169873ccb78a6b&act
ion=click

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Daily Quiz

Q1: In Priority Queue how many fields are there:


a) 1
b) 2
c) 3
d) none
Q2: PRI Number is a field in priority queue which stores …………………………….
Q3: In D-Queue we can insert or delete from:
e) Front
f) Rear
g) Both ends
h) middle

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


FAQs

Q1: What do you mean by Priority Queue? Explain with diagram.


Q2: How priority Queue is different from normal queue?
Q3: What is the difference between priority queue and D-queue?

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Recap

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.

Double ended queue:


Double ended queue is a more generalized form of queue data structure
which allows insertion and removal of elements from both the ends, i.e ,
front and back.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Weekly Assignment

Q1: What do you mean by a queue? How it is different from stack?


Q2: Explain with neat diagram the insertion and deletion of an element into
the queue.
Q3: What do you mean by Priority Queue? Explain with diagram.
Q4: How priority Queue is different from normal queue?
Q5: What is the difference between priority queue and D-queue?
Q6: What do you by linked list? State the types of linked lists.
Q7: Explain Circular linked list with neat diagram.
Q8: Explain how a node is inserted as first node in the linked list?
Q9: Write an algorithm to delete a node from the given location.
Q10: Write an algorithm to insert a node before the given location.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


MCQ s

1. Process of inserting an element in stack is called ____________


a) Create
b) Push
c) Evaluation
d) Pop

2. Process of removing an element from stack is called __________


a) Create
b) Push
c) Evaluation
d) Pop

Nidhi Sharma AOE0662 Data Stuctures Unit 2


MCQ s

3. In a stack, if a user tries to remove an element from empty stack it is


called _________
a) Underflow
b) Empty collection
c) Overflow
d) Garbage Collection

4. Pushing an element into stack already having five elements and


stack size of 5, then stack becomes
a) Overflow
b) Crash
c) Underflow
d) User flow

Nidhi Sharma AOE0662 Data Stuctures Unit 2


MCQ s

5. Which of the following applications may use a stack?


a) A parentheses balancing program
b) Tracking of local variables at run time
c) Compiler Syntax Analyzer
d) Data Transfer between two asynchronous process

6. 1. Recursion is a method in which the solution of a problem depends


on ____________
a) Larger instances of different problems
b) Larger instances of the same problem
c) Smaller instances of the same problem
d) Smaller instances of different problems

Nidhi Sharma AOE0662 Data Stuctures Unit 2


MCQ s

7. Recursion is similar to which of the following?


a) Switch Case
b) Loop
c) If-else
d) if elif else

8. A queue follows __________


a) FIFO (First In First Out) principle
b) LIFO (Last In First Out) principle
c) Ordered array
d) Linear tree

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
MCQ s

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

10. A data structure in which elements can be inserted or deleted


at/from both the ends but not in the middle is?
a) Queue
b) Circular queue
c) Dequeue
d) Priority queue

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Glossary Question

i. Stack ii. Abstract Data type iii.Queues iv. Linear


search
Answer the questions.
a. Another name for sequential search…..
b. An elements inserted and deleted from one end
only….
c. The specification of a data type with some language.
d. Elements removed from the other end.

Nidhi Sharma AOE0662 Data


Stuctures Unit 2
Old Question Papers
Q1: What do you mean by a queue? How it is different from stack?
Q2: Explain with neat diagram the insertion and deletion of an element into
the queue.
Q3: What do you mean by Priority Queue? Explain with diagram.
Q4: How priority Queue is different from normal queue?
Q5: What is the difference between priority queue and D-queue?
Q6: What do you by linked list? State the types of linked lists.
Q7: Explain Circular linked list with neat diagram.
Q8: Explain how a node is inserted as first node in the linked list?
Q9: Write an algorithm to delete a node from the given location.
Q10: Write an algorithm to insert a node before the given location.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Old Question Papers

05/12/2025 Nidhi Sharma AOE0662 Data Structures Unit 1


Old Question Paper

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

Q1: Write an algorithm to insert a node before the given location.


Q2: What do you mean by Priority Queue? Explain with diagram.
Q3: Explain with neat diagram the insertion and deletion of an element into
the queue.
Q4: Explain Circular linked list with neat diagram.
Q5: How priority Queue is different from normal queue?

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Expected Questions for University Exam
1. What is the principle of STACK? Write the algorithm for tower of Hanoi.
2. Evaluate the following postfix expressions:
i) AB+C*D/ for A=2, B=3, C=4, D=5
ii) abcde+*+-
iii) P1: 5, 3, +, 2, *, 6, 9, 7, -, /, -
iv) P2: 3, 5, +, 6, 4, -, *, 4, 1, -, 2, ^, +
v) P3: 3, 1, +, 2, ^, 7, 4, - 2, *, +, 5, -

3. Consider the following infix expression Q:


Q: A+ (B * C - (D / E ^ F ) * G ) * H
Translate Q into its equivalent postfix expression using STACK
4. Write an algorithm to POP an element from stack.
5. Write an algorithm to PUSH an element in stack.
6. What is linked list and types of linked list? Write an algorithm for
inserting a node after a given node(LOC).

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)

• Two basis operations associated with stack:


• “push”– to insert an element into a stack.
• “pop”– to delete an element from a stack.

• Prefix notation: Operator symbol is placed before its two operands.


• Infix notation: Operator in between the two operands.
• Postfix notation: Operator is placed after the two operands and also
called “reverse Polish notation”.

• Two ways to represent stack in the memory are:


• Array representation of stack.
• Linked representation of stack.
Faculty : Nidhi Sharma KOE 035 Data Structure and algorithms Unit-
05/12/2025 3
Recap

Queue – Array Representation


Queue representation mean how the queue elements are stored in the
memory.

Operations on queue:

− add (store) an item to the queue.


− remove (access) an item from the 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.

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2


Noida Institute of Engineering and Technology,
Greater Noida

Thank You

05/12/2025 Nidhi Sharma AOE0662 Data Stuctures Unit 2

You might also like