0% found this document useful (0 votes)
38 views

Queue

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)
38 views

Queue

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/ 19

QUEUE

QUEUE
• A queue is a linear list of elements in which
deletions can take place only at one end,
called the Front and insertions can take place
at the other end called the Rear.
• Its insertion and removal routines follows the
first-in-first-out (FIFO) principle.
• Queues may be represented by linear arrays
or one way lists
Queue Applications
• Real life examples
– Waiting in line
• Applications related to Computer Science
– Job scheduling (e.g. Round-Robin algorithm for
CPU allocation)
QUEUE ADT
• ADT queue is
– Object: a finite ordered list with zero or more
elements
– Functions:
• createQ(maxqueuesize)
– Creates an empty queue with size=maxqueuesize
• Isfull(queue,maxqueuesize)-
– If (number of elements in queue=maxqueuesize) return true
– Else return false
• Isempty(queue)
– if queue==createQ(maxqueueusize) return true
– Else return false
• AddQ(queue,item)
– If Isfullqueue(queue) return msg(“queue is full;
cannot add”)
– Else insert item to the rear of the queue and return

• DeleteQ(queue)
– If Isemptyqueue(queue) return msg (“Queue is
empty”)
– Else remove and return item from front of queue
Implementing a Queue with an array
• The queue will be maintained by a linear array
called QUEUE and the variable FRONT
contains the location of the front element of
the queue and the variable REAR contains the
location of the rear element of the queue.
• The condition that FRONT=0 or NULL will
indicate that the queue is empty.
• Whenever an element is deleted from the
queue front is incremented and whenever an
element is inserted rear is incremented.
Array representation of a QUEUE
AAA BBB CCC DDD

Front=1 , Rear=4

AAA BBB CCC DDD EEE

Inserting a new element, Rear=Rear+1=5


Rear=N indicates QUEUE is FULL(OVERFLOW)

BBB CCC DDD EEE

Deleting an element, Front=Front+1=2


Front=0 or NULL indicates QUEUE is empty (UNDERFLOW)
Queue Insert
Insert ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR
point to the front and rear of the QUEUE. ITEM is the value to be
inserted.
1. If (REAR == N) Then [Check for overflow]
2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted
[End of Step 1 If]
9. Exit
Queue Delete
Delete ( ):
Description: Here QUEUE is an array with N locations. FRONT and
REAR point to the front and rear of the QUEUE.
1. If (FRONT == 0) Then [Check for underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [Check if only one element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
8. Print: ITEM deleted
[End of Step 1 If]
9. Exit
Circular QUEUE
• The array representation of the QUEUE will not permit
to insert if rear=n, even if the Q is empty. To avoid the
array QUEUE can be considered as circular.
• Here if Front=n and an element is deleted, Front is
reset to 1.
• Also if Rear=n, and an element is inserted, Rear is
reset to 1
• Suppose Q contains only one element with
Front=Rear, then deleting an element will reset
Front=NULL or 0 and Rear=NULL or 0
• (Front=1 and Rear=n) or (Front=Rear+1) indicates
overflow
• Front=NULL or 0 indicates underflow
Circular QUEUE
AAA BBB CCC DDD EEE FFF GGG

Front=1, Rear=7
AAA BBB CCC DDD EEE FFF GGG HHH
Inserting a new element, Front=1, Rear=8
No more insertions possible (Overflow)

BBB CCC DDD EEE FFF GGG HHH


Deleting an element, Front=2, Rear=8

III BBB CCC DDD EEE FFF GGG HHH

Inserting a new element, Front=2, Rear=1


No more insertions possible (Overflow)

III CCC DDD EEE FFF GGG HHH


Deleting an element, Front=3, Rear=1
Circular queue delete
Delete Circular ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear elements of the QUEUE.

1. If (FRONT == 0) Then [Check for Underflow]


2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only one element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end of QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]
11. Exit
Circular queue insert
Insert Circular ( ):
Description: Here QUEUE is an array with N locations. FRONT and REAR points to the
front and rear elements of the QUEUE. ITEM is the value to be inserted.

1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then


2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end of QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]
11. Exit
Implementing a Queue with a Singly Linked List

• Nodes connected in a chain by links


• The head of the list is the front of the queue,
the tail of the list is the rear of the queue.
• Elements may be inserted at any time, but
only the element which has been in the queue
the longest may be removed.
• Elements are inserted at the rear (enqueued)
and removed from the front (dequeued)
Linked Representation of a QUEUE

Front
Rear
ww
xxx yyy .
w

Delete a node Front


Rear
xxx yyy .

Insert a node
Front Rear

xxx yyy zzz .


• Defining of a QUEUE node
struct node {
int data;
struct node* next;
};
• Initializing the QUEUE
struct node* Front=NULL, Rear=NULL
Algorithm InsertQ_Linked
• InsertQ_Linked ( ):
//Description: Here Front is a pointer variable which contains the
address of first node and Rear contains the address of the last
node.. ITEM is the value to be inserted into the QUEUE.//
1.If (Front == NULL & Rear==NULL) Then
2.Front = New Node [Create a new node]
3. Front->INFO = ITEM [Assign ITEM to INFO field]
4. Front->LINK = NULL [Assign NULL to LINK field]
5. Rear=Front
6. Else
7. Set Ptr = Rear, Rear= New Node
8. Rear->INFO = ITEM [Assign ITEM to INFO field]
9. Rear->LINK = Null
10. Ptr->LINK=Rear
[End of step 1 if]
11. EXIT
Algorithm DeleteQ_Linked
• DeleteQ_Linked ( ):
// Description: Here Front is a pointer variable which contains the address of first node
and Rear contains the address of the last node. ITEM is the value deleted from the
Queue.//
1,If (Front == NULL) Then [Check whether queue is empty]
2. Print: Queue is empty.
3. Else
4. ITEM=Front->INFO
5. If Front==Rear // Only one element left
6. Set Front=NULL; Rear=NULL
7. Else
8. PTR = Front
9. Front =Front->LINK [Front now points to next node]
10. Delete PTR
[End of step 5 if|
11. Print : ITEM deleted
12. EXIT
.
Applications of QUEUE
• Queue is useful in CPU scheduling, Disk Scheduling. When multiple
processes require CPU at the same time, various CPU scheduling
algorithms are used which are implemented using Queue data structure.
• When data is transferred asynchronously between two processes, Queue
is used for synchronization. Examples : IO Buffers, pipes, file IO, etc.
• In print spooling, documents are loaded into a buffer and then the printer
pulls them off the buffer at its own rate. Spooling also lets you place a
number of print jobs on a queue instead of waiting for each one to finish
before specifying the next one.
• Breadth First search in a Graph .It is an algorithm for traversing or
searching graph data structures. It starts at some arbitrary node of a graph
and explores the neighbor nodes first, before moving to the next level
neighbors. This Algorithm uses Queue data structure.
• Handling of interrupts in real-time systems. The interrupts are handled in
the same order as they arrive, First come first served.
• In real life, Call Center phone systems will use Queues, to hold people
calling them in an order, until a service representative is free.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy