0% found this document useful (0 votes)
11 views48 pages

Data Structures Lecture 4

The document provides an overview of Queue data structures, detailing their definition, operations, types (Simple, Circular, Priority, and Double-Ended), and implementation using arrays and linked lists. It explains the FIFO principle, basic operations like enqueue and dequeue, and highlights real-life applications and advantages of queues. Additionally, it discusses the limitations of queues, such as their strict ordering and lack of random access.

Uploaded by

mntsr80awdail
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)
11 views48 pages

Data Structures Lecture 4

The document provides an overview of Queue data structures, detailing their definition, operations, types (Simple, Circular, Priority, and Double-Ended), and implementation using arrays and linked lists. It explains the FIFO principle, basic operations like enqueue and dequeue, and highlights real-life applications and advantages of queues. Additionally, it discusses the limitations of queues, such as their strict ordering and lack of random access.

Uploaded by

mntsr80awdail
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
You are on page 1/ 48

Data Structures

Batch_16 _4th semester


CS + IT+ IS Departments
Lecture 4: Queue

By: Hind Ali


12/16/2024 1
Queue Overview
• Queue ADT
• Basic operations of Queue
• Enqueuing, dequeuing etc.
• Type of Queue
• Queue using
• array
• linked list

12/16/2024 2
Queue
• A Queue Data Structure is a fundamental concept in computer
science used for storing and managing data in a specific order.
• Is a linear data structure that follows the principle of “First in,
First out” (FIFO), where the f irst element added to the queue is
the first one to be removed.
• It operates like a line where elements are added at one end (rear)
and removed from the other end (front).
• Queues are com m only used in various algorithm s and
applications for their simplicity and ef ficiency in managing data
flow.

12/16/2024 3
FIFO Principle of Queue Data Structure:
• A Queue is like a line waiting to purchase tickets, where the f irst
person in line is the f irst person served. (i.e. First Come First
Serve).
• Position of the entry in a queue ready to be served, that is, the
f irst entry that will be removed from the queue, is called the
front of the queue(sometimes, head of the queue). Similarly, the
position of the last entry in the queue, that is, the one most
recently added, is called the rear (or the tail) of the queue.

12/16/2024 4
Types of Queues
1. Simple Queue
• A simple queue is the most basic queue. In this queue, the
enqueue operation takes place at the rear, while the dequeue
operation takes place at the front:

• Its applications are process scheduling, disk scheduling,


memory management, IO buffer, pipes, call center phone
systems, and interrupt handling.
12/16/2024 5
2. Circular Queue
• A circular queue permits better memory utilization than a simple
queue when the queue has a fixed size.
• In this queue, the last node points to the first node and creates a
circular connection. Thus, it allows us to insert an item at the
f irst node of the queue when the last node is full and the f irst
node is free.

12/16/2024 6
3. priority queue
• A priority queue is a special kind of queue in which each item
has a predef ined priority of service. In this queue, the enqueue
operation takes place at the rear in the order of arrival of the
items, while the dequeue operation takes place at the front
based on the priority of the items.
• That is to say that an item with a high priority will be dequeued
before an item with a low priority.
• In the case, when two or more items have the same priority, then
they’ll be dequeued in the order of their arrival. Hence, it may or
may not strictly follow the FIFO rule:

12/16/2024 7
4.Double-Ended Queue (Deque)

• A deque is also a special type of queue. In this queue, the


enqueue and dequeue operations take place at both front and
rear. That means, we can insert an item at both the ends and
can remove an item from both the ends. Thus, it may or may not
adhere to the FIFO order:


12/16/2024 8
Basic Operations of Queue Data Structure

 Enqueue (Insert): Adds an element to the rear of the queue.


 Dequeue (Delete): Removes and returns the element from the
front of the queue.
 Peek: Returns the element at the front of the queue without
removing it.
 Rear: This operation returns the element at the rear end without
removing it.
 Empty: Checks if the queue is empty.
 Full: Checks if the queue is full.

12/16/2024 9
Implementation of Queue
• Just as stacks can be implemented as arrays or linked lists, so
with queues.
• Dynamic queues have the same advantages over static queues
as dynamic stacks have over static stacks

12/16/2024 10
Queue using Array

front rear
1 7 5 2
1 7 5 2
0 1 2 3 4 5 6 7
front rear
0 3

12/16/2024 11
Queue using Array
enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4

12/16/2024 12
Queue using Array
enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5

12/16/2024 13
Queue using Array
dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5

12/16/2024 14
Queue using Array
dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5

12/16/2024 15
Queue using Array
enqueue(9)
enqueue(12)

front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
2 7
enqueue(21) ??

12/16/2024 16
Queue using Array
 Basic idea is to picture the array as a circular
array.

0 1
front
front rear
7 2 2
12 5
5 2 6 8 9 12
6 9 2
8 6 3 rear
7
5 4

12/16/2024 17
Queue using Array
enqueue(21)
0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear noElements
0 7
5 4
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}

12/16/2024 18
Queue using Array
enqueue(7)
0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElements
1 8
5 4
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}
12/16/2024 19
Queue using Array
dequeue()
0 1

front rear 21 7 front size


7 2 4 8
12
6 8 9 12 21 7
6 9
8 6 3 rear noElements
1 6
5 4
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
12/16/2024 20
1. Enqueue Operation in Queue using Array
Enqueue() operation in Queue adds (or stores) an element to the
end of the queue,the following steps should be taken to enqueue
(insert) data into a queue:

12/16/2024 21
12/16/2024 22
12/16/2024 23
2. Dequeue Operation in Queue using Array:

Removes the f irst element from the queue,the following


steps are taken to perform the dequeue operation:

12/16/2024 24
12/16/2024 25
12/16/2024 26
3. Front Operation in Queue Data Structure:
The peek() is an operation which is used to retrieve the front
element in the queue, without deleting it. This operation is used to
check the status of the queue with the help of the pointer.
•Check if the queue is empty. If the queue is empty, return -1.
•If the queue is not empty, return the element pointed by the front pointer.

12/16/2024 27
4. Display function

12/16/2024 28
12/16/2024 29
Implementing Queue

 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

12/16/2024 30
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2

12/16/2024 31
Implementing Queue

 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 9

12/16/2024 32
Operations To implement queue using linked list
we need to set the following things before implementing actual
operations.
• Step 1 - Include all the header files which are used in the program.
And declare all the user defined functions.
• Step 2 - Define a 'Node' structure with two members data and next.
• Step 3 - Define two Node pointers 'front' and 'rear' and set both to
NULL.
• Step 4 - Implement the main method by displaying Menu of list of
operations and make suitable function calls in the main method
to perform user selected operation.

12/16/2024 33
12/16/2024 34
enQueue(value) - Inserting an element into the Queue

We can use the following steps to insert a new node into the
queue...
Step 1 - Create a newNode with given value and set 'newNode →
next' to NULL.
Step 2 - Check whether queue is Empty (rear&front == NULL)
Step 3 - If it is Empty then, set front = newNode and rear =
newNode.

Step 4 - If it is Not Empty then, set rear next = newNode and
rear = newNode.

12/16/2024 35
12/16/2024 36
12/16/2024 37
deQueue() - Deleting an Element from Queue

We can use the following steps to delete a node from the queue...
• Step 1 - Check whether queue is Empty (front == NULL).
• Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is not
possible!" and terminate from the function
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set
it to 'front'.

• Step 4 - Then set 'front = front next' and delete 'temp'
(free(temp)).

12/16/2024 38
12/16/2024 39
12/16/2024 40
12/16/2024 41
display() - Displaying the elements of Queue

We can use the following steps to display the elements (nodes)


of a queue...
• Step 1 - Check whether queue is Empty (front == NULL).
• Step 2 - If it is Empty then, display 'Queue is Empty!!!' and
terminate the function.
• Step 3 - If it is Not Empty then, define a Node pointer 'temp' and
initialize with front.
→ →
• Step 4 - Display 'temp data --->' and move it to the next node.
Repeat the same until 'temp' reaches to 'rear' (temp next !=
NULL).

• Step 5 - Finally! Display 'temp data ---> NULL'.

12/16/2024 42
12/16/2024 43
12/16/2024 44
Real-Life Applications of Queues
 Managing Waiting Lines in Systems
 Queues are used in printers to handle multiple print requests. Each
job is processed in the order it was received:Printing Jobs.
 Distributed Systems
 Customer Service Systems
 Multiplayer games use queues to organize players waiting to join a
game or server :Online Gaming
 Operating Systems:: Scheduling
Queues manage tasks or processes waiting to be executed by the CPU.
Handling input/output requests.
 Search Engines
 Processing customer orders in the sequence they are placed. Order
Management::E-Commerce Applications

12/16/2024 45
Advantages of Queue
• Efficient data processing: A queue can be used to efficiently
process data in the order it was received. For example, in a
computer system, a queue can be used to schedule processes
in the order they were submitted.
• Resource management: A queue can be used to manage
resources that are shared among multiple processes or threads.
For example, a printer can use a queue to manage print jobs.
• Buffering: A queue can be used to buffer incoming data so that
it can be processed at a later time. For example, a network
device can use a queue to buffer incoming data packets before
forwarding them to their destination.
• Memory management: A queue can be used to manage
memory by allocating and deallocating memory blocks in
12/16/2024 46
sequential order.
Disadvantages of Queue
• Limited flexibility: Queues follow a strict FIFO order, meaning the
element that enters first is the first one to be removed. This lack of
flexibility can be a disadvantage in some use cases where other
priorities or requirements must be considered.
• No random access: Unlike arrays or linked lists, queues do not allow
random access to the elements. The user can only access the first
element in the queue, and to access any other element, they need to
remove all the elements that come before it. This can be a disadvantage
when the user needs to access an element in the middle of the queue.
• Overhead: Queues require extra overhead to maintain the order of
elements. Whenever an element is added or removed from the queue, all
the other elements must be shifted accordingly, which can be time-
consuming for large queues.
• Limited size: In some implementations, queues have a limited size,
which can be a disadvantage when dealing with large amounts of data.
Once the queue reaches its maximum size, it can no longer accept new
elements.
• No search operation: Queues do not provide a search operation. The
user cannot search for a specific element in the queue they can only
remove the elements in the order they were added and hope to find the
element they are looking for.
12/16/2024 47
END

12/16/2024 48

You might also like