You are on page 1of 21

QUEUES

Presented by:
Jovar V.Tabat
Ma.Liezell N. Acosta
What is Queue?

•It is an ordered list in which all


insertion take place at the other end,
known as the bottom. This type of
processing behavior is also called
FIFO or First-In-First-Out.
Representation of Queues
Queues may be
represented as:
1.One-
Dimensional
Arrays
One - Dimensional Array
Representation
Arr [n] =>

Arr [n-1] =>

Arr [4] =>



Phillip <=Top
Arr [3] =>
Retchan
Arr [2] =>

Arr [1] =>


Gretchen <= Bottom
One – Dimensional Array
If we are going to delete “Gretchen” the output will be look like
this:
Arr [n] =>

Arr [n-1] =>

Arr [4] => …


Arr [3] =>
Phillip
Arr [2] =>
<=Top
Retchan
Arr [1] => <= Bottom
Queues are represented as
One-Dimensional Array when:

1.The maximum size


of the queue is
known.
2.The amount of time
wasted in shifting
Single-Linked List
Representation
HEAD TAIL

25 H 5H 6H
Jason 5H Melai 6H Carol Null

Queues are represented as singly-


linked list when:
1. The maximum size of the queue
QUEUE OPERATION
There are Three Basic
Operation that may be
performed on queues.
1.Inserting an element at the
top of a queue.
2.Deleting the bottom element
from a queue.
Inserting a Node at the Top of a
Queue
• Head : The address of the first node
in the queue
• Tail : The address of the last node
in the queue
• Value : The value to be inserted into
the queue
Code: Insert _ Q (Head, Tail , Value)
Deleting the Bottom Node From
a Queue
• Head : The address of the first
node in the queue
•Tail : The address of the last
node in the queue
Code: Delete _ Q (Head, Tail)
Retrieving the Bottom Element
from a Queue

Code : Retrieve _ Q ( Head)


Circular Queues
• Here, the elements of the array are “ arranged” in a
circular fashion with Arr[1] following Arr[n].

Arr [3]
Arr [2]
Top = 0
Bottom = 0
Arr [4]
Arr [1]

Arr [5]
Arr [6]
One-Dimensional Array as
Circular Queues
♥When using this, the bottom of the
queue is not always located in
Arr[1].Instead, the bottom, and top
of the queue is monitored by two
variables which we will also call TOP
and BOTTOM.
♥TOP and BOTTOM contain the value
(0)
♥ If we insert “A” into the queue, TOP
will be 1 and BOTTOM will be 1 too.
Representation of
Circular Queue
Arr[2] Arr[3]

Top = 1
Arr[1]
Bottom = 1 Arr[4]
A

Arr[6] Arr[5]

Circular Queue after inserting “A”


Representation of
Circular Queue
Arr[2] Arr[3]
Top = 2
B

Arr[1]
Bottom = 1 Arr[4]
A

Arr[6] Arr[5]

Circular Queue after Inserting ”B”


Representation of
Circular Queue
Arr[2] Arr[3]
Top, Bottom = 2
B

Arr[1]
Arr[4]

Arr[6] Arr[5]

Circular Queue After Deleting the Bottom


Element
Inserting an Element into
Circular Queue
• Top : The location of the element at
the top of the queue.
• Bottom : The location of the element
at the bottom of the queue.
• Arr : The array representing the
queue.
• Max : The maximum number of
elements in the queue.
• Value : The value of the element to
be inserted into the queue.
Code for Inserting Element
• Insert _ Circular_ Q ( Top, Bottom, Arr, Max, Value )
Top=Bottom = 0

Arr[1] Arr[2] Arr[3] Arr[4] Arr5] Arr[6] Arr[7] Arr[8] Arr[9] Arr[10]

Circular Queue With Maximum 10 Elements


Code for Inserting Element
• Insert _ Circular_ Q ( Top, Bottom, Arr, Max, Value )

A
Arr[1] Arr[2] Arr[3] Arr[4] Arr5] Arr[6] Arr[7] Arr[8] Arr[9] Arr[10]

Circular Queue With Maximum 10 Elements


Insert A:
Insert _ Circular_ Q ( 0, 0, Arr, 10, “A” )
Top=1
Bottom=1
Arr[1]=“A”
Code for Deleting Element
• Delete _ Circular_ Q ( Top, Bottom, Arr, Max)

A
Arr[1] Arr[2] Arr[3] Arr[4] Arr5] Arr[6] Arr[7] Arr[8] Arr[9] Arr[10]

Circular Queue With Containing 1 Element


Delete A:
Delete _ Circular_ Q ( 1,1, Arr, 10)
Top=0
Bottom=0
♥THANK YOU FOR
LISTENING♥