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

7-DSA - Queue

Uploaded by

seharamjadnuml
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)
11 views25 pages

7-DSA - Queue

Uploaded by

seharamjadnuml
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/ 25

DATA STRUCTURES

Queue
Queues
 A Queue is a special kind of list, where items are inserted at one
end (the rear) And deleted at the other end (the front).

 Accessing the elements of queues follows a First In,First Out (FIFO)


order.
 Example
 Like customers standing in a check-out line in a store, the first
customer in is the first customer served
Queue – Analogy
 A queue is like a line of people waiting for a bank teller
 The queue has a front and a rear
Queue – Analogy
 New people must enter the queue at the rear
Queue – Analogy
 An item is always taken from the front of the queue
Queues – Examples
 Billing counter
 Booking movie tickets
 Queue for paying bills
 A print queue
 Vehicles on toll-tax bridge
 Luggage checking machine
Queues – Applications
 Operating systems
 Process scheduling in multiprogramming environment
 Controlling provisioning of resources to multiple users (or processing)

 Middleware/Communication software
 Hold messages/packets in order of their arrival
 Messages are usually transmitted faster than the time to process them
 The most common application is in client-server models
 Multiple clients may be requesting services from one or more servers
 Some clients may have to wait while the servers are busy
 Those clients are placed in a queue and serviced in the order of arrival
Basic Operations
 MAKENULL(Q)
 Makes Queue Q be an empty list

 FRONT(Q)
 Returns the first element on Queue Q

 ENQUEUE(x ,Q)
 Inserts element x at the end of Queue Q

 DEQUEUE(Q)
 Deletes the first element of Q

 EMPTY(Q)
 Returns true if and only if Q is an empty queue
Enqueue And Dequeue Operations
Implementation
 Static
 Queue is implemented by an array
 Size of queue remains fix

 Dynamic
 A queue can be implemented as a linked list
 Expand or shrink with each enqueue or dequeue operation
Array Implementation
 Use two counters that signify rear and front

 When queue is empty


 Both front and rear are set to -1

 When there is only one value in the Queue,


 Both rear and front have same index

 While enqueueing increment rear by 1


 While dequeueing, increment front by 1.
Array Implementation Example
Array Implementation Example
Using Circular Queue
 Allow rear to wrap around the array
 Use modular arithmetic

rear = (rear + 1) % queueSize;


Example
How to Determine Empty and Full Queues?
 A counter indicating number of values/items in the queue
Dynamic Implementation
 Refer to Functions of Singly Linked List
 Enqueue??  Add to end
 Dequeue??  Remove from head

You might also like