You are on page 1of 22

Queues

QUEUE
A Queue is a special kind of list, where items are
inserted at one end (the rear) And deleted at the
other end (the front)

Other Name:
First In First Out (FIFO)
Difference from Stack:
Insertion go at the end of the list, rather
than the beginning of the list.
03/20/15

Queue
Stores a set of elements in a particular
order
Stack principle: FIRST IN FIRST
OUT = FIFO
It means: the first element inserted is
the first one to be removed
Example
The first one in line is the first one to be
served

03/20/15

st In First Out

rear
front

B
A

C
rear B
front A

D
rear C
B
front A

rear

D
C
front B

rear
front

Difference from Stack:


Insertion go at the end of the list, rather than
the beginning of the list.

03/20/15

Queue Applications
Real life examples
Waiting in line
Waiting on hold for tech support

Applications related to Computer Science


Threads
Job scheduling (e.g. Round-Robin algorithm for CPU
allocation)

03/20/15

Applications of Queues
Operating system
multi-user/multitasking environments, where several
users or task may be requesting the same resource
simultaneously.

Communication Software
queues to hold information received over networks
and dial up connections. (Information can be
transmitted faster than it can be processed, so is
placed in a queue waiting to be processed)

Some other?

03/20/15

Static
Queue is implemented by an array, and size of queue
remains fix

Dynamic
A queue can be implemented as a linked list, and
expand or shrink with each enqueue or dequeue
operation.

03/20/15

7
7

1.
2.
3.
4.
5.

MAKENULL(Q): Makes Queue Q be an empty list.


FRONT(Q): Returns the first element on Queue Q.
ENQUEUE(x,Q): Inserts element x at the end of Queue Q.
DEQUEUE(Q): Deletes the first element of Q.
EMPTY(Q): Returns true if and only if Q is an empty
queue.

Example:
Line of customers in a bank
03/20/15

8
8

03/20/15

Implementation
Static
Queue is implemented by an array, and size of queue
remains fix

Dynamic
A queue can be implemented as a linked list, and it
can expand or shrink with each enqueue or dequeue
operation.

03/20/15

10

Array-based Queue Implementation


As with the array-based stack implementation, the
array is of fixed size
A queue of maximum N elements

Slightly more complicated


Need to maintain track of both front and rear
Implementation 1

Implementation 2

03/20/15

11

Array Implementation

Front
First Element
Second
Element
.

When queue is empty both front and rear are set to -1

While enqueueing increment rear by 1, and while


dequeueing increment front by 1

When there is only one value in the Queue, both rear


and front have same index

Can we implement Queue by using only one


index variable Front or Rear??

YES, by moving elements of array to


neighboring locations like we did in STACK but
this is in-efficient

Why it is inefficient?

Rear

Last Element
maxlength

03/20/15

12

Implementation 1: createQ, isEmptyQ,


isFullQ
Queue createQ(max_queue_size) {
# define MAX_QUEUE_SIZE 100
// Maximum queue size
int queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = -1;
Boolean isEmpty(queue) ::= front == rear
Boolean isFullQ(queue) ::= rear ==(n-1)

03/20/15

13

Implementation 1: enqueue
void enqueue(int rear, int item)
{
//add an item to the queue
if (rear == MAX_QUEUE_SIZE)
{
queue_full( );
return;
}
queue [++rear] = item;
}
03/20/15

14

Implementation 1: dequeue
int dequeue(int front, int rear)
{
// remove element at the front of the queue
if ( *front == rear)
return queue_empty( );
return queue [++ front];
}

03/20/15

15

Circular Queues and


Implementation with Array

Array Implementation
5

7
5

6
6

12

67

Front=0
Rear=6
8
0

7
5

6
6

Front=4
Rear=6
7
0

Front=5
Rear=8

03/20/15

6
6

How can we insert more elements? Rear index can


not move beyond the last element.
17

Solution: Using circular queue


Allow rear to wrap around the array.
if(rear == queueSize-1)
rear = 0;
else
rear++;
Or use modulo arithmetic
rear = (rear + 1) % queueSize;

03/20/15

18

Enqueing in a Circular Queue

7
0

6
6

12

67

Front=5
Rear=8
Enqueue 39

Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0

39
0

7
1

6
6

12

67

Front=5
Rear=0
03/20/15

19

Implementation 2: Wrapped Configuration


EMPTY QUEUE

[3]

[2]

[2]

[3]
J2

[1]

[4]

[0]

J3

[1] J1

[5]

front = 0
rear = 0

[4]

[0]

[5]

front = 0
rear = 3

Can be seen as a circular queue


03/20/15

20

How to determine empty and full Queues?


Leave one empty
space when queue
is full
Why?

FULL QUEUE
[2]

[3]
J2

[1]

FULL QUEUE
[2]
J8

J3

J1

J4
J5

[0]
front =0
rear = 5

[5]

[3]
J9

[4][1] J7

[4]
J6
[0]

J5
[5]

front =4
rear =3

How to test when queue is empty?


How to test when queue is full?
03/20/15

21

How to determine empty and full Queues?


Number of approaches
A counter indicating number of values in the queue
can be used (We will use this approach)
Comparing the Rear and the Front of the Queue

03/20/15

22

You might also like