0% found this document useful (0 votes)
30 views

08 Queue and Circular Queue

This document discusses queues and circular queues. It defines a queue as a collection where items are inserted at one end and removed from the other, following a First-In First-Out (FIFO) approach. Key queue operations like insertion, removal and implementation using arrays are described. The document also covers issues with linear/array implementation like overflow and presents the circular queue as an alternative solution that views the array as a circular structure.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

08 Queue and Circular Queue

This document discusses queues and circular queues. It defines a queue as a collection where items are inserted at one end and removed from the other, following a First-In First-Out (FIFO) approach. Key queue operations like insertion, removal and implementation using arrays are described. The document also covers issues with linear/array implementation like overflow and presents the circular queue as an alternative solution that views the array as a circular structure.

Uploaded by

Haseeb Muhammad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

1

Algorithms and Data


Structures

Queue and Circular Queue


Dr. Sohail Iqbal
Associate Professor
Department of Computing,
SEECS
Overview
2

 What is a Queue?
 Queue Operations.
 Applications.
 Linear Implementation.
 Circular Implementation.

With thanks to Dr. Yasir Faheem and Dr. Aimal Tariq for DSA support!
What is Queue
3

 A queue is an ordered collection of items from


which items may be deleted at one end (called the
front of the queue ) and into which items may be
inserted at the other end ( called the rear of the
queue ).
 That’s why a Queue is often called FIFO (First in
First Out ).
Example
4

0 1 2 3 4 5 6 7

Front Rear
Insert red block operation
5

0 1 2 3 4 5 6 7

Front Rear
Insert operation
6

0 1 2 3 4 5 6 7

Front Rear
Remove operation
7

0 1 2 3 4 5 6 7

Front Rear

This comes off the


queue
Remove operation
8

0 1 2 3 4 5 6 7

Front Rear

This comes off the


queue
Insert operation
9

0 1 2 3 4 5 6 7

Front Rear
Applications
10

 In operating systems, e.g. printer queues,


process queues, etc.
 Bus Ticket Queues
 Simulation programs.
 Algorithms
Queue Operations
11

 Initialize a Queue
 Insert Operation( formally called Enqueue)
 Remove Operation (formally called Dequeue)
 Empty Operation
 Full Queue Operation
Insert Operation
12

Before Insertion

Front Rear
Insert Operation(Contd)
13

After Insertion

Front Rear
Remove Operation
14

Front Rear
Remove Operation(Contd)
15

After Removal

Front Rear

This comes off the


queue
implementation of Queues
16

 We will use two variables : front and rear , to hold


the position within the array of the first and last
elements of the queue.

public class ArrayBndQueue


{
protected final int DEFCAP = 100; // default capacity
protected int[] queue; // array that holds queue elements
protected int numElements = 0; // number of elements n the queue
protected int front = 0; // index of front of queue
protected int rear; // index of rear of queue
17

 To initialize queue , we set :


rear = -1 ;
& front = 0 ;
 Suppose MAXQUEUE = 5
 The queue is empty whenever rear < front
 The number of elements in the queue at any time is
equal to the value :-
rear – front + 1
 We are using class to implement queue.
QUEUE-Linear Implementation
18
public class queue_linear
{
int [] items = new int[MAXQUEUE];
int front,rear;

public :
queue_linear();
int full();
int empty();
int remove() ;
int insert(int x);
};
Constructor
19

public queue_linear()
{
front=0;
rear=-1;
}
Empty Operation
20

boolean isEmpty()
{
if (rear<front)
return true;
else
return false;
}
Full Operation
21

boolean isFull()
{
if (rear==MAXQUEUE-1)
return true;
else
return false ;
}
Insert (Enqueue) Operation
22

 Inserts new elements at the back end (rear) of the


queue, by incrementing rear.
 Also known as enqueue.
Enqueue Operation
23

public boolean Enqueue(int x)


{
if (full())
{
System.out.println(“Queue Overflow);
return false;
}
else {
items[++rear]=x;
return true;
}
}
Remove (Dequeue) Operation
24

 Removes the element pointed to by front , from the


front of the queue , by incrementing front.
 This operation is also known as Dequeue.
Dequeue Operation
25

public int Dequeue()


{
if (empty())
{
System.out.println(“Queue Underflow“);
return special_value;
}
else
{
return items[front++];
}

}
Problems related to Array Implementation of
Queues
26

 Using array to implement queue introduces the


possibility of overflow if the queue should grow
larger than the size of the array.
 Now , we study other problems that arise in the
linear implementation of queue.
(Contd)
27
q.items

4
3

2 q.Front = 0
q.Rear = -1
1

Queue is intialiized
(Contd)
28

2 C
q.Rear = 2
q.Front = 0
1 B

0 A

After instering three elements


(Contd)
29

4
3

2 C q.front = q.rear=2

After deleting two elements


(Contd)
30

4 E
3 D

2 C q.front = 2
q.rear=4
1

After inserting two elements


Problem of memory allocation
31

 In the figure on the previous slide , we have an


interesting situation
 Here q.front = 2 and q.rear = 4
 So , 4 - 2 +1 =3 , elements in the queue
 Queue has the room for five elements but we can’t
insert new elements in the queue
 Because to insert new element , q.rear must be
incremented to 5 , which is outside the bounds of
the array.
Solution 1
32

 When an item is deleted , the entire queue should


be shifted to the beginning of the array.
 This method is too inefficient
 Because if queue has thousands of elements , each
deletion would involve moving all of them.
 So , we should look for a more efficient
implementation of queue.
Solution 2
33

 Another solution is to view the array that holds the


queue as a circle rather than straight line.
34

Circular Queue
Circular Implementation
35

7 0

6 1

5 2

4 3
We shall see the details
Circular Implementation(Contd)
36

 We can see that , in this implementation the first


element immediately follows last element.
 Even if the last element is occupied , we can insert
new element in the first position, as long as the first
element is empty.
 In this implementation , we have an additional
variable count , to keep track of the number of
elements in the queue.
37

THE END

You might also like