0% found this document useful (0 votes)
2 views37 pages

Module 3 Stacks and Queue

Uploaded by

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

Module 3 Stacks and Queue

Uploaded by

shivaprasadm6363
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Definition of Stack

A Stack is a linear data structure that follows the principle:


LIFO (Last In, First Out)
This means:
● The last element inserted is the first one to be removed
Example:

Think of a stack of plates:


● You place a plate on top push
● You remove the top plate pop

Basic Operations of Stack


1. Push
o Adds an element to the top of the stack
o Example: Push(10)
2. Pop
o Removes the top element from the stack
o Example: Pop() removes last inserted element
3. Peek / Top
o Shows the top element without removing it
4. isEmpty
o Checks if the stack is empty
5. isFull
o Checks if the stack is full (in array implementation)

Basic Structure
Top [30]

[20]

[10]
● Here, 30 is the top element
● Stack grows upward

Working Example

1
Step-by-step:
Push(10) [10]

Push(20) [10, 20]

Push(30) [10, 20, 30]

Pop() removes 30 [10, 20]

Peek() 20

Characteristics of Stack
● Follows LIFO order
● Insertion & deletion happen only at one end (top)
● Simple and efficient structure
● Can be implemented using:
o Arrays
o Linked Lists

Applications of Stack
● Function calls (recursion)
● Expression evaluation (infix, postfix)
● Undo/Redo operations
● Parenthesis checking
● Backtracking (e.g., maze solving)

Advantages
Easy to implement

Fast operations (O(1))

Useful in many algorithms

Disadvantages
Limited size (array implementation)

2
No direct access to middle elements

1. Array Representation of Stack


Concept
● Stack is implemented using an array
● A variable called top keeps track of the last inserted element

Structure
Index: 0 1 2 3 4

[10] [20] [30] [ ] [ ]

top

Basic Operations

Push Operation
● Add element at top + 1
top = top + 1

stack[top] = value

Pop Operation
● Remove element from top
value = stack[top]

3
top = top - 1

Peek Operation
return stack[top]

Conditions
● Overflow when top == size - 1
● Underflow when top == -1

Advantages
Simple to implement

Fast access (O(1))

Disadvantages
Fixed size (cannot grow dynamically)

Wastage of memory

2. Linked List Representation of Stack


Concept
● Stack is implemented using a linked list
● Each node contains:
o Data
o Pointer to next node
● Top points to the first node

4
Structure
Top [30 | • ] [20 | • ] [10 | NULL]

Basic Operations
Push Operation
Create new node

new->data = value

new->next = top

top = new

Pop Operation
temp = top

top = top->next

delete temp

Peek Operation
return top->data

Conditions
● Overflow only if memory is full
● Underflow when top == NULL

Advantages
Dynamic size (grows/shrinks)

5
No memory wastage

Disadvantages
Extra memory for pointers

Slightly complex

Key Differences
Array
Feature Linked Stack
Stack
Size Fixed Dynamic
Contiguo
Memory Non-contiguous
us
When Rare (memory
Overflow
full issue)
Implementati
Easy Moderate
on

Conclusion
● Use array when size is known
● Use linked list when size is dynamic

Feature PUSH POP


Insert element into Remove element from
Meaning
stack stack
Operation
Insertion Deletion
type
Position Always at the top Always from the top
Effect Increases stack size Decreases stack size
Condition Overflow (if full) Underflow (if empty)

Polish Notation (Prefix Notation) – Stack Application Notes


Polish Notation is a method of writing arithmetic expressions without using brackets.

In this notation, the operator is placed before the operands.


It was introduced by the Polish mathematician Jan Łukasiewicz, hence the name
Polish Notation.

1. Definition

6
Polish Notation (Prefix Notation) is a form of writing expressions where the operator
comes before the operands.
General Form:
Operator Operand1 Operand2
Example:
+AB
This means:
A+B

2. Example Expressions
Infix Prefix
Expression (Polish)
A+B +AB
A−B -AB
A×B *AB
A/B /AB
(A + B) × C *+ABC
A + (B × C) +A*BC

3. Why Polish Notation is Used


Advantages:
1. No brackets required
2. No precedence confusion
3. Easy evaluation using stack
4. Used in compilers and expression parsing

4. Evaluation of Prefix Expression Using Stack


Steps
1. Scan the expression from right to left
2. If the symbol is an operand, push it into the stack
3. If the symbol is an operator
o Pop two operands from the stack
o Perform the operation
o Push the result back to the stack
4. Continue until expression ends
5. Final value in stack is the result

7
Example
Evaluate:
+9*23
Step-by-step:
St Sym Sta
Action
ep bol ck
1 3 Push 3
2 2 Push 2,3
2×3=6
3 * 6
push
4 9 Push 9,6
5 + 9+6=15 15
Result = 15

5. Conversion of Infix to Prefix


Example:
(A + B) * C
Steps:
1. Reverse the infix expression
2. Replace brackets
3. Convert to postfix
4. Reverse the result
Result:
*+ABC

6. Applications of Polish Notation


1. Expression evaluation using stack
2. Compiler design
3. Syntax parsing
4. Expression tree construction
5. Programming language interpreters

7. Prefix vs Infix vs Postfix


Exam Operator
Notation
ple Position
Between
Infix A+B
operands
Before
Prefix (Polish) +AB
operands

8
Postfix (Reverse
Polish) AB+ After operands

Summary:

Polish Notation is a prefix form of writing expressions where operators appear before
operands. It removes the need for parentheses and is efficiently evaluated using a
stack data structure.

Conversion of Infix to Postfix (Stack Application)


1. Infix Expression
An infix expression is the normal form of writing arithmetic expressions where the
operator is between operands.
Example:
A+B

A+B*C

(A + B) * C

2. Postfix Expression (Reverse Polish Notation)


In postfix notation, the operator comes after the operands.
Example:
Post
Infix
fix
A+B AB+
A+B* ABC
C *+
(A + B) AB+
*C C*

3. Operator Precedence
Operat Preceden
or ce
() Highest
^ High
*/ Medium
+- Low

4. Algorithm for Infix Postfix Conversion (Using Stack)


1. Create an empty stack
2. Scan the infix expression from left to right

9
3. If the symbol is an operand, add it to the postfix expression
4. If the symbol is ( push it to the stack
5. If the symbol is ), pop from stack until ( appears
6. If the symbol is an operator
o Pop operators from stack with greater or equal precedence
o Then push the current operator
7. After scanning the expression, pop remaining operators from stack

5. Example Conversion
Convert:
A+B*C
Symb Sta Post
Action
ol ck fix
A Add to output A
+ Push + A
B Add to output + AB
Push (higher
* +* AB
precedence)
C Add to output +* ABC
ABC
End Pop all
*+
Postfix Expression:
ABC*+

6. Another Example
Convert:
(A + B) * C
Symb Sta Post
Action
ol ck fix
( Push (
A Output ( A
+ Push (+ A
B Output (+ AB
Pop
) AB+
until (
* Push * AB+
AB+
C Output *
C
AB+
End Pop
C*
Postfix Expression

10
AB+C*

7. Applications
● Expression evaluation in stack
● Compiler design
● Syntax parsing
● Expression tree creation
● Used in calculators and interpreters

Short Definition (for exams):

Infix to postfix conversion is the process of converting a normal arithmetic


expression where operators are between operands into postfix form where operators
appear after operands using a stack data structure.

Evaluation of Postfix Expression (Stack Application)


1. Definition
A postfix expression (Reverse Polish Notation) is an expression where the operator
comes after the operands.
Example:
AB+
means
A+B
Evaluation of postfix expressions is easily done using a stack.

2. Algorithm to Evaluate Postfix Expression


1. Create an empty stack.
2. Scan the postfix expression from left to right.
3. If the symbol is an operand, push it into the stack.
4. If the symbol is an operator:
o Pop two operands from the stack.
o Perform the operation.
o Push the result back into the stack.
5. Repeat until the expression ends.
6. The final value in the stack is the result.

11
3. Example
Evaluate the postfix expression:
23*54*+9-
Step-by-Step Evaluation
Symb Operatio Sta
ol n ck
2 Push 2
3 Push 2,3
2×3=6
* 6
push
5 Push 6,5
6,5,
4 Push
4
6,2
* 5×4=20
0
+ 6+20=26 26
26,
9 Push
9
- 26−9=17 17
Result
17

4. Another Simple Example


Evaluate:
23+5*
Steps:
Symb Operati Sta
ol on ck
2 Push 2
3 Push 2,3
+ 2+3=5 5
5 Push 5,5
* 5×5=25 25
Result:
25

5. Advantages
● No parentheses required
● Faster evaluation using stack
● Used in compilers and calculators
● Avoids precedence problems

12
Short Exam Definition:

Postfix expression evaluation is the process of computing the result of a postfix


expression by scanning from left to right and using a stack to store operands and
intermediate results.

Applications of Stack (Data Structure)


A stack is a linear data structure that follows the LIFO principle (Last In First Out).

It is widely used in many computer science applications.

1. Expression Evaluation
Stacks are used to evaluate arithmetic expressions.
Examples:
● Postfix evaluation
● Prefix evaluation
Example:
23+5*
Using stack:
2+3=5

5 * 5 = 25

2. Infix to Postfix / Prefix Conversion


Stacks help convert expressions from infix form (normal mathematical form) to
postfix or prefix form.
Example:
Post
Infix
fix
A+B AB+
(A+B) AB+
*C C*
This is widely used in compilers and interpreters.

3. Parenthesis Checking
Stacks are used to check whether parentheses, brackets, and braces are balanced.
Example:
Valid:
{ (A + B) * C }
Invalid:
(A + B * C
Steps:

13
● Push opening brackets into stack.
● Pop when closing brackets appear.

4. Function Calls and Recursion


Stacks manage function calls in programs.
When a function is called:
● Its return address and variables are stored in a call stack.
Example:
main()

function1()

function2()
When functions finish, they return in reverse order.

5. Backtracking
Stacks help in solving problems where we need to go back to previous steps.
Examples:
● Maze solving
● Puzzle solving
● Depth First Search (DFS)

6. Undo / Redo Operations


Stacks are used in applications like:
● Text editors
● Image editing software
Example:
Undo remove last action

Redo restore action


Applications:
● Word processors
● Graphic design software

7. Syntax Parsing
Stacks help check syntax errors in programming languages.

14
Example:
● Missing brackets
● Incorrect expression structure
Used in compiler design.

8. Reversing Data
Stacks can reverse strings or data.
Example:
Input:
HELLO
Output using stack:
OLLEH

Short Exam Answer (Important):


Applications of Stack:
1. Expression evaluation
2. Infix to postfix/prefix conversion
3. Parenthesis checking
4. Function calls and recursion
5. Backtracking algorithms
6. Undo and redo operations
7. Syntax parsing in compilers
8. Reversing strings or data

Recursion (Data Structures)


1. Definition
Recursion is a technique in which a function calls itself repeatedly to solve a problem.
It solves a big problem by breaking it into smaller subproblems.
A recursive function must have:
1. Base case – condition to stop recursion
2. Recursive case – function calling itself

2. General Structure of Recursion


function recursion()

if(base condition)

15
return value;

else

recursion(); // function calls itself

3. Example: Factorial Using Recursion


Factorial formula:
n! = n × (n−1)!
Example:
5! = 5 × 4 × 3 × 2 × 1
Algorithm
1. If n = 0 or 1, return 1
2. Otherwise return n × factorial(n−1)
Example Steps
fact(5)

= 5 × fact(4)

= 5 × 4 × fact(3)

= 5 × 4 × 3 × fact(2)

= 5 × 4 × 3 × 2 × fact(1)

=5×4×3×2×1

= 120

C Program Example
#include<stdio.h>

int factorial(int n)

if(n==0 || n==1)

return 1;

16
else

return n * factorial(n-1);

int main()

int n=5;

printf("Factorial = %d", factorial(n));

return 0;

}
Output:
Factorial = 120

4. Recursion and Stack


Recursion internally uses a stack (call stack).
Each recursive call is stored in the stack until the base condition is reached, then
functions return in reverse order.
Example:
main()

fact(5)

fact(4)

fact(3)

fact(2)

fact(1)

17
Then it returns back step by step.

5. Advantages of Recursion
● Makes code simpler and shorter
● Useful for complex problems
● Used in tree and graph algorithms

6. Disadvantages
● Uses more memory
● Can be slower than iteration
● May cause stack overflow if recursion is too deep

7. Applications of Recursion
1. Factorial calculation
2. Tower of Hanoi
3. Tree traversal
4. Graph algorithms
5. Searching and sorting
6. Mathematical computations

Short Exam Definition:

Recursion is a programming technique where a function calls itself repeatedly until a


base condition is satisfied to solve a problem.

Implementation of Recursive Procedures Using Stack


1. Definition
Recursion is a process in which a function calls itself to solve a problem.

In a computer system, recursion is implemented using a stack data structure called


the call stack.
Each time a function calls itself, the system pushes information about that function
into the stack.

2. Information Stored in Stack


When a recursive function is called, the following details are stored in the stack:

18
● Function parameters
● Local variables
● Return address (where the function should return)
● State of the function
This group of information is called a stack frame.

3. Working of Recursion Using Stack


1. When a function is called, its activation record (stack frame) is pushed onto
the stack.
2. If the function calls itself again, another stack frame is pushed.
3. This continues until the base condition is reached.
4. After reaching the base case, the function starts returning.
5. Stack frames are popped one by one until the program finishes.

4. Example: Factorial Using Recursion


Function:
fact(n) = n × fact(n−1)

fact(1) = 1
Example:
fact(4)
Step 1: Function Calls (Push)
Step Stack
Call
fact(4)
fact(4)
Call
fact(4), fact(3)
fact(3)
Call
fact(4), fact(3), fact(2)
fact(2)
Call fact(4), fact(3), fact(2),
fact(1) fact(1)

Step 2: Returning Values (Pop)


Operati Res
Step
on ult
fact(
return 1
1)
fact(
2×1 2
2)

19
fact(
3×2 6
3)
fact(
4×6 24
4)
Final result:
24

5. Stack Representation
Top

-----

fact(1)

fact(2)

fact(3)

fact(4)

-----

Bottom
After the base condition, elements are popped in reverse order.

6. Advantages
● Simplifies complex problems
● Easy to implement algorithms like tree traversal and Tower of Hanoi

7. Disadvantages
● Uses more memory because of stack storage
● Can cause stack overflow if recursion depth is large

Short Exam Answer:

Recursive procedures are implemented using a stack (call stack). Each recursive call
creates a stack frame that stores function parameters, local variables, and return
address. When the base condition is reached, the stack frames are popped in reverse
order to return the final result.

Tower of Hanoi (Recursion Application)


1. Definition

20
Tower of Hanoi is a mathematical puzzle used to demonstrate recursion.

It consists of three rods (pegs) and n disks of different sizes.


The disks are initially placed on one rod in decreasing size order (largest at bottom,
smallest at top).

2. Components
● Source rod (A) – where disks are initially placed
● Auxiliary rod (B) – temporary rod
● Destination rod (C) – final rod where disks must be moved

3. Rules of Tower of Hanoi


1. Only one disk can be moved at a time.
2. Only the top disk can be moved.
3. A larger disk cannot be placed on a smaller disk.

4. Recursive Algorithm
To move n disks from Source (A) to Destination (C) using Auxiliary (B):
1. Move n−1 disks from A B using C
2. Move 1 disk from A C
3. Move n−1 disks from B C using A

Algorithm
TOH(n, source, auxiliary, destination)

if n == 1

move disk from source to destination

else

TOH(n-1, source, destination, auxiliary)

move disk from source to destination

TOH(n-1, auxiliary, source, destination)

5. Example (3 Disks)

21
Move 3 disks from A to C using B.
St Mov
ep e
A
1
C
A
2
B
C
3
B
A
4
C
B
5
A
B
6
C
A
7
C
Total moves = 7

6. Formula for Number of Moves


Minimum moves required:

where n = number of disks


Example:
Dis Mov
ks es
1 1
2 3
3 7
4 15

7. Applications
● Understanding recursion concepts
● Algorithm design
● Used in problem-solving and programming exercises

Short Exam Definition:

The Tower of Hanoi is a puzzle that involves moving disks from one rod to another
using recursion while following rules that only one disk can be moved at a time and
a larger disk cannot be placed on a smaller disk.

22
Queue (Data Structure)
1. Definition
A Queue is a linear data structure that follows the FIFO principle (First In First Out).

The element that is inserted first will be removed first.


● Insertion operation is called Enqueue
● Deletion operation is called Dequeue

2. Basic Operations
1. Enqueue – Insert an element into the queue
2. Dequeue – Remove an element from the queue
3. Peek / Front – Display the first element
4. isEmpty – Check if queue is empty
5. isFull – Check if queue is full

3. Example of Queue
Real-life example: Queue in a ticket counter
Person1 Person2 Person3 Person4
● Person1 enters first and leaves first.
● Person4 enters last and leaves last.

4. Queue Representation
Front 10 20 30 40 # Rear
● Insertion happens at Rear
● Deletion happens at Front

5. Small C Program for Queue


#include<stdio.h>

23
#define MAX 5

int queue[MAX];

int front = -1;

int rear = -1;

void enqueue(int value)

if(rear == MAX-1)

printf("Queue Overflow\n");

else

if(front == -1)

front = 0;

rear++;

queue[rear] = value;

printf("%d inserted\n", value);

void dequeue()

24
if(front == -1 || front > rear)

printf("Queue Underflow\n");

else

printf("%d deleted\n", queue[front]);

front++;

void display()

int i;

if(front == -1)

printf("Queue is empty\n");

else

for(i = front; i <= rear; i++)

printf("%d ", queue[i]);

int main()

25
{

enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

return 0;

6. Sample Output
10 inserted

20 inserted

30 inserted

10 20 30

10 deleted

20 30

Short Exam Answer:

A queue is a linear data structure that follows the FIFO (First In First Out) principle
where insertion takes place at the rear and deletion takes place at the front.

Array Representation of Queue


1. Definition

26
A Queue is a linear data structure that follows the FIFO (First In First Out) principle.

In array representation, the queue elements are stored in a linear array, and two
variables front and rear are used to track the positions of elements.
● Front points to the first element
● Rear points to the last element

2. Queue Structure Using Array


Index: 0 1 2 3 4

-----------------------

Queue: |10 | 20 | 30 | 40 | |

-----------------------

! !

Front Rear
● Insertion (Enqueue) happens at rear
● Deletion (Dequeue) happens at front

3. Operations on Queue
1. Enqueue (Insertion)
Steps:
1. Check if rear = MAX − 1 Queue Overflow
2. If queue is empty, set front = 0
3. Increment rear
4. Insert element at queue[rear]

2. Dequeue (Deletion)
Steps:
1. Check if front = -1 or front > rear Queue Underflow
2. Delete element at queue[front]
3. Increment front

4. Algorithm
Enqueue

27
if rear == MAX-1

print "Queue Overflow"

else

if front == -1

front = 0

rear = rear + 1

queue[rear] = item
Dequeue
if front == -1 OR front > rear

print "Queue Underflow"

else

item = queue[front]

front = front + 1

5. Example
Initial:
Front = -1

Rear = -1
After inserting 10, 20, 30:
Index: 0 1 2

-------------

Queue: |10 |20 |30 |

-------------

! !

Front Rear
After deleting one element:
Queue: |10 |20 |30 |

! !

Front Rear

28
10 is removed.

6. Advantages
● Simple to implement
● Easy memory allocation
● Fast insertion and deletion

7. Disadvantages
● Memory wastage when front moves forward
● Cannot reuse empty spaces in simple queue
● Can cause false overflow

Linked List Representation of Queue


1. Definition
In linked list representation, a queue is implemented using a linked list where each
element (node) contains:
● Data
● Link (pointer) to the next node
Two pointers are used:
● Front points to the first element
● Rear points to the last element
The queue still follows the FIFO (First In First Out) principle.

2. Structure of Node
Each node contains:
[ Data | Next ]
Example queue:
Front [10|•] [20|•] [30|NULL] # Rear
● Insertion (Enqueue) happens at rear
● Deletion (Dequeue) happens at front

3. Enqueue Operation (Insertion)


Steps:
1. Create a new node.

29
2. Insert the data into the node.
3. If queue is empty:
o Front = Rear = new node
4. Otherwise:
o Rear next = new node
o Rear = new node

4. Dequeue Operation (Deletion)


Steps:
1. Check if Front = NULL Queue Underflow
2. Store the front node
3. Move Front = Front next
4. Delete the old node

5. Algorithms
Enqueue
Create newnode

newnode->data = value

newnode->next = NULL

if front == NULL

front = rear = newnode

else

rear->next = newnode

rear = newnode

Dequeue
if front == NULL

print "Queue Underflow"

else

30
temp = front

front = front->next

delete temp

6. Example
Insert elements 10, 20, 30
After insertion:
Front 10 20 30 # Rear
After deleting one element:
Front 20 30 # Rear
Element 10 is removed.

7. Advantages
● Dynamic memory allocation
● No queue overflow (until memory is full)
● Memory is used efficiently

8. Disadvantages
● Requires extra memory for pointer
● Implementation is slightly complex compared to array

Types of Queue (Data Structure)


A queue is a linear data structure that follows the FIFO (First In First Out) principle.

There are several types of queues depending on how elements are inserted and
removed.

1. Simple Queue (Linear Queue)


In a simple queue, insertion happens at the rear and deletion happens at the front.
Example:
Front 10 20 30 40 # Rear
Operations:
● Enqueue insert at rear
● Dequeue delete from front
Disadvantage:

Unused spaces may occur when elements are deleted.

31
2. Circular Queue
In a circular queue, the last position is connected back to the first position to form a
circle.
Example:
Front 10 20 30

! ∀

# # # # #
Advantages:
● Efficient use of memory
● Avoids memory wastage in linear queues

3. Priority Queue
In a priority queue, elements are removed based on priority, not only by order.
Example:
Eleme Priori
nt ty
A 1
B 3
C 2
Removal order:
A C B
Applications:
● CPU scheduling
● Task management systems

4. Double Ended Queue (Deque)


A Deque (Double Ended Queue) allows insertion and deletion at both ends.
Example:
Front 10 20 30 Rear
Two types of deque:
1. Input Restricted Deque
● Insertion allowed only at one end
● Deletion allowed at both ends
2. Output Restricted Deque
● Deletion allowed only at one end
● Insertion allowed at both ends

32
Summary Table
Type Insertion Deletion
Simple
Rear Front
Queue
Circular
Rear Front
Queue
Priority Based on Based on
Queue priority priority
Deque Both ends Both ends

Operations on Queue (Data Structure)


A queue follows the FIFO (First In First Out) principle where the first inserted element
is removed first.

The main operations performed on a queue are:

1. Enqueue (Insertion)
Enqueue means adding an element to the queue.

Insertion always happens at the rear.


Steps
1. Check if rear = MAX − 1 Queue Overflow
2. If queue is empty, set front = 0
3. Increment rear
4. Insert element at queue[rear]
Example:
Before insertion

Front 10 20 30 # Rear

After enqueue(40)

Front 10 20 30 40 # Rear

33
2. Dequeue (Deletion)
Dequeue means removing an element from the queue.

Deletion always happens at the front.


Steps
1. Check if front = -1 or front > rear Queue Underflow
2. Remove element from queue[front]
3. Increment front
Example:
Before deletion

Front 10 20 30 40 # Rear

After dequeue

Front 20 30 40 # Rear
10 is removed.

3. Peek / Front
This operation displays the first element of the queue without removing it.
Example:
Queue: 10 20 30

Peek = 10

4. isEmpty
Checks whether the queue is empty.
Condition:
front == -1 OR front > rear

5. isFull
Checks whether the queue is full.
Condition:
rear == MAX - 1

Summary of Queue Operations

34
Operati
Description
on
Enqueu
Insert element at rear
e
Dequeu Delete element from
e front
Peek Display first element
Check if queue is
isEmpty
empty
isFull Check if queue is full

Applications of Queue (Data Structure)


A queue is a linear data structure that follows the FIFO (First In First Out) principle.

It is widely used in many real-life and computer science applications.

1. CPU Scheduling
Queues are used in CPU scheduling in operating systems.
Example:
● Processes waiting for CPU execution are stored in a ready queue.
● The process that arrives first gets executed first.
Example order:
P1 P2 P3 P4

2. Printer Queue
In a computer system, print jobs are stored in a printer queue.
Example:
User1 User2 User3
The first document sent to the printer is printed first.

3. Process Scheduling in Operating Systems


Queues are used to manage different types of processes such as:
● Ready queue
● Waiting queue
● Job queue
These queues help the operating system handle multiple tasks efficiently.

4. Breadth First Search (BFS) in Graphs


Queues are used in Breadth First Search algorithms to visit nodes level by level.

35
Example traversal order:
Node1 Node2 Node3 Node4

5. Handling Requests in Servers


Queues are used in web servers and network systems to handle multiple user
requests.
Example:
Request1 Request2 Request3
Each request is processed in the order it arrives.

6. Simulation of Real-Life Queues


Queues are used to simulate real-world waiting lines such as:
● Ticket counters
● Bank queues
● Call centers
● Supermarket billing
Example:
Customer1 Customer2 Customer3

7. Data Transfer and Buffering


Queues are used in I/O buffering for transferring data between devices such as:
● Keyboard input
● Network data packets
● Disk operations

Summary
Application Description
Managing process
CPU Scheduling
execution
Printer Queue Managing print jobs
Process
Handling tasks in OS
Scheduling
BFS Algorithm Graph traversal
Handling multiple
Server Requests
requests
Real-Life
Bank, ticket counters
Queues
Data Buffering Input/output operations

36
37

You might also like