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

Lecture 4-Queues 071655

This lecture covers the concept of queues, a FIFO data structure where insertions occur at the rear and deletions at the front. It discusses various types of queues, including linear, circular, priority, and double-ended queues, along with their implementations and applications in computer science and real-world scenarios. Additionally, it outlines operations on queues and implementation methods using arrays and linked lists.
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)
6 views

Lecture 4-Queues 071655

This lecture covers the concept of queues, a FIFO data structure where insertions occur at the rear and deletions at the front. It discusses various types of queues, including linear, circular, priority, and double-ended queues, along with their implementations and applications in computer science and real-world scenarios. Additionally, it outlines operations on queues and implementation methods using arrays and linked lists.
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/ 35

Lecture Four

LINEAR LISTS
(Queues)

ICT 224– DATA STRUCTURES AND ALGORITHMS,


By Matthew Cobbinah 1
Objectives:
At the end of this unit, you will be able to:
 Examine queue processing
 Define a queue abstract data type
 Demonstrate how a queue can be used to solve
problems
 Explain the different types of Queues
 Examine various queue implementations
 Outline some applications of queues
 Compare queue implementations

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 2


Matthew Cobbinah
QUEUE
 An ordered list with the basic restriction that insertions
are done at one end (rear or tail) and deletions are done
at the other (front or head)
 Queue can also be defined as the list or collection in
which the insertion is done from one end known as the
rear end or the tail of the queue, whereas the deletion is
done from another end known as the front end or the
head of the queue.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 4


Matthew Cobbinah
QUEUE
 A queue is a FIFO (first in, first out) data structure
 Elements are stored in order of insertion
 Client can only add to the end of the
queue, and can only examine/remove
the front of the queue

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 5


Matthew Cobbinah
Conceptual View of a Queue
Adding an element

Front of queue

New element is
added to the rear of
the queue

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 6


Matthew Cobbinah
Conceptual View of a Queue
Removing an element

New front element of queue

Element is removed
from the front of the
queue
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 7
Matthew Cobbinah
Queues in computer science
 Operating systems:
 Waiting lists for a single shared resource like printer,
disk, CPU.
 Queue of programs / processes to be run
 Queue of network data packets to send
 In operating systems for handling interrupts.
 Keyboard input buffer
 (as buffers also in MP3 media player, CD player)

 Programming:
 Modeling a line of customers or clients
 Storing a queue of computations to be performed in
order
 GUI event queue (click on buttons, menu items)

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 8


Matthew Cobbinah
Real world examples of Queues
 In simulation studies, where the goal is to
reduce waiting times:
 Optimize the flow of traffic at a traffic light
 Determine number of cashiers to have on
duty at a grocery store at different times of
day
 People on an escalator or waiting in a line
 Cars at a gas station (or on an assembly
line)

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 9


Matthew Cobbinah
Operations on a Queue
Operation Description

dequeue Removes an element from the front of the queue

enqueue Adds an element to the rear of the queue

First/peek Examines the element at the front of the queue

isEmpty Determines whether the queue is empty

size Determines the number of elements in the queue

toString Returns a string representation of the queue

isFull Determines whether the queue is full


ICT 224– DATA STRUCTURES AND
ALGORITHMS, By Matthew Cobbinah 6-10
Using Queues (in Java)
add(value) places given value at back of queue
remove() removes value from front of queue and returns it;
throws a NoSuchElementException if queue is empty
peek() returns front value from queue without removing it;
returns null if queue is empty
size() returns number of elements in queue
isEmpty() returns true if queue has no elements

Queue<Integer> q = new LinkedList<Integer>();


q.add(42);
q.add(-3);
q.add(17); // front [42, -3, 17] back
System.out.println(q.remove()); // 42

 IMPORTANT: When constructing a queue you must use a


new LinkedList object instead of a new Queue object.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 11
Matthew Cobbinah
Types of Queues
There are four different types of queue that are
listed as follows:

1. Simple or Linear or
ordinary
2. Circular
3. Priority
4. Double Ended (Deque)

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 12


Matthew Cobbinah
1. Simple Queue or Linear Queue
 Insertion takes place from one end while the deletion
occurs from another end.
 Also known as ordinary queue
 The end at which the insertion takes place is known
as the rear end, and the end at which the deletion
takes place is known as front end.
 It strictly follows the FIFO rule.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 13


Matthew Cobbinah
Drawback of Linear Queue
 The major drawback of using a linear Queue is that
insertion is done only from the rear end.
 Ifthe first three elements are deleted from the Queue,
we cannot insert more elements even though the space
is available in the queue.
 In this case, the linear Queue shows the overflow
condition as the rear is pointing to the last element of the
Queue

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 14


Matthew Cobbinah
2. Circular Queue
 In Circular Queue, all the nodes are represented as circular.
 The last node points to the first node and creates a circular
connection.
 It is similar to the linear Queue except that the last element
of the queue is connected to the first element.
 It is also known as Ring Buffer, as all the ends are
connected to another end.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 15


Matthew Cobbinah
2. Circular Queues…
 The drawback that occurs in a linear queue is overcome by
using the circular queue.
 If the empty space is available in a circular queue, the new
element can be added in an empty space by simply
incrementing the value of rear.
 The main advantage of using the circular queue is better
memory utilization when the queue has a fixed size.
 It’s used to switch on and off the lights of the traffic signal
systems.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 16


Matthew Cobbinah
3. Priority Queue
 A priority queue is a special kind of queue in which each
item has a predefined priority of service.
 In this queue, the enqueue operation takes place at the
rear in the order of arrival of the items, while the dequeue
operation takes place at the front based on the priority of
the items.
 That is to say that an item with a high priority will be dequeued
before an item with a low priority.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 17


Matthew Cobbinah
3. Priority Queue…
 In the case, when two or more items have the same priority,
then they’ll be dequeued in the order of their arrival.
 Hence, priority queue may or may not strictly follow the
FIFO rule
 It’s used in interrupt handling, A* search algorithm, heap
sort, and Huffman code generation.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 18


Matthew Cobbinah
Types of Priority Queue
1. Ascending priority queue –
 Elements can be inserted in arbitrary order, but only smallest
can be deleted first. Suppose an array with elements 7, 5,
and 3 in the same order, so, insertion can be done with the
same sequence, but the order of deleting the elements is 3,
5, 7.
2. Descending priority queue –
 Elements can be inserted in arbitrary order, but only the
largest element can be deleted first. Suppose an array with
elements 7, 3, and 5 in the same order, so, insertion can be
done with the same sequence, but the order of deleting the
elements is 7, 5, 3.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 19


Matthew Cobbinah
Double Ended Queue (Deque)
 In this queue, both the enqueue and dequeue operations
take place at both front and rear.
 That means, we can insert an item at both the ends
and can remove an item from both the ends.
 Thus, it may or may not adhere to the FIFO order:
 Deque can be used both as stack and queue
 It allows the insertion and deletion operations on both
ends.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 20


Matthew Cobbinah
Types of Deque
 Deque has two special cases:
 input-restricted deque and output-restricted deque.

1. Input restricted deque –


 As the name implies, in input restricted queue,
insertion operation can be performed at only one end,
while deletion can be performed from both ends.
 An input-restricted queue is useful when we need to
remove an item from the rear of the queue.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 21


Matthew Cobbinah
Types of Deque…
 Deque has two special cases:
 input-restricted deque and output-restricted deque.
 …
2. Output restricted deque –
 As the name implies, in output restricted queue,
deletion operation can be performed at only one end,
while insertion can be performed from both ends.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 22


Matthew Cobbinah
Queue Implementation
 There are two ways of implementing Queue:
 Implementation using array:
 The sequential allocation in a Queue can be
implemented using an array.
 Implementation using Linked list:
 The linked list allocation in a Queue can be
implemented using a linked list. For more details,

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 23


Matthew Cobbinah
Queue Implementation Issues
 What do we need to implement a queue?
 A data structure (container) to hold the data elements
 Something to indicate the front of the queue
 Something to indicate the end of the queue

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 24


Matthew Cobbinah
Linked Implementation of a Queue
 Internally, the queue is represented as a linked list of
nodes, with each node containing a data element
 We need two pointers for the linked list
 A pointer to the beginning of the linked list (front of
queue)
 A pointer to the end of the linked list (rear of queue)
 We will also have a count of the number of items in the
queue

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 25


Matthew Cobbinah
Linked Implementation of a Queue

A queue q containing four elements

rear
q

front

4
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 26


Matthew Cobbinah
Discussion

 What if the queue is empty?

 What if there is only 1 element?

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 27


Matthew Cobbinah
Queue After Adding Element
New element is added in a node at the end of the list, rear points
to the new node, and count is incremented

rear
q
front

5
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 28


Matthew Cobbinah
Queue After a dequeue Operation
Node containing is removed from the front of the list (see previous
slide), front now points to the node that was formerly second, and count
has been decremented.

rear
q
front

4
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 29


Matthew Cobbinah
Array Implementation of a Queue
 First Approach:
 Use an array in which index 0 represents one end of
the queue (the front)
 Integer value count represents the number of
elements in the array (so the element at the rear of
the queue is in position count - 1)

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 30


Matthew Cobbinah
An Array Implementation of a Queue

A queue aq containing four elements

front
0 1 2 3 4

queue
aq
4
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 31


Matthew Cobbinah
Queue After Adding an Element
The element is added at the array location given by the value of
count and then count is increased by 1.

0 1 2 3 4

queue
aq
5
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 32


Matthew Cobbinah
Queue After Removing an Element
Element is removed from array location 0, remaining
elements are shifted forward one position in the array, and then
count is decremented.

0 1 2 3 4

queue
aq
4
count

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 33


Matthew Cobbinah
Second Approach: Queue as a Circular Array

 If we don't fix one end of the queue at index 0, we won't


have to shift elements
 Circular array is an array that conceptually loops
around on itself
 The last index is thought to “precede” index 0
 In an array whose last index is n, the location
“before” index 0 is index n; the location “after” index
n is index 0
 Need to keep track of where the front as well as the rear
of the queue are, at any given time

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 34


Matthew Cobbinah
Conceptual Example of a Circular Queue
front
After 5
1 After 7 enqueues 1 dequeues
0 0

front
12 12
11 11
rear rear
10 10

rear
1
0
front
12
11
After 8 more enqueues
10
35
ICT 224– DATA STRUCTURES AND ALGORITHMS,
Circular Array Implementation
 When an element is enqueued, the value of rear is
incremented
 But it must take into account the need to loop back to
index 0:

rear = (rear+1) % queue.length;

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 36


Matthew Cobbinah

You might also like