You are on page 1of 22

1

Queue
Subtopics:
Simple Queue
Circular Queue

Project Made by:

Mehul Bhanderi
Kishan Jasani
Vinesh Kumbhani
Nilay Bharti

2
1. Introduction ____________________________________________________3
2. Operations _____________________________________________________6
3.Illustration of Simple Queue _______________________________________7
4.Algorithm for inserting data in Simple Queue __________________________8
5.Algorithm for deleting data from Simple Queue ________________________9
6.Drawback of Simple Queue________________________________________10
7.Circular Queue__________________________________________________12
8. Illustration of Circular Queue______________________________________13
9.Algorithm for inserting data in Circular Queue_________________________19
10. Algorithm for deleting data from Cicular Queue ______________________20
11. Summary and Conclusion________________________________________21
12. Sources______________________________________________________22

INDEX
3
1. Introduction
Queue
A queue is a particular kind of collection in which the entities
in the collection are kept in order and the principal (or only)
operations on the collection are the addition of entities to
the rear terminal position and removal of entities from the
front terminal position. This makes the queue a First-In-
First-Out (FIFO) data structure. In a FIFO data structure,
the first element added to the queue will be the first one to
be removed. This is equivalent to the requirement that
once an element is added, all elements that were added
before have to be removed before the new element can be
invoked. A queue is an example of a linear data structure.


4
Queues provide services in computer science,
transport, and operations research where various
entities such as data, objects, persons, or events are
stored and held to be processed later. In these
contexts, the queue performs the function of a buffer.
Queues are common in computer programs, where
they are implemented as data structures coupled
with access routines, as an abstract data structure or
in object-oriented languages as classes. Common
implementations are circular buffers and linked lists.
5
The defining attribute of a queue data structure is
the fact that allows access to only the front and
back of the structure. Furthermore, elements can
only be removed from the front and can only be
added to the back. In this way, an appropriate
metaphor often used to represent queues is the
idea of a checkout line. Other examples of queues
are people traveling up an escalator, machine
parts on an assembly line, or cars in line at a
petrol station. The recurring theme is clear:
queues are essentially the same as a queue you
would get in a shop waiting to pay.
6
2. Operations
In queue, data elements are added at one end, called
the rear and removed from another end, called the front
of the list.
Two basic operations are associated with queue:
1. Insert operation is used to insert an element into a
queue.
2. Delete operation is used to delete an element from a
queue.
7
3. Consider the following queue (simple queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
(1) Insert 20. Now Rear = 5 and Front = 1
1 2 3 4 5 6 7
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
8
4. Algorithm for inserting data in
Simple Queue:
Insert ( ):
Here QUEUE is an array with N locations. FRONT and
REAR points 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
9
5. Algorithm for deleting data:
Delete ( ):
Here QUEUE is an array with N locations. FRONT and REAR
points 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
10
6. Drawback of Simple Queue
Problem: RightwardDrifting:
After a sequence of additions and removals, items will
drift towards the end of the array
Even though, there are empty spaces in front of the
queue array, insert operation cannot be performed
on the queue, since back= size - 1.
11
Rightward Drifting Solutions
To optimize space and to solve rightward drifting:
Shift array elements after each deletion.
However, shifting is not effective and dominates
the cost of the implementation.
12
7. Circular Queue:
Using a circular array makes it easier: When front
or back reach the
end of the array, wrap them around to the
beginning of the array.

Let's see inserting and
Deleting of data.

13
8. Example: Consider the following circular queue
with N=5
.



Initially, Rear = 0, Front =
0.
2. Insert 10, Rear = 1, Front = 1.
Rear
Front
14
3. Insert 50, Rear = 2, Front = 1.
Rear
Front
4. Insert 20, Rear = 3, Front = 0.
Rear
Front
15
5. Insert 70, Rear = 4, Front = 1.
6. Delete front, Rear = 4, Front = 2.
Rear
Rear
Front
Front
16
7. Insert 100, Rear = 5, Front = 2.
8. Insert 40, Rear = 1, Front = 2.
Front
Rear
Front
Rear
17
9. Insert 140, Rear = 1, Front = 2. As Front = Rear + 1, so Queue overflow.
Rear
Front
10. Delete front, Rear = 1, Front = 3.
Rear
Front
18
11. Delete front, Rear = 1, Front = 4.
12. Delete front, Rear = 1, Front = 5.
Rear
Rear
Front
Front
19
9. Algorithm to insert data in Circular Queue:
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 if 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
20
10. Algorithm to delete data from Circular
Queue:
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 element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if
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
21
11. Summary and Conclusion
Queue can be implemented using linear array and
circular
array.
Structure of queue is linear, items that manage the
array are front and back.
Insertion happens at back, while deletion happens at
front.
Drawback of simple queue is that it will lead to
rightward drifting problem after sequence of deletion
and insertion is performed on the queue.
Circular Queue can be used in order to solve the
problem, whereby after front or back reach the end of
the array, it will wrap around to the beginning of the
array.
22
12 . Sources:

http://www.wiziq.com/tutorial/191784-Linear-Queue-and
-Circular-Queue
https://www.princeton.edu/~achaney/tmve/wiki100k/docs/
Queue_(data_structure).html
http://www.w3professors.com/Pages/Courses/Data-Structure
/Algorithms/Data-Structure-Algorithm.html
http://ocw.utm.my/file.php/31/Module/ocwQueueDec2011.pdf
http://mithun039.weebly.com/uploads/3/5/3/6/3536408/queue.ppt

You might also like