You are on page 1of 39

Data Structures and

Algorithm
Queues
Queue

Queue Operations
Topics
Representation of Queue

Types of Queue
Queues

A queue is like a line of people


waiting for a bank teller. The
queue has a front and a rear.

$ $

Front
Rear
Queues
Queue – an ordered list in which all
insertions take place at one end
while all deletions take place at
the other end.
Top/rear – end of the list where all
insertion takes place
Bottom/front – other end of the list
where all deletions take place
A queue is processed in a First In
First Out fashion, FIFO.
 Operating systems:
 queue of print jobs to send to the printer
 queue of programs / processes to be run
(CPU scheduling)
 queue of network data packets to send

Queues in  Programming:
Computer  modeling a line of customers or clients

Science  storing a queue of computations to be


performed in order

 Real world examples:


 people on an escalator or waiting in a line
 cars at a gas station (or on an assembly line)
5
A Conceptual View of a Queue
Operations on Queue

 New people must enter the queue at the rear.


This process is called an add, put , enqueue
operations.

$ $

Front
Rear
Operations on Queue

 Whenan item is taken from the queue, it always


comes from the front. This is called a delete,
remove, get or dequeue operation.

$ $

Front
Rear
Arrays Stacks Queues
Insert Push Put, add, enqueue
Delete Pop Get, delete, remove,
dequeue
Search Peek Peek, retrieve

Operations on Queue
Operations on
Queue

 Enqueue – insertion is
always at the top of the
list
 Enqueue – insertion an element at the top of the list
 Dequeue – to delete an element at the bottom of the
list
 Peek or Retrieve - print or display the element at the
bottom of the list
• being viewed or retrieved is what you are about to
Operations on
delete or remove and not what you have inserted Queue
 Empty and Full
• return you an error because the queue is empty
• a message that the queue is full when trying to
insert another element
Operations on Queue

Add : adds a new node • Add(X,Q) → add the value X to the tail of queue

Remove : removes a node


• Remove(Q) → removes the head node and returns
its value

IsEmpty: reports whether the • IsEmpty(Q) → report whether the queue Q is


queue is empty empty

IsFull : reports whether the


queue is full • IsFull(Q) → report whether the queue Q is full

Initialize : • Initialize(Q) → create a new empty queue named


creates/initializes the queue Q
Destroy : deletes the contents of
the queue (may be implemented • Destroy(Q) → deletes the contents of the queue Q
by re-initializing the queue)
Operations on Queue – Example
Operation Queue’s contents Return value

1. Initialiaze(Q) <empty> -
2. Enqueue (‘A’,Q) A -
3. Enqueue(‘B’,Q) A B -
4. Enqueue(‘C’,Q) A B C -
5. Dequeue (Q) B C A
6. Enqueue(‘D’,Q) B C D -
7. Dequeue(Q) C D B
8. Dequeue(Q) D C
9. Dequeue(Q) <empty> D
Exercise: Queue Operation

What would be the contents of a queue after the


following operations?
Initialize(Q)
Enqueue (‘A’,Q)
Enqueue(‘F’,Q)
Enqueue(‘X’,Q)
Dequeue(Q)
Enqueue(‘B’,Q)
Dequeue(Q)
Dequeue(Q)
→B
Queue operations work as follows:
• two pointers FRONT and REAR
• FRONT track the first element of the queue
• REAR track the last elements of the queue
• initially, set value of FRONT and REAR to -1
Working of
 Enqueue Operation
Queue
• check if the queue is full
• for the first element, set value of FRONT to 0
• increase the REAR index by 1
• add the new element in the position pointed to
by REAR
 Dequeue Operation
• check if the queue is empty
• return the value pointed by FRONT
Working of
• increase the FRONT index by 1
Queue
• for the last element, reset the values
of FRONT and REAR to -1
Enqueue and Dequeue Operations
Queues can be represented as :
1. One-dimensional array
Representation 2. Linked List
of Queues
Representation of
Queues

 Queue as one-
dimensional array
Representation of Queues
Queue as one-dimensional array

Queues are represented as one-


dimensional arrays when:

• the maximum size of the queue is known.


• the amount of time wasted in shifting the elements after
deletion is negligible.
• Queue as an array - to be efficient, shifting elements
must be avoided.

A better solution is to treat the array as


circular.
Linked List Representation

Queue as Singly-linked list when the maximum


size of the queue is unknown

A queue can also be implemented with a linked


list with both a head and a tail pointer. 13
15

10

7
null
head_ptr
tail_ptr
13
Linked List
Representation
15

10
 Which end do you think
is the front of the
7
queue? Why?
null

head_ptr

tail_ptr
Linked List
Front Representation
13

15
 The head_ptr points to the
front of the list.
10

 Because it is harder to remove


items from the tail of the list.
7

null

head_ptr
Rear
tail_ptr
Types of Queue

 Simple Queue
 In a simple queue,
insertion takes place at
the rear and removal
occurs at the front. It
strictly follows FIFO
rule.
Types of Queue
 Circular Queue
 In a circular queue, the last
element points to the first
element making a circular link.
 The main advantage of a circular
queue over a simple queue is
better memory utilization. If the
last position is full and the first
position is empty then, an
element can be inserted in the
first position. This action is not
possible in a simple queue.
Types of Queue

 Priority Queue
 A priority queue is a special
type of queue in which each
element is associated with a
priority and is served
according to its priority. If
elements with the same
priority occur, they are served
according to their order in the
queue.
Circular Queue

 A circular array is a regular


array that is treated as if it
loops back around on itself.
That is, the last index is
considered to precede index
0.
 Sometimes called as ring
buffer
 elements of the array are
“arranged” in a circular
manner
Array Implementation – Circular Queue

 A queue can be implemented with an array. For example, this


queue contains the integers 4 (at the front), 8 and 6 (at the
rear).

[0] [1] [2] [3] [4] [5] ...

4 8 6

An array of integers to
implement a queue of No data yet in
integers this part of the array.
Array Implementation – Circular Queue

 The easiest implementation also keeps track of


the number of items in the queue and the index 3 size
of the first element (at the front of the queue),
the last element (at the rear). 0 first

2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array Implementation – Circular Queue

 When an element leaves the queue, size


2 size
is decremented, and first changes, too.
1 first

2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array Implementation – Circular Queue

 When an element enters the queue,


size is incremented, and last changes, 3 size

too.
1 first

3 last

[0] [1] [2] [3] [4] [5] ...

8 6 2
Array Implementation – Circular Queue

 There is special behavior at the end of


the array. For example, suppose we 3 size
want to add a new element to this
queue, where the last index is [5]: 3 first

5 last

[0] [1] [2] [3] [4] [5]

2 6 1
Array Implementation – Circular Queue

 Thenew element goes at the front of 4 size


the array (if that spot isn’t already
used): 3 first

0 last

[0] [1] [2] [3] [4] [5]

4 2 6 1
Array Implementation – Circular Queue
Aspects of Implementing queue as an Array
 Easy to implement, but it has a limited capacity
with a fixed array or you must use a dynamic
3 size
array for an unbounded capacity
 Special behaviour is needed when the rear
reaches the end of the array, called circular 0 first
array. The idea of a circular array is that the end of
the array “wraps around” to the start of the array 2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
Algorithm for inserting element in a circular queue:

1. If bottom of the queue is pointing to the last


position then proceed to second or else third step
2. make the bottom value as 0

3. increment the bottom value by one

4. a. if the top points where bottom is pointing and the queue holds a not
NULL value for it, then its a "queue overflow" state, so quit; else go to step-b
• b. insert the new value for the queue position pointed by the bottom

Circular Queue
Circular
Queue
Circular Queue
Algorithm to delete the element at the top of a
circular queue
1. If the queue is empty then say "empty queue"
and quit; else continue
2. Delete the top element
3. If the top is pointing to the last position of the
queue then step-4 else step-5
4. Make the top point to the first position in the
queue and quit
5. Increment the top position by one
Java Code for Queue
public void insert (int j)
{ if (rear == maxSize -1)
rear = -1;
queArray [++rear] = j; nItems++;
}
public int remove()
{ int temp = queArray [front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public int peekFront()
{ return queArray[front]; }
Queue: Circular Array vs. Linked List

 Circular Array  Linked List


 May waste unneeded  Always just enough space
space or run out of space
 But more space per
 Space per element element
excellent
 Operations very simple /
 Operations very simple / fast
fast

39

You might also like