You are on page 1of 32

1

CSC 220
Data Structures and Algorithms

Lecture # 8

Queues

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Lecture Outlines 2

Lecture Outlines
• Queue ADT
• Real-life Queues
• Using a Queue
• Queue Operations
• Examples:
 Queues using ArrayList
 Queues using Array
• Implementation of Queues:
 Using ArrayList
 Using Array

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Lecture Outlines 3

Lecture Outlines (cont.)


• Sorting Data with Queues: Radix Sort
• Priority Queues
• Implementation of Queue using Circular Array
• Allocation of Memory
• Implementation of Queue using Linked List

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue ADT 4

Queue ADT
• Queue: is an abstract data type (ADT) in which items are
added at one end (the rear) and removed from the other
end (the front).
• Queue is a FIFO “First In, First Out” structure.
 The first item in is the first item served (out).

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Real-life Queues 5

Real-life Queues

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Using a Queue 6

Using a Queue
• “Buffer” for printer uses a queue.
• “Buffer” for a mail server uses a queue.
• Operating System (OS) uses “Priority Queues”.
• Simulation applications use queues to model customers
waiting in a line.
• Any problem requiring FIFO property should use a
queue.

 Buffer: is a region of memory used to temporarily hold data.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 7

Queue Operations

Enqueue Adds new item to the rear of the queue.

Dequeue Removes the item at the front of the queue.

MakeEmpty Sets queue to an empty state.

IsEmpty Determines whether the queue is currently empty.

IsFull Determines whether the queue is currently full.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 8

Queue Operations (cont.)

Transformers
 Enqueue Change state
 Dequeue
 MakeEmpty

Observers
 IsEmpty Observe state

 IsFull

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Queue Operations 9

Basic Operations of a Queue

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Example 1 – Queues using Arrays 10

Example 1 – Queues using Arrays


0 1 2 3
-1
(1) New Queue:
Rear Front
0 1 2 3
(2) Enqueue('A'): A
Rear Front
0 1 2 3
(3) Enqueue('B'): A B
Front Rear
0 1 2 3
(4) Enqueue('C'): A B C
Front Rear
0 1 2 3
(5) Dequeue(): B C
Front Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Example 2 – Queues using ArrayLists 11

Example 2 – Queues using ArrayLists


0 1 2 3
-1
(1) New Queue:
Rear Front
0 1 2 3
(2) Enqueue('A'): A
Rear Front
0 1 2 3
(3) Enqueue('B'): A B
Front Rear
0 1 2 3
(4) Enqueue('C'): A B C
Front Rear
0 1 2 3
(5) Dequeue(): B C
Front Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues 12

Implementation of Queues

• Queues can be implemented using:


 Array (static: the size of queue is given initially).

 ArraysList (static and dynamic: never become full).

 Linked Lists (dynamic: never become full).

• Let us now see how to use ArrayList and Array to implement a queue.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using ArrayList 13

Queue class – using ArrayList


class CQueue {
private ArrayList pqueue;

public CQueue() {
pqueue = new ArrayList();
}
.
.
.
};

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using ArrayList 14

Enqueue Queue – using ArrayList


public void Enqueue(object item) {
pqueue.Add(item);
}

Dequeue Queue – using ArrayList


public object Dequeue() {
object obj = pqueue[0];
pqueue.RemoveAt(0);
return obj;
}

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using Array 15

Queue class – using Array


class CQueue {
private int front,rear,counter;
private int [] values;
public CQueue() {
values = new int[10];
front = 0;
rear = -1;
counter = 0;
}
.
.
.
};

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queues – using Array 16

Enqueue Queue – using Array


public void Enqueue(int item) {
if(counter == 10) // if queue is full, print error
Console.WriteLine("Error: the queue is full.");
else {
rear = rear + 1;
values[rear] = item;
counter++; }
}

Dequeue Queue – using Array


public int Dequeue() {
if(counter == 0) { //if queue is empty, print error
Console.WriteLine("Error: the queue is empty.");
return -1; }
else {
int x = values[front];
front = front + 1;
counter--;
return x; }
}
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Sorting Data with Queues: Radix Sort 17

Sorting Data with Queues: Radix Sort


• Radix sort is given here to demonstrate the usage of queues.
• Objects are sorted using a mechanism sorter that utilized
bin-like structures.

• The Radix sort works by making two passes over a set of


data, in this case integers in the range 0-99.
 The first pass: sorts the numbers based on the 1’s digit.

 The second pass: sorts the numbers based on the 10’s digit.

 Each number is then placed in a bin based on the digit in each of


these places.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Sorting Data with Queues: Radix Sort 18

Radix Sort Example


• Sort the following items using Radix Sort:
91, 46, 85, 15, 92, 35, 31, 22

Pass 1: 1’s digit Pass 2: 10’s digit


Bin 0: Bin 0:
Bin 1: 91, 31 Bin 1: 15
Bin 2: 92, 22 Bin 2: 22
Bin 3: Bin 3: 31, 35
Bin 4: Bin 4: 46
Bin 5: 85, 15, 35 Bin 5:
Bin 6: 46 Bin 6:
Bin 7: Bin 7:
Bin 8: Bin 8: 85
Bin 9: Bin 9: 91, 92

Pass 1 Result: The Final Result:


91,31,92,22,85,15,35,46 15,22,31,35,46,85,91,92
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Sorting Data with Queues: Radix Sort 19

Radix Sort Implementation


• We can implement Radix sort algorithm by using queues to
represent the bins.

• We need nine queues, one for each digit.


• We use modulus and integer division for determining the 1’s
and 10’s digits.

 See Textbook on Pages 88-90.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 20

Priority Queues
• As you know, a queue is a data structure where the first item
placed in the structure is the first item taken out of the
structure.

• The effect of the behavior is the oldest item in the structure


that is removed first.

• For many applications, though, a data structure is needed


where an item with the highest priority is removed first, even
if it is not the “oldest” item in the structure.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 21

Priority Queue Operations


• A priority queue is an abstract data type that supports the
following three operations:

 insert WithPriority: adds an item to the queue with an


associated priority.

 getNext: removes the item from the queue that has the highest
priority, and return it.
(also known as: “PopElementOff” or “GetMinimum”)

 peekAtNext (optional): looks at the item with highest priority


without removing it.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 22

Priority Queues - Example


 Suppose that we have a College with the following Priority values:

Student 0
Lecturer 1
Doctor 2
Head of Department 3
Dean 4

 Consider an empty ArrayList, with size 6, to be used for a priority queue


with front and rear indexes. Illustrate the following operations:
1. An empty queue 2. Enqueue(“Ali”,0)
3. Enqueue(“Omer”,0) 4. Enqueue(“Sami”,2)
5. Enqueue(“Fadi”,1) 6. Enqueue(“Ahmed”,2)
7. Dequeue() 8. Dequeue()
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 23

Priority Queues – Example Solution


-1 Front

1. An empty queue:
Rear

Front
Ali
2. Enqueue("Ali",0): 0
Rear

Front
Ali Omer
3. Enqueue("Omer",0): 0 0
Rear

Front
Sami Ali Omer
4. Enqueue("Sami",2): 2 0 0
Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Priority Queues 24

Priority Queues – Example Solution (cont.)


Front

5. Enqueue("Fadi",1): Sami Fadi Ali Omer


2 1 0 0
Rear

Front

6. Enqueue("Ahmed",2): Sami Ahmed Fadi Ali Omer


2 2 1 0 0
Rear

Front
Ahmed Fadi Ali Omer
7. Dequeue(): 2 1 0 0
Rear

Front
Fadi Ali Omer
8. Dequeue(): 1 0 0
Rear
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 25

Implementation of Queue using Circular Array


• When an item is enqueued, the rear index moves forward.
• When an item is dequeued, the front index also moves forward by
one element.

Front Rear
A B C

Front Rear
after 1 dequeue, and 1 enqueue: B C D

Front Rear
after 1 dequeue, and 1 enqueue: C D E

The problem here is that the rear index cannot move beyond the last
element in the array.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 26

Circular Array (cont.)


• Using a Circular Array, when an element moves past the end of
a Circular Array, it wraps around to the beginning.

• So, we can continue the above example as follows:


Front Rear
C D E

Rear Front
after 1 enqueue: F C D E

Rear Front
after 2 dequeue and 2 enqueue: F G H E

Front Rear
after 1 dequeue: F G H

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 27

Circular Array (cont.)


• How to move front and rear to the beginning of the array?
 Use modulo “%” with them.
 front = (front + 1) % arraySize;

 rear = (rear + 1) % arraySize;

• How to detect an empty or full queue, using a Circular Array?


 Use a counter to count the number of items in the queue.
 When Enqueue a new item: counter++;

 When Dequeue an item : counter--;

 if(counter == 0)  the queue is empty.


 if(counter == arraySize)  the queue is full.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Circular Array 28

Circular Array Time Complexity


 Assume Queue will not have more than K items.
 Create array S[] of size K: S[K]

Run-time complexity:
• Enqueue(), Dequeue(), … are all O(1)

But storage complexity is a problem:


• S(n) = K (where: K is the array size, n is number of Queue items)

 Will be wasteful (if K too large).


 Will be a problem (if K too small).

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Allocation of Memory 29

Allocation of Memory

Static Dynamic
Allocation Allocation

• Allocation of memory • Allocation of memory


space at compile time. space at run time.

• Example: Array • Example: Linked List

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 30

Implementation of Queue using Linked List

Key idea: allocate memory as required

 Keep Linked List with pointers to front and rear.


 Each queue item stored in a node.
 Each node has two fields: item and next.

front rear

X Y Z 

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 31

Queues using Linked List - Example:


front rear

(1) New Queue: NULL

front rear

(2) Enqueue('V'): 'V' 

front rear

(3) Enqueue('C'): 'V' 'C' 

front rear

(4) Enqueue('S'): 'V' 'C' 'S' 

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)
Implementation of Queue using Linked List 32

Queues using Linked List - Example: (cont.)


front rear

(5) Dequeue(): 'C' 'S' 

front rear

(6) Dequeue():
'S' 

front rear

(7) Dequeue(): NULL

front rear

(8) Enqueue('K'): 'K' 

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (8)

You might also like