You are on page 1of 33

ICS 121 DS

Queue
Overview
• Consider a situation where you have to create an application with the following
set of requirements:
Application should serve the requests of multiple users.
At a time, only one request can be processed.
The request, which came first should be given priority.
• How can you solve this problem? retrieved or saved in the order of their arrival.
• A data structure called queue stores and retrieves data in the order of its arrival.
• A queue is also called a FIFO list.
Overview .. Contd.,
• The queue have 2 ends: FRONT = DELETION and REAR = INSERTION

During the INSERT or REAR operation we have to check the condition for OVERFLOW.
During the DELETE or FRONT operation we have to check the condition for UNDERFLOW.
Conceptual View of a Queue

Adding an element
Front of queue

New element is added


to the rear of the queue
Conceptual View of a Queue

Removing an element

New front element of queue

Element is removed
from the front of the
queue
Queue Concept
Applications
The concept of queue has many applications in real-life:
• If a train ticket is in the waiting list (such as W/L1), it means the ticket is in a queue of tickets
waiting to get confirmed, as per the increasing order of waiting numbers. If a confirmed ticket is
cancelled, the W/ L1 numbered ticket is removed from the FRONT of the waiting queue and
confirmed.
• Sometimes on calling a customer service centre, the Interactive Voice Response System (IVRS)
tells us to wait till a support person is available. Here the call is put into a queue of customers
waiting to be serviced.
• Imagine there is a single-lane one-way road, then the vehicle that entered first will exit first,
following the concept of queue. Likewise, vehicles in a highway toll tax booth are served following
the principle of FIFO.
Applications .. Contd.,
Application of queue in computer science
• Suppose there is a web-server hosting a web-site to declare result(s). This server can handle a
maximum of 50 concurrent requests to view result(s). So, to serve thousands of user requests, a
Queue would be the most appropriate data structure to use.
• Some Operating Systems (OS) are required to handle multiple tasks called - jobs, seeking to use
the processor. But we know that a processor can handle only one task at a time. Therefore, in a
multitasking operating system, jobs are lined up (queued) and then given access to the processor
according to FIFO basis.
• When we send print commands from multiple files from the same computer or from different
computers using a shared printer. The OS puts these print requests in a queue and sends them to
the printer one by one on a FIFO basis.
Insertion or Enqueue
• It refers to the addition of an item in the queue.
• Suppose you want to add an item F in the following queue.
• Since the items are inserted at the rear end, therefore, F is inserted
after D.
• Now F becomes the rear end.
Deletion or Dequeue
• It refers to the deletion of an item from the queue.
• Since the items are deleted from the front end, therefore, item B is
removed from the queue.
• Now A becomes the front end of the queue.
Conditions: Linear Queue
• OVERFLOW If one can try to insert an element with an filled QUEUE than that situation will be
called as the OVERFLOW.
• Condition for OVERFLOW
Rear = size -1 (for the QUEUE starts with 0)
Rear = size (for the QUEUE starts with 1)
• UNDERFLOW If one can try to delete an element from an empty QUEUE than that situation will be
called as the UNDERFLOW.
• Condition for UNDERFLOW
Front = -1 (for the QUEUE starts with 0)
Front = 0 (for the QUEUE starts with 1)
• CONDITION FOR EMPTY QUEUE
Front = -1 and Rear = -1 [for the QUEUE starts with 0]
Front = 0 and Rear = 0 [for the QUEUE starts with 1]
Example: Queue Insertion
Example: Queue Deletion
Operations on Queue
The data structure queue supports the following operations:
• ENQUEUE: is used to insert a new element to the queue at the rear end. We can insert
elements in the queue till there is space in the queue for adding more elements.
• DEQUEUE: is used to remove one element at a time from the front of the queue. We can
delete elements from a queue until it is empty.
• To perform enqueue and dequeue efficiently on a queue, following operations are also
required:
• IS EMPTY: used to check whether the queue has any element or not, so as to avoid
Underflow exception while performing dequeue operation.
• PEEK: used to view elements at the front of the queue, without removing it from the
queue.
• IS FULL: used to check whether any more elements can be added to the queue or not, to
avoid Overflow exceptions while performing enqueue operation.
Algorithm For Insert Operation
• INSERT(QUEUE[SIZE], FRONT, REAR, NUM) [QUEUE[SIZE] is the QUEUE]
STEP 1 : IF (REAR = SIZE – 1) THEN : //Q is full [NUM is the Number to Search]
WRITE : “OVERFLOW” [FRONT & REAR are the positions of the Queue]
RETURN
[END OF IF] 1. Check if the queue is full.
2. If the queue is full, produce overflow error and exit
STEP 2 : IF (REAR = -1) THEN : // Q is empty
3. If the queue is not full, increment rear pointer to point to
FRONT := 0
the next empty space.
REAR :=0
4. Add data element to the queue location, where the rear is
ELSE : pointing.
REAR :=REAR+1 5. Return success.
[END OF IF]
STEP 3: QUEUE[REAR] :=NUM
STEP 4: RETURN
Algorithm For Delete Operation
• DELET(QUEUE[SIZE], FRONT, REAR) [QUEUE[SIZE] is the QUEUE]
STEP 1 : IF (FRONT = -1) THEN : [FRONT & REAR are the positions of the Queue]
WRITE : “UNDERFLOW” //Q empty
1. Check if the queue is empty.
RETURN 2. If the queue is empty, produce underflow error and exit.
[END OF IF] 3. If the queue is not empty, access the data where front is
STEP 2 : IF (FRONT ==REAR ) THEN : // Q one element pointing.
FRONT := -1 4. Increment front pointer to the next available data
REAR :=-1 element.
ELSE : 5. Return success
FRONT := FRONT +1
[END OF IF]
STEP 3 : WRITE: QUEUE[FRONT]
STEP 4: RETURN
Algorithm For Traverse Operation
• TRAVERSE(QUEUE[SIZE], FRONT, REAR) [QUEUE[SIZE] is the QUEUE]
STEP 1 : IF (FRONT = -1) THEN : [FRONT & REAR are the positions of the Queue]
WRITE : “ QUEUE IS EMPTY ”
RETURN
[END OF IF]
STEP 2 : SET I:=0
STEP 3 : REPEAT FOR I = FRONT TO REAR
WRITE : QUEUE[I]
[END OF LOOP]
STEP 4: RETURN
Algorithm For Peek Operation
• PEEK(QUEUE[SIZE], NUM, FRONT, REAR)
STEP-1 : IF (REAR = - 1) THEN : [QUEUE[SIZE] is the Queue]
WRITE : “QUEUE IS EMPTY” [NUM is the Number to Queue]
RETURN [FRONT & REAR are the positions of the Queue]
[END OF IF]
STEP-2 : SET I: = 0
STEP-3 : REPEAT FOR I = FRONT TO REAR
IF (NUM = QUEUE[I]) THEN:
WRITE : “NUMBER IS FOUND AT”
WRITE : I+1
WRITE : “POSITION”
RETURN
[END OF IF]
IF I= REAR THEN:
WRITE : “NUMBER IS NOT FOUND”
[END OF IF]
[END OF LOOP]
STEP-4 : RETURN
Algorithm For Update Operation
• UPDATE(QUEUE[SIZE], NUM, FRONT, REAR)
[QUEUE[SIZE] is the QUEUE]
STEP-1 : IF (REAR = - 1) THEN : [NUM is the Number to Update]
WRITE : “QUEUE IS EMPTY” [FRONT & REAR is the position of the Queue]
RETURN
[END OF IF]
STEP-2 : SET I: =0
STEP-3 : REPEAT FOR I = FRONT TO REAR
IF (NUM = QUEUE[I]) THEN:
QUEUE[I] = NEW NUM
RETURN
[END OF IF]
IF I=REAR THEN:
WRITE : “UPDATE SUCCESSFULLY NOT COMPLETED”
[END OF IF]
[END OF LOOP]
STEP-4 : RETURN
Linear Queue: Drawback

• We cannot insert more elements, once the Queue is full, even though we dequeue a few of the
elements.

• Implementation of a linear queue brings the drawback of memory wastage.


Circular Queue
• A circular queue is the extended version of a regular queue where the last element is connected
to the first element.
Why Circular Queue? What its Concept?
Why Circular Queue?
What its Concept?
Applications of a Circular Queue
• Buffer in Computer Systems: Computer systems supply a holding area for maintaining
communication between two processes, two programs, or even two systems. This
memory area is also known as a ring buffer.

• CPU Scheduling: In the Round-Robin Scheduling Algorithm, a circular queue is utilized to


maintain processes that are in a ready state.

• Traffic System: Circular queue is also utilized in traffic systems that are controlled by
computers. Each traffic light turns ON in a circular incrementation pattern in a constant
interval of time.
Circular Queue in a Data Structure?

The circular queue resolves the memory wastage problem with the help of a circular link.
Operations of Circular Queue

• Front - Used to get the starting element of the Circular Queue.


• Rear - Used to get the end element of the Circular Queue.
• enQueue(value) - Used to insert a new value in the Circular Queue. This
operation takes place from the end of the Queue.
• deQueue() - Used to delete a value from the Circular Queue. This operation takes
place from the front of the Queue.
How Circular Queue Works?

Let's take the same example that we used for the simple
queue.

Lets see how circular queues overcome the situation when we


have some unused spaces left after deletion.

We will insert elements until the circular queue is full.

Next, we will try deleting some of them and see if we can insert
new elements in the vacated spaces.
How Circular Queue Works? .. Contd.,

(i) Insert 45, 11, 23, 81, 57

(ii) Insert 32
How Circular Queue Works? .. Contd.,

(iii) Delete: 45, 11, 23, 81

(iv) Insert: 29 (v) Delete: 57, 29


How Circular Queue Works? .. Contd.,

(vi) Insert: 17, 19


Algorithm For Insert Operation in Circular Queue
INSERT(C_QUEUE[MAXSIZE], FRONT, REAR, NUM)
Step 1 : If FRONT = (REAR + 1) % MAXSIZE : then // check overflow condition
Write : “Queue Overflow”
Return.
[End of If]
Step 2 : Read NUM to be inserted in Circular Queue.
Step 3 : If FRONT= -1 : then // Check if Q is Empty and Set Rear
Set FRONT = REAR =0.
Else
Set REAR=(REAR + 1) % MAXSIZE.
[End of If Else]
Step 4 : Set CQUEUE[REAR]=NUM; // Insert the new element
Step 5 : Exit
Algorithm For Delete Operation in Circular Queue
• DELETE(CQUEUE[MAXSIZE], FRONT, REAR, NUM)
Step 1 : If FRONT = - 1 : then // Check Underflow condition
Write : “Queue Underflow”
Return.
[End of If]
Step 2 : Set NUM = CQUEUE[FRONT]. // Retrieve the Front element
Step 3 : Write ‘Deleted element from circular queue is : ",NUM.
Step 4 : If FRONT = REAR : then // Check if Q has only 1 element left
Set FRONT = REAR = -1;
Else
Set FRONT = (FRONT + 1) % MAXSIZE.
Step 5 : Exit // Return the Deleted Element

You might also like