0% found this document useful (0 votes)
7 views10 pages

Queue

The document discusses different queue data structures including basic queue operations and implementations using arrays. It describes common queue applications and also covers priority queues, circular queues, dequeues including input and output restricted variations.

Uploaded by

armaansohail9356
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)
7 views10 pages

Queue

The document discusses different queue data structures including basic queue operations and implementations using arrays. It describes common queue applications and also covers priority queues, circular queues, dequeues including input and output restricted variations.

Uploaded by

armaansohail9356
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/ 10

MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),

MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

QUEUE:
Queue is also a linear data structure, just like stack data structure, in which the first element is inserted from
one end called the REAR(also called tail), and the removal of existing element takes place from the other end
called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that element inserted first will be
removed first.
Which is exactly how queue system works in real world. If you go to a ticket counter to buy movie tickets, and
are first in the queue, then you will be the first one to get the tickets. Right? Same is the case with Queue data
structure. Data inserted first, will leave the queue first.
The process to add an element into queue is called Enqueue and the process of removal of an element from
queue is called Dequeue.

Basic features of Queue

1. Like stack, queue is also an ordered list of elements of similar data types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements inserted before the new element in
the queue must be removed, to remove the new element.

Page 1
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Applications of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the
first one coming in, also gets out first while the others wait for their turn, like in the following scenarios:

1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as they
arrive i.e First come first served.

Implementation of Queue Data Structure


Queue can be implemented using an Array, Stack or Linked List. The easiest way of implementing a queue is by
using an Array.
Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of the array (starting the
index of array from 0). As we add elements to the queue, the tail keeps on moving ahead, always pointing to the
position where the next element will be inserted, while the head remains at the first index.

Page 2
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Whenever we move head one position ahead, after removal of first element, the size on Queue is reduced by
one space each time.
QINSERT( QUEUE, SIZE, FRONT, REAR, ITEM)
1. IF REAR =SIZE, then:
WRITE: OVERFLOW and Return
2. IF FRONT =NULL, then:
Set FRONT:=1 and REAR:=1

ELSE:
Set REAR:=REAR+1
[End of IF Structure]
3. Set QUEUE[REAR] := ITEM
4. Return

Page 3
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

QDELETE( QUEUE, N, FRONT, REAR, ITEM)


1. IF FRONT= NULL, then:
WRITE: UNDERFLOW and Return
2. Set ITEM:=QUEUE[FRONT]
3. IF FRONT =REAR, THEN:
Set FRONT:=NULL and REAR:=NULL

ELSE:
Set FRONT:=FRONT+1
[End of IF Structure]
4. Return

1 2 3 4 5
30 40

Front=4
Rear=5

Page 4
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Circular Queue

CQUEUE_INSERT( QUEUE, SIZE, FRONT, REAR, ITEM)

1. IF FRONT=1 AND REAR =SIZE OR FRONT=REAR+1, then:


WRITE: OVERFLOW and Return

2. IF FRONT =NULL, then:


Set FRONT:=1 and REAR:=1
ELSE IF
Set REAR=N, then
REAR:=1

ELSE:
Set REAR:=REAR+1
[End of IF Structure]
3. Set QUEUE[REAR] := ITEM
Return

CQUEUE_DELETE( QUEUE, N, FRONT, REAR, ITEM)


1. IF FRONT= NULL, then:
WRITE: UNDERFLOW and Return
2. Set ITEM:=QUEUE[FRONT]
3. IF FRONT =REAR, THEN:
Set FRONT:=NULL and REAR:=NULL
ELSE IF
FRONT=N, then
Set FRONT:=1
ELSE:
Set FRONT:=FRONT+1
[End of IF Structure]
4. Return

Page 5
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

1 2 3 4 5
70 80 90 100
Front=2
Rear=5

DEQUE

Types of Deque:

1. Input Restricted
2. Output Restricted

Page 6
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Input Restricted:-

1 2 3 4 5
10 20 30
Rear
40

Front=2
Rear=4

Dequeue_Insert (Q,REAR,N,FRONT,ITEM)

1. IF REAR=N AND FRONT=1


WRITE (“OVERFLOW”)
2. IF FRONT>1, then // Insert from Front End
FRONT:=FRONT-1
ELSE RETURN
3. Q[FRONT]:=ITEM
4. IF REAR<N, then // Insert From Rear End
REAR:=REAR+1
ELSE RETURN
5.Q[REAR]:=ITEM

Page 7
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Output Restricted Dequeue

1 2 3 4 5
30

Dequeue_Delete (Q,REAR,N,FRONT,ITEM)

1. If (FRONT=0 AND REAR=0)


WRITE(“UNDERFLOW”)
2. IF(FRONT>0) // DELETE FROM FRONT END
ITEM:=QUEUE[FRONT]
RETURN(ITEM)
3. IF(FRONT=REAR)
FRONT:=0 AND REAR:=0
ELSE
FRONT:=FRONT+1
4. IF(REAR>0) //DELETE FROM REAR END
ITEM:=QUEUE[REAR]
RETURN(ITEM)

5. IF(FRONT=REAR)
Page 8
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

FRONT:=0 AND REAR:=0


ELSE
REAR:=REAR-1
6. RETURN

Priority Queue
1 2 3 4 5
10

20 30 40 5

15

70 50 60

80

Front Rear
2 2
1 3
0 0
4 1
3 3

Page 9
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure

Page 10

You might also like