0% found this document useful (0 votes)
41 views34 pages

Queues

The document provides an overview of queues as a data structure. It discusses that unlike stacks, queues have two ends - one for adding elements (rear) and one for removing elements (front). Elements are removed in FIFO order. It then covers array-based and linked list implementations of queues, including operations like enqueue, dequeue, and the use of front and rear pointers. It also discusses solutions to problems like overflow in array-based queues through the use of circular queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views34 pages

Queues

The document provides an overview of queues as a data structure. It discusses that unlike stacks, queues have two ends - one for adding elements (rear) and one for removing elements (front). Elements are removed in FIFO order. It then covers array-based and linked list implementations of queues, including operations like enqueue, dequeue, and the use of front and rear pointers. It also discusses solutions to problems like overflow in array-based queues through the use of circular queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Data Structures & Algorithms

Lecture 8: Queues
Today‘s Lecture

▪ Basic data structures including


► Arrays & structures

► Linked Lists (single, double & circular)

► Stacks (and its applications)

► Queues

- Array based implementation


- Circular queue
- Linked based implementation

Lecture 8: Queues 2
Queues
▪ Unlike a stack, a queue is a structure in which both ends are
used: one for adding new elements and one for removing them

▪ 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)
► I.e., it is simply a waiting line that grows by adding elements
to its end and shrinks by taking elements from its front
► Thus, the last element has to wait until all elements
preceding it on the queue are removed
► It is data structure which implements a First-In, First-Out
(FIFO) behaviour i.e., elements are removed in the same
order as they were entered

Lecture 8: Queues 3
Queues

▪ Unlike a stack, a queue is a structure in which both ends are


used: one for adding new elements and one for removing
them

Examples:
Printer SPOOL
CPU Scheduling
(at times)

First one in line is


the first to serve

Lecture 8: Queues 4
Queues

▪ Basically data enters the queue at one end and exits at


the other

Lecture 8: Queues 5
Queue Attributes

▪ Size: Represents the total number of elements in a


queue

▪ Front: Points to the first element of queue


► Used for removal

▪ Rear: Points to the last element of queue


► Used for insertion
A B C
Front Rear
Rear
Front
B C
Lecture 8: Queues 6
Queue Operations

▪ Primarily two operations performed on both ends

► Enqueue: Add an item at the rear of queue

► Dequeue: Remove an item from the front of queue

Lecture 8: Queues 7
Queue Operations

▪ Enqueue Concerns
► What if when the queue is full?

▪ Dequeue Concerns
► What if the queue is empty

Solution:

▪ Before any enqueue, check if the queue is already full


or not

▪ Before dequeue, check if queue is not already empty


Lecture 8: Queues 8
Queue Operations

Enqueue(element) // Add an element at the end of the queue


Dequeue() // Remove an element from the front of the queue

isEmpty() // Check to see if queue is empty

isFull() // Check to see if queue is full


FirstElement() // Returns first element in queue without removing it

Clear() // Clear the queue

Lecture 8: Queues 9
Choice of implementation

▪ Any implementation of a queue requires:


► Storage for the data as well as markers (“pointers”) for the
front and for the back of the queue

Two possible choices:

▪ Array based queue: Maximum queue size is known


ahead of time

▪ Linked List based queue: Maximum queue size unknown

Lecture 8: Queues 10
Array based queue implementation

▪ An array-based implementation would need structures


like

► An array to store the elements of the queue, items

► An index to track the front queue element, Front

► An index to track the position following last queue


element, Rear

▪ Additions to the queue would result in incrementing


Rear while deletions from the queue would result in
incrementing Front
Lecture 8: Queues 11
Array based queue implementation

#define length 5
Struct q // queue
{
int items[length];
int front, rear;
}
insert(q, x) // enqueue
q.items[++q.rear]=x;
X=remove(q) // dequeue
x=q.items[++q.front];

Lecture 8: Queues 12
Array based queue implementation

4 4
3 3
2 2 C q.rear = 2
1 1 B
0 q.front = 0 0 A q.front = 0
q.rear = -1

q.rear < q.front


▪ The queue is empty when
▪ The number of elements in the queue at any time is equal
to the value ofq.rear – q.front + 1
Lecture 8: Queues 13
Problem: Run out of space
Any problem ?
4 4 E q.rear = 4
3 3 D
2 C q.front = q.rear 2 C q.front = 2
1 =2
1
0 0
▪ Now there are 3 elements the queue but there is room for 5
▪ To insert a new value F, q.rear must be increased by 1 such
that q.items[5] is set to the value F but q.items is an array of
only 5 elements so this insertion cannot be made

Lecture 8: Queues 14
One possible solution: Shift items

▪ Shifting the elements downward with each deletion


► Check if array is not empty

► Simply dequeue from the first location of array, say

array[0] i.e., the zeroth index


► After dequeue shift all the elements of the array
from array[index] to array[index-1]

0 1 2 3 4
A B C D

Rear Rear
0 1 2 3 4
B C D dequeue

Lecture 8: Queues 15
Dequeue operation

x = q.items[0];
for ( i = 0 ; i < q.rear; i++ )
q.items[i] = q.items[i+1]
q.rear--;

▪ Method is costly as we have to move all the elements


of the array
▪ Do we need Front Index in this case?
► No, because we are always dequeue(ing) from the

first index

Any better idea?


Lecture 8: Queues 16
Circular queue

▪ To avoid the costly operation of coping all the elements


again we employ another method called circular queue

▪ Idea: To view array as a circular buffer, i.e. wrapping


the end to the front
[5] [4]
[0] [1] [2] [3] [4] [5]

[0] [3]

[1] [2]

Lecture 8: Queues 17
Circular Queue Operations cont.
The queue is empty now
front Condition: front
front - 1 == rear
q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
? ? ? ? ? ? A ? ? ? ? ?
rear (1) Empty Queue (2) Enqueue(A)
rear
front front

q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D ? ? ? ? A D T ? ? ?

(3) Enqueue (D)


rear rear
(4) Enqueue (T)
Lecture 8: Queues 18
Circular Queue Operations cont.

front front

q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D T E ? ? A D T E F ?

rear rear
(5) Enqueue (E) (6) Enqueue (F)
front

q[0] q[1] q[2] q[3] q[4] q[5] Now the queue is full, what is
A D T E F G the condition to check that?
Condition:
rear front == 0 && rear == Max-1
(7) Enqueue (G)

Lecture 8: Queues 19
Circular Queue Operations cont.

front front

q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
A D T E F G A D T E F G

(8) Dequeue rear rear


(9) Dequeue
Is it possible now, to insert new item
into the queue?
Yes
Where would it be inserted?
At 0th index, as we are using circular
queue
Lecture 8: Queues 20
Circular Queue Operations cont.
front front

q[0] q[1] q[2] q[3] q[4] q[5] q[0] q[1] q[2] q[3] q[4] q[5]
H D T E F G H I T E F G

rear rear
(10) Enqueue (H) (12) Enqueue (I)
Now the queue is full, what is the condition to check
that?
Condition:
front - 1 == rear
NOTE: it is the same condition as if queue
is empty
We need a distinguishing condition for empty queue
and full queue
Lecture 8: Queues 21
Circular Queue Operations cont.

▪ In order to have distinguishing condition for empty


queue and full queue:
► Keep an empty slot between Front and Rear
rear front

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


H I T E F G

▪ The condition would then be:


( (rear + 1) % Max == Front - 1)

▪ Now the condition to check an empty queue is different


than the condition to check a full queue.
Lecture 8: Queues 22
Circular Queue Operations cont.

front
front = 0
q[0] q[1] q[2] q[3] q[4] q[5] rear = 5
H I T E F G
Condition:
front == 0 && rear == Max - 1
front rear

front = 2
q[0] q[1] q[2] q[3] q[4] q[5]
rear = 1
H I T E F G
Condition:
( (rear + 1) % Max == front - 1)
rear

Two conditions to check if the queue is full

Lecture 8: Queues 23
Better way!

▪ Initialize both front and rear to be “Max – 1”


front

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


? ? ? ? ? ?

rear

▪ Change condition to check whether queue is full to:


( (rear + 1) % Max == front)
▪ Change condition to check whether queue is empty to:
rear == front
Lecture 8: Queues 24
Implementation

#define length 5
bool IsFull(struct queue *pq)
struct queue
{
{
return (
int items[length]; int
(pq->rear + 1) % length == pq->front
front, rear;
);
}; }
Struct queue q;
q.front = length –1; //initialize
q.rear = length –1; // initialize
Full: ( (rear + 1) % length == front)
bool IsEmpty(struct queue *pq) Empty: rear == front
{
return(pq->front==pq->rear)
}

Lecture 8: Queues 25
Implementation cont.

void insert(struct queue *pq, int x) int remove (struct queue *pq){
{ if (! isEmpty(pq)){
pq->front = (pq->front + 1) % length;
if (!isFull(pq))
return pq->items[pq->front];
{ }
pq->rear = (pq->rear + 1) % length;
else{
pq->items[pq->rear] = x; cout<<“queue underflow”
}
}
else return 0;
cout<< “Queue is full: ”; }
}

Lecture 8: Queues 26
Another way

▪ To have different conditions for a full and empty circular


queue
► Add a ‘count’ variable which keeps track of how many
elements exist in the queue
► Initialize count to zero
► Increment count each time an element is enqueued
► Decrement count each time an element is dequeued

Lecture 8: Queues 27
struct Queue {
int Count; // number of queue items
int Front; // head of queue
int Rear; // tail of queue
int Items[length];
};

void InitializeQueue(Queue *Q)


{ Q->Count = 0; // zero count of items
Q->Front = length-1;

Q->Rear = length-1;
}
int isEmpty(Queue *Q)
{ return (Q->Count == 0); }

int isFull(Queue *Q)


{ return (Q->Count == length); }
28
Linked List Based Implementation of Queues?

Lecture 8: Queues 29
Linked List Based Implementation of Queues?

Lecture 8: Queues 30
Priority queues
▪ In many situations, simple queues are inadequate, because first
in/first out scheduling has to be overruled using some priority criteria
► A handicapped person may have priority over others and may be
served first
► On roads with tollbooths, some vehicles may be put through
immediately, even without paying (police cars, ambulances, fire
engines, and the like)
► In a sequence of processes, process P2 may need to be
executed before process P1 for the proper functioning of a
system
▪ In priority queues, elements are dequeued according to their
priority and their current queue position
▪ Although priority queue can be implemented using many of the data
structures e.g., an array, a linked list etc., they do not provide the most
efficient operations. To make all of the operations very efficient, heap
data structure is typically employed

Lecture 8: Queues 31
Deques (‚Decks‘)

▪ A deque is a double-ended queue


▪ Insertions and deletions can occur at either end
▪ Implementation is similar to that for queues
▪ This differs from the queue abstract data type or First-In-
First-Out List (FIFO), where elements can only be added to
one end and removed from the other

remove remove
rear front from front
from rear

Add to rear Add to front

Lecture 8: Queues 32
Deque Operations

Lecture 8: Queues 33
Deques

▪ Both the basic and most common list types in computing,


queues and stacks can be considered specializations of
deques, and can be implemented using deques
► I.e., this hybrid linear structure provides all the capabilities
of stacks and queues in a single data structure

▪ Deques are not heavily used

▪ You should know what a deque is, but we won’t explore


them much further

Lecture 8: Queues 34

You might also like