IT2070 Lecture 02 2023
IT2070 Lecture 02 2023
Queues
2
SLIIT - Faculty of Computing
Data Structures and Algorithms
Queues
In a queue all insertions are made at the Rear end and deletions are made at the Front
end.
Insertions and deletions are restricted from the Middle of the Queue.
The elements are inserted and removed according to the First-In-First-Out (FIFO) principle.
3
SLIIT - Faculty of Computing
Data Structures and Algorithms
Queue - Insert
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
15 27 39 23 15 27 39 23 15 27 39 23 45
New Element
(Insert)
4
SLIIT - Faculty of Computing
Data Structures and Algorithms
Queue - Remove
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
15 27 39 23 15 27 39 23 27 39 23
5
SLIIT - Faculty of Computing
Data Structures and Algorithms
Queue - PeekFront
0 1 2 3 4 5 0 1 2 3 4 5
15 27 39 23 15 27 39 23
Peek 15
Peek is used to read the value from the Front of the queue without
removing it. You can peek only the Front item, all the other items are
invisible to the queue user.
6
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 01
Draw the Queue frame after performing the below operations to the queue given
below.
0 1 2 3 4 5 6
60 47 99 67 24
e of
Front Rear siz 7
s
u m ue i
im
i) Insert item 33 ax que
M is
ii) Insert item 53 th
iii) peekFront
iv) remove
v) remove
vi) remove
vii) remove
viii) remove
7
SLIIT - Faculty of Computing
Data Structures and Algorithms
Uses of Queue
printer queue
stores keystroke data as you type at the keyboard
pipeline
8
SLIIT - Faculty of Computing
Data Structures and Algorithms
Queue - Implementation
Queue implementation using an array with restricted access
Constructor creates a new Queue class QueueX {
private int maxSize; // size of queue array
of a size specified in its argument. private int [] queArray;
private int front; //front of the queue
Variable front, which stores the private int rear; //rear of the queue
index of the item on the front of the private int nItems; //no of items of the queue
queue. publc QueueX (int s) {// constructor
12
SLIIT - Faculty of Computing
Data Structures and Algorithms
}
nItems = 0; // no items
public int peekFront() {
public void insert(int j) { // check whether queue is empty
// check whether queue is full // if not
if (rear == maxSize – 1)
System.out.println(“Queue is full”); // access item
else {
}
queArray[++rear] = j;
nItems++;
}
}
}
13
SLIIT - Faculty of Computing
Data Structures and Algorithms
queArray[++rear] = j;
nItems++;
}
}
14
}
SLIIT - Faculty of Computing
Data Structures and Algorithms
queArray[++rear] = j;
nItems++;
}
}
15
}
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 02
isEmpty() method of the Queue class returns true if the Queue is empty and
isFull() method returns true if the Queue is full.
16
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 03
Draw the Queue frame after performing the below operations to the queue given below.
0 1 2 3 4 5 6
67 12 22 55 34 70 60
Front Rear
i) remove
ii) remove
iii) remove
iv) Insert item 88
17
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 03 Contd..
Draw the Queue frame after performing the below operations to the queue given below.
0 1 2 3 4 5 6 0 1 2 3 4 5 6
77 67
67 12 1222 2255 55 3434 7070 60
60 77 67 12 2255 55 3434 7070 60
60
18
SLIIT - Faculty of Computing
Data Structures and Algorithms
0 0 1 4 5 6
2 3
77 67 12 2255 55 3434 7070 60
60
Rear
6 60 1
Front Rear
70
5 2
34 55
Front
4 3
19
SLIIT - Faculty of Computing
Data Structures and Algorithms
Circular Queue
The problem in using the linear queue can be overcome by using circular
queue.
i.e. if the queue is not full we can make the rear start from the beginning
by wrapping around
Question 04
Draw the Queue frame after performing the below operations to the circular queue
given below.
i) insert(14);
ii) insert(29);
iii) insert(33);
iv) insert(88);
v) peekFront();
vi) remove();
vii) remove();
viii) insert(90);
ix) insert(100);
x) peekFront();
21
SLIIT - Faculty of Computing
Data Structures and Algorithms
nItems++;
}
}
22
SLIIT - Faculty of Computing
Data Structures and Algorithms
}
}
23
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 05
24
SLIIT - Faculty of Computing
Data Structures and Algorithms
Question 06
Creating a Queue
Using the implemented QueueX class, Write a program to create a queue with
maximum size 10 and insert the following items to the queue.
10 25 55 65 85
Delete all the items from the queue and display the deleted items.
25
SLIIT - Faculty of Computing
Data Structures and Algorithms
Creating a Queue
class QueueApp {
public static void main(String[] args) {
QueueX theQueue = new QueueX(10); // create a queue with max size 10
26
SLIIT - Faculty of Computing
Data Structures and Algorithms
References
27
SLIIT - Faculty of Computing