Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Chapter 4: Queues

What is Queues ?
Queue is a data structure in which the elements
are added at one end, called the tail or rear, and
deleted from the other end, called the head or
front.
In a queue, the first to enter will be the first to
leave.
The middle elements of the queue are
inaccessible.
Queue is a First In First Out (FIFO) data structure.
What is Queues ?
Examples of queues are: customers
waiting (in a line) at a bank counter,
people waiting at a bus stop and cars
waiting at toll counter.
Each new customer gets in the line at
the rear and whenever a teller is ready
for a new customer, the customer at the
front of the line is served.
Queues Operations
Enqueue (queue insert) – Entering a
queue. After a new insertion is made,
the new element becomes the tail or
rear of the queue.
Dequeue (queue delete) – Delete from
queue. In a queue deletion, the earliest
data element will be deleted.
Exercise: Stack or Queue?
Cafeteria trays
Books on table
Ticket line
People in escalator
Printing Microsoft Word documents
Array Implementation of Queues
Suppose that FRONT is an index of the front
element of the queue, and REAR is an index
of the rear element of the queue.
When an element is inserted to the queue,
the value of REAR is increased by one.
When an element is deleted from the queue,
the value of FRONT is increased by one.
0 1 2 3 4 5
A
Insert
FRONT = 0
REAR = 0
Figure 1: After A is inserted

0 1 2 3 4 5
A B C D
Insert
FRONT = 0
REAR = 3
Figure 2: After B, C & D are inserted
0 1 2 3 4 5
C D
Delete
FRONT = 2
REAR = 3
Figure 3: After A & B are deleted

0 1 2 3 4 5
C D E F
Problem: What happen if
FRONT = 2 G is inserted ?
REAR = 5
Figure 4: After E & F are inserted
Circular Queue
Incrementing FRONT and REAR may pose a problem
when if it reach the maximum queue size (N).
Circular queue is used to solve this problem.
When REAR reach N-1, instead increasing REAR to N,
REAR is reset to 0.
Similarly if FRONT=N-1 and an element is deleted,
instead of increasing N-1 to N, FRONT is reset to 0.
This is done by using the mod operator;
REAR=(REAR+1)%N
FRONT=(FRONT+1)%N
Exercise
Mod operator
Given REAR=4; N=5; What is the value for
REAR after the following operation?
REAR=(REAR+1)%N;

Given FRONT=1; N=4; What is the value for


FRONT after the following operation?
FRONT=(FRONT+1)%N
Circular Queue
0 1 2 3 4 5
G C D E F
Insert
FRONT = 2
REAR = 0
Figure 5: After G is inserted
Empty Queue or Full Queue ? Exercise
0 1 2 3 4 5
A

FRONT = 0
REAR = 0
Figure 1: After A is inserted

What is the value for REAR & FRONT after B,C,D,E is


inserted and A,B,C,D,E is deleted?
FRONT = ?
REAR = ?
Empty Queue or Full Queue ? Exercise
0 1 2 3 4 5

FRONT = 5
REAR = 4

What is the value for REAR & FRONT after F, A,


B,C,D,E is inserted?
FRONT = ?
REAR = ?
Empty Queue or Full Queue ?
0 1 2 3 4 5
E

FRONT = 4
REAR = 4
Figure 6: Queue with one item

0 1 2 3 4 5

Delete
FRONT = 5
REAR = 4
Figure 7: Empty queue
Empty Queue or Full Queue ?
0 1 2 3 4 5
A B C D F

FRONT = 5
REAR = 3
Figure 8: Queue with five items

0 1 2 3 4 5
A B C D E F
Insert
FRONT = 5
REAR = 4
Figure 9: Full queue
Empty Queue or Full Queue ?
Notice that Figure 7 (Empty queue) and
Figure 9 (Full queue) have identical values for
FRONT and REAR.
This poses another problem: distinguishing
between an empty and full queue.
One possible solution is to introduce another
variable, COUNT to determine whether a
queue is empty or full.
Empty Queue or Full Queue ?
COUNT is incremented whenever a new
element is added to the queue, and
decremented whenever an element is
removed from the queue.
If COUNT is 0, then the queue is empty.
If COUNT is equal to N, then the queue
is full.
Empty Queue or Full Queue ?
0 1 2 3 4 5

FRONT = 5
REAR = 4
COUNT = 0
Figure 10: Empty queue during initialization

0 1 2 3 4 5
A B C D E F

FRONT = 5
REAR = 4
COUNT = 6
Figure 11: Full queue ( N = 6)
Algorithm for Queue Insertion
1. Check for queue overflow. If queue
overflow occurs, disallow further insertion
operation (overflow if COUNT=N).
2. Find new value of REAR
• (REAR=(REAR+1)%N)
3. Increase COUNT by one.
4. Insert new element to queue [REAR].
5. Exit
Algorithm for Queue Deletion
1. Check for queue underflow. If queue
underflow occurs, disallow further
deletion (underflow if COUNT=0)
2. Find new value of FRONT
(FRONT=(FRONT+1)%N)
3. Decrease COUNT by one.
4. Exit
Linked List Implementation of Queues
Because the size of array is fixed, only a finite
number of queue element can be stored in
the array.
Linked implementation will solve this problem
because the memory to store a queue
element is allocated dynamically, the queue is
never full unless the memory is full.
Two pointers, REAR and FRONT represent the
front and rear of queue.
Enqueue Operation REAR point to
last node
Before insertion
1st node 2nd node 3rd node
… 2 4 6
front
7
newnode
After insertion
1st node 2nd node 3rd node
… 2 4 6
front
REAR point to
last node 7
newnode
Enqueue Algorithm
1. Create a new node.
2. Enter the new node information and set the
link field of the new node to NULL.
3. If FRONT is NULL
1. FRONT point to NEWNODE
2. REAR point to NEWNODE
4. If FRONT is not NULL
1. Link field of REAR point to NEWNODE
2. REAR point to NEWNODE
Dequeue operation
Before deletion
1st node 2nd node 3rd node
… 2 4 6
front
Node to delete
FRONT now point to
second node
After deletion
1st node 2nd node 3rd node
… 2 4 6
front
Dequeue Algorithm
1. TEMP point to FRONT
2. If FRONT is NULL
1. List is empty
3. If FRONT is not NULL
1. FRONT point to link field of FRONT
2. Delete TEMP

You might also like