Data Structures Unit 2 Notes

Complete RGPV exam-oriented notes on Stack, Queue and Recursion. This page explains every important concept in simple language with algorithms, examples, important questions, PYQ trends and exam writing tips.

📘 Detailed Notes

Complete Unit 2 notes written in simple exam-friendly language for quick learning and revision.

Read Document

⭐ Important Questions

High-weightage and repeated questions from Stack, Queue, Recursion and Expression Conversion.

View Questions

📄 PYQ Analysis

Topic-wise previous year question analysis to understand exam trends and scoring areas.

Open Analysis

Unit 2 Syllabus Topics

Introduction to Unit 2

Data Structures Unit 2 mainly focuses on two very important linear data structures: Stack and Queue. A linear data structure stores data elements in a sequence. Stack and Queue are special because they follow strict rules for insertion and deletion. These rules make them useful in programming, operating systems, compilers, memory management and real-life simulation problems.

A stack works on the principle of LIFO, which means Last In First Out. The element inserted last is removed first. A queue works on the principle of FIFO, which means First In First Out. The element inserted first is removed first. Recursion is also related to stack because every recursive function call is stored in the system stack until the base condition is reached.

Exam Tip: Unit 2 is very scoring. Algorithms of push, pop, enqueue, dequeue, infix to postfix conversion, postfix evaluation and circular queue are frequently asked in university exams.

Stack as Abstract Data Type

A stack is an ordered collection of elements in which insertion and deletion are performed only from one end called TOP. Abstract Data Type means we focus on what operations are performed, not how they are internally implemented. In stack ADT, the main operations are push, pop, peek and display.

Basic Stack Operations

Stack Representation

TOP → | 40 | | 30 | | 20 | | 10 | ------

Here 40 is at the top, so it will be removed first. This is why stack follows Last In First Out.

Push Algorithm

PUSH(STACK, TOP, MAX, ITEM) 1. If TOP == MAX - 1, print "Overflow" 2. Else TOP = TOP + 1 3. STACK[TOP] = ITEM 4. Stop

Overflow occurs when we try to insert an element into a full stack. In array-based stack, maximum size is fixed, so overflow must be checked before insertion.

Pop Algorithm

POP(STACK, TOP) 1. If TOP == -1, print "Underflow" 2. Else ITEM = STACK[TOP] 3. TOP = TOP - 1 4. Return ITEM

Underflow occurs when we try to delete an element from an empty stack. This is a common condition that must be written in exam algorithms.

Implementation of Stack

Stack can be implemented using array and linked list. Array implementation is simple but has fixed size. Linked list implementation is dynamic because memory is allocated at runtime.

BasisArray StackLinked List Stack
MemoryFixed sizeDynamic size
OverflowPossible when array is fullOnly when memory is unavailable
ImplementationEasySlightly complex
AccessUses index TOPUses pointer TOP

Multiple Stack

Multiple stack means maintaining more than one stack in a single array. This is done to use memory efficiently. The most common example is two stacks in one array, where one stack grows from left to right and the other grows from right to left.

Stack 1 grows → ← Stack 2 grows | 10 | 20 | | | | 90 | 80 | ↑ ↑ TOP1 TOP2

Overflow occurs only when TOP1 + 1 equals TOP2. This method reduces wastage of array space.

Infix to Postfix Conversion

Infix expression is the normal mathematical expression in which operator is written between operands, such as A+B. Postfix expression is the expression in which operator is written after operands, such as AB+. Computers prefer postfix expression because it can be evaluated easily using stack without brackets and operator precedence confusion.

Operator Precedence

Algorithm

1. Scan the infix expression from left to right. 2. If symbol is operand, add it to postfix expression. 3. If symbol is '(', push it onto stack. 4. If symbol is ')', pop operators until '(' is found. 5. If symbol is operator, pop higher or equal precedence operators from stack, then push current operator. 6. After scanning, pop all remaining operators from stack.

Example

Convert A + B * C into postfix.

Infix: A + B * C Postfix: A B C * +

Multiplication has higher precedence than addition, so B*C is performed first. Therefore the postfix expression becomes ABC*+.

Evaluation of Postfix Expression

Postfix expression is evaluated using stack. When an operand is found, it is pushed into the stack. When an operator is found, required operands are popped from the stack, operation is performed and result is pushed back.

Algorithm

1. Scan postfix expression from left to right. 2. If symbol is operand, push it onto stack. 3. If symbol is operator, pop two operands. 4. Apply the operator. 5. Push the result back into stack. 6. Final stack value is the answer.

Example

Evaluate postfix expression: 23*54*+

2 3 * = 6 5 4 * = 20 6 + 20 = 26 Answer = 26

Recursion

Recursion is a programming technique in which a function calls itself directly or indirectly. Every recursive function must have a base condition. Without a base condition, recursion continues infinitely and causes stack overflow.

Parts of Recursion

Factorial Example

factorial(n) 1. If n == 0, return 1 2. Else return n * factorial(n-1)

For factorial(5), the calls are factorial(5), factorial(4), factorial(3), factorial(2), factorial(1), factorial(0). After reaching the base case, the values return back one by one.

Advantages and Disadvantages

Recursion makes code short and easy for problems like tree traversal, factorial and Fibonacci series. However, it uses extra memory because each function call is stored in stack. It may be slower than iteration for simple problems.

Queue as Abstract Data Type

A queue is a linear data structure in which insertion is performed from the rear end and deletion is performed from the front end. It follows First In First Out order. A real-life example is a ticket counter line where the person who comes first gets service first.

Queue Operations

Queue Diagram

FRONT → | 10 | 20 | 30 | 40 | ← REAR Deletion from FRONT, Insertion from REAR

Enqueue Algorithm

ENQUEUE(QUEUE, FRONT, REAR, MAX, ITEM) 1. If REAR == MAX - 1, print "Overflow" 2. Else if FRONT == -1, set FRONT = 0 3. REAR = REAR + 1 4. QUEUE[REAR] = ITEM

Dequeue Algorithm

DEQUEUE(QUEUE, FRONT, REAR) 1. If FRONT == -1 or FRONT > REAR, print "Underflow" 2. ITEM = QUEUE[FRONT] 3. FRONT = FRONT + 1 4. Return ITEM

Circular Queue, Dequeue and Priority Queue

Circular Queue

A circular queue is an improved version of linear queue in which the last position is connected back to the first position. It solves memory wastage problem of simple queue. In circular queue, rear and front move in circular manner using modulo operation.

REAR = (REAR + 1) % MAX FRONT = (FRONT + 1) % MAX

Double Ended Queue

Dequeue or Double Ended Queue allows insertion and deletion from both front and rear ends. It is more flexible than a simple queue. Input restricted dequeue allows insertion at one end but deletion from both ends. Output restricted dequeue allows deletion at one end but insertion from both ends.

Priority Queue

A priority queue is a special queue where each element has a priority. The element with highest priority is served before lower priority elements. If two elements have same priority, FIFO rule may be followed.

Priority queues are used in CPU scheduling, printer scheduling, graph algorithms and emergency service systems.

Applications of Stack and Queue

Data StructureApplications
StackFunction calls, recursion, expression conversion, postfix evaluation, undo operation, browser back button, parenthesis matching
QueueCPU scheduling, printer queue, call center system, ticket booking, BFS traversal, buffering, simulation
Circular QueueRound robin scheduling, memory buffers, traffic control systems
Priority QueueEmergency handling, Dijkstra algorithm, job scheduling, bandwidth management

Important Questions for RGPV Exams

  1. Define stack as ADT. Explain push and pop operations with algorithms.
  2. Explain array and linked list implementation of stack.
  3. Convert an infix expression into postfix using stack.
  4. Evaluate a postfix expression using stack with example.
  5. Explain recursion with suitable example.
  6. What is stack overflow and underflow?
  7. Define queue as ADT and explain enqueue and dequeue operations.
  8. Differentiate between stack and queue.
  9. Explain circular queue with algorithm.
  10. Explain double ended queue and its types.
  11. What is priority queue? Explain its applications.
  12. Explain applications of queue in computer science.

PYQ Analysis and Topic Weightage

TopicExpected MarksExam FrequencyPreparation Priority
Stack ADT and Operations7-10Very HighMust Prepare
Infix to Postfix Conversion7-10Very HighMust Prepare
Postfix Evaluation5-7HighImportant
Recursion5-7HighImportant
Queue ADT7-10Very HighMust Prepare
Circular Queue7HighImportant
Priority Queue and Dequeue5-7MediumPrepare

Frequently Asked Questions

What is the main difference between stack and queue?

Stack follows LIFO order, while queue follows FIFO order. In stack, insertion and deletion are done from top. In queue, insertion is done from rear and deletion is done from front.

Why is stack used in recursion?

Every function call is stored in the system stack. In recursion, each recursive call waits until the base condition is reached, so stack is used to manage these pending function calls.

Why is circular queue better than linear queue?

Circular queue reuses empty spaces created after deletion. Linear queue may waste memory because rear cannot move back to the beginning.

Which topics are most important from DS Unit 2?

Stack operations, infix to postfix conversion, postfix evaluation, recursion, queue operations, circular queue and priority queue are the most important topics for exams.

Is Unit 2 scoring in RGPV exams?

Yes. Unit 2 is highly scoring because many questions are algorithm-based and example-based. Proper diagrams and step-by-step algorithms can help students score well.

Related Data Structure Units

Previous: Unit 1 Next: Unit 3