What is aqueue?
It is an ordered group of homogeneous items of
elements.
Queues have two ends:
Elements are added at one end.
Elements are removed from the other end.
The element added first is also removed first
(FIFO: First In, First Out).
3.
Note:
In queue deletioncan take place only at
one end called FRONT end and
insertion can take place only at the
other end called REAR end.
The order in which the element enter a
queue is the order in which they will
leave
3
4.
Purpose of queue
4
The purpose of queue is to provide some form of
buffering. In a computer system, queues are used
Process Management: For example, in a timesharing
system in computer, programs are added to queue and
are executed one after the other.
Buffer between the fast computer and a slow printer:
Documents sent to the printer for printing is added to a
queue. The document sent first is printed first and
document sent last is printed last.
5.
Example of queue
An important example of queue in computer
science occurs in a timesharing system in which
programs with different priorities form a queue
while waiting to be executed (priority queue).
Queues used in everyday life. The automobiles are
waiting to pass through an intersection from a queue in
which the first car is line is the first can through,
The people is waiting in line at a bank from a queue,
where the first person in line is the first person to be
waited on and so on.
5
Operation on Queue
7
1.Insertion or enqueue ( ): The insertion operation [insert
(QUEUE, ITEM)] inserts an ITEM at the REAR of a QUEUE.
2. Deletion or dequeue ( ): The deletion operation [remove
(QUEUE, ITEM)] removes the ITEM from the FRONT of a
QUEUE.
3. Empty: The third operation [empty (QUEUE)] return FALSE
when the QUEUE is not empty otherwise it returns TRUE if the
QUEUE is empty.
4. Full: There is another operation performed on a QUEUE. The
full operation [full (QUEUE)]) return TRUE value if the
QUEUE is full otherwise it returns FALSE.
8.
Representation of Queues
Queue can be represented in the computer memory in
various ways usually in one way list or linear array.
Queue which we will mention the algorithm will be a
linear array 'Queue' and two pointer variables.
FRONT will contain the location number of the front
element of the queue and REAR will contain the
location number of the rear element of the queue.
The condition FRONT = NULL will indicate that the
queue is empty.
8
1) Algorithm toinsert in Q
ALGORITHM: INSERTION (FRONT,REAR,ITEM, MAX , QUEUE)
Step #01: [Check for overflow]
If (Rear = = max-1) then
Write(“Overflow or Queue is full”)
Return null or exit;
Step # 02: [Set REAR Pointer]
If ( REAR = = -1 and FRONT = = -1) then
REAR= FRONT = 0
Else
REAR= REAR+1
[ End of if structure]
Step # 03: [Insert the item]
QUEUE [REAR]=ITEM
Step # 04: [Finish]
Exit
13
14.
2) Algorithm todelete from Q
ALGORITHM: Deletion (FRONT,REAR,ITEM, QUEUE)
Step #01: [Check for under flow]
If ( FRONT = = -1 and REAR = = -1) then
Write(“Under flow or Queue is empty”)
Return null or exit;
Step # 02: [Delete the item]
ITEM = QUEUE [FRONT]
Step # 03: [Set FRONT Pointer]
If ( FRONT = = REAR) then
FRONT = REAR = -1
Else
FRONT= FRONT+1
[ End of if structure]
Step # 04: [Finish]
Exit
14
15.
Deque
Deque,pronounced “deck”, which stands for double-
ended queue.
A deque is a linear list in which elements can be added or
removed at either end but not at the middle.
A deque, also known as a double-ended queue, is an
ordered collection of items similar to the queue.
It has two ends, a front and a rear, and the items remain
positioned in the collection. What makes a deque different
is the unrestrictive nature of adding and removing items.
New items can be added at either the front or the rear.
Likewise, existing items can be removed from either end.
15
16.
Deque
In asense, this hybrid linear structure provides all the
capabilities of stacks and queues in a single data
structure.
It is important to note that even though the deque can
assume many of the characteristics of stacks and
queues, it does not require the LIFO and FIFO
orderings that are enforced by those data structures.
It is up to you to make consistent use of the addition
and removal operations.
16
17.
Types of Deque
a)Input restricted Deque: It is the type of Deque which
allows insertion from one end and deletion from both
ends.
b) Output restricted Deque: It is the type of Deque
which allows deletion from one end and insertion
from both ends.
17