of 55
8/10/2019 16 queues_JAVA
1/55
27/1
Queues
Reading: Lewis and Loftus, JAVA:
Software Solutions, (3rded), Chapter 12.2Savitch Chapter 10.2
8/10/2019 16 queues_JAVA
2/55
27/2
Objectives
To introduce the definition of a queue
To learn how to implement a queue using a
linked list
To develop an implementation of a queue
using an array
8/10/2019 16 queues_JAVA
3/55
27/3
Queues
8/10/2019 16 queues_JAVA
4/55
27/4
What is a queue?
8/10/2019 16 queues_JAVA
5/55
27/5
What is a queue?
8/10/2019 16 queues_JAVA
6/55
27/6
What is a queue?
8/10/2019 16 queues_JAVA
7/5527/7
What is a queue?
8/10/2019 16 queues_JAVA
8/5527/8
What is a queue?
8/10/2019 16 queues_JAVA
9/5527/9
What is a queue?
8/10/2019 16 queues_JAVA
10/55
27/10
What is a queue?
8/10/2019 16 queues_JAVA
11/55
27/11
What is a queue?
8/10/2019 16 queues_JAVA
12/55
27/12
What is a queue?
8/10/2019 16 queues_JAVA
13/55
27/13
Definition of a queue
A list in which cells may be removed only
from the front of the list and new cells may
be added only to the end of the list
Called a FIFO list (first-in, first-out)
8/10/2019 16 queues_JAVA
14/55
27/14
Queue Operations
insertto add an element at the rear of thequeue
getto get the value and delete the elementat the front of the queue
isEmptyto return true if the queue has no
element isFullto return true if the queue is full
(array implementation)
8/10/2019 16 queues_JAVA
15/55
27/15
Class exercise: queues
int x;Queue q;
q.insert (8);q.insert (9);q.insert (3);x = q.get ();q.insert (18);
x = q.get ();q.insert (22);
while (! q.isEmpty())
System.out.println(q.get());
System.out.println(x);
What is output by the following code?
8/10/2019 16 queues_JAVA
16/55
27/16
Linked list implementation
JAVA declaration
class QueueNode {Object data;QueueNode next;QueueNode(Object _d) {
data = _d;next = null;
}... ...}
Variables are usedwith package access,
just for this teaching
session, normally
these variableswould have private
access
8/10/2019 16 queues_JAVA
17/55
27/17
Linked list implementation
public class Queue {private QueueNode front = null;
private QueueNode back = null;public void insert(Object new_data) {... ...}public Object get() {... ... }public boolean isEmpty() {... ...}
}
8/10/2019 16 queues_JAVA
18/55
27/18
Linked list implementation
As with StackNodeand Stack, QueueNode
may be defined as an inner class within the
class Queue.
8/10/2019 16 queues_JAVA
19/55
27/19
Linked list implementation
front
45 51 .. 13
back
8/10/2019 16 queues_JAVA
20/55
27/20
Linked list implementation - get
front
45 51 .. 13
backObject result = front.data
result
45
8/10/2019 16 queues_JAVA
21/55
27/21
Linked list implementation - get
front
45 51 .. 13
backfront = front.next
result
45
8/10/2019 16 queues_JAVA
22/55
27/22
Linked list implementation - get
front
51 .. 13
back
result
45
8/10/2019 16 queues_JAVA
23/55
27/23
Get from a singleton queue
front back
8
8/10/2019 16 queues_JAVA
24/55
27/24
Get from a singleton queue
front back
result = front.data
result8
8
8/10/2019 16 queues_JAVA
25/55
27/25
Get from a singleton queue
front / back
front = front .next
result8
8
8/10/2019 16 queues_JAVA
26/55
27/26
Get from a singleton queue
front / back
??
result8
8/10/2019 16 queues_JAVA
27/55
27/27
Special case
front = front.next;
if (front == null)back = null;
8/10/2019 16 queues_JAVA
28/55
27/28
Linked list implementation -
insert
.. 131851
front back
8/10/2019 16 queues_JAVA
29/55
27/29
Linked list implementation -
insert
.. 131851
front back
7
temp
8/10/2019 16 queues_JAVA
30/55
27/30
Linked list implementation -
insert
.. 131851
front back
7
temp
back.next = temp;
8/10/2019 16 queues_JAVA
31/55
27/31
Linked list implementation -
insert
.. 131851
front back
7
temp
back = temp;
8/10/2019 16 queues_JAVA
32/55
27/32
Insert to an empty queue
/front /back
8/10/2019 16 queues_JAVA
33/55
27/33
Insert to an empty queue
/front /back15
temp
8/10/2019 16 queues_JAVA
34/55
27/34
Insert to an empty queue
/front /back15
temp
back.next = temp ??
8/10/2019 16 queues_JAVA
35/55
27/35
Insert to an empty queue
/front /back15
temp
front still null!!
8/10/2019 16 queues_JAVA
36/55
27/36
Special case
if (front == null) {front = temp;back = temp;
}else
back.next = temp;
8/10/2019 16 queues_JAVA
37/55
27/37
Linked list implementation
public void insert (Object new_data) {QueueNode temp = new QueueNode(new_data);
if (isEmpty())front = temp;else
back.next = temp;
back = temp;}
insert
8/10/2019 16 queues_JAVA
38/55
27/38
Linked list implementation
public Object get () {if (isEmpty() ) { error }
Object result = front.data;
front = front.next;if (front == null)
back = null;
return result;}
get
8/10/2019 16 queues_JAVA
39/55
27/39
Class exercise
public boolean isEmpty () {
.}
Write code for isEmpty
8/10/2019 16 queues_JAVA
40/55
27/40
Array implementation of a queue
0 1 2 3 4 5
front
back
0
3
q6 51 3
8/10/2019 16 queues_JAVA
41/55
27/41
Insertion
0 1 2 3 4 5
front
back
0
3
q
To insert a new cell:
q[back] = new_cell;
back++;
6 51 3
8/10/2019 16 queues_JAVA
42/55
27/42
Insertion
0 1 2 3 4 5
front
back
0
3
q
To insert a new cell:
q[back] = new_cell;
back++;
6 51 3 9
8/10/2019 16 queues_JAVA
43/55
27/43
Insertion
0 1 2 3 4 5
front
back
0
4
q
To insert a new cell:
q[back] = new_cell;
back++;
6 51 3 9
8/10/2019 16 queues_JAVA
44/55
27/44
Getting an item from the queue
0 1 2 3 4 5
front
back
0
4
q
result = q[front];
front++;
6 51 3 9
8/10/2019 16 queues_JAVA
45/55
27/45
Getting an item from the queue
0 1 2 3 4 5
front
back
0
4
q 6
result = q[front];
front++;
51 3 9
result 66
8/10/2019 16 queues_JAVA
46/55
27/46
Getting an item from the queue
0 1 2 3 4 5
front
back
1
4
q
result = q[front];
front++;
51 3 9
8/10/2019 16 queues_JAVA
47/55
27/47
Problem: insert 34 below
0 1 2 3 4 5
front
back
2
6
q 3 9 14 23
8/10/2019 16 queues_JAVA
48/55
27/48
Solution: wrap-around list
0 1 2 3 4 5
front
back
2
1
q 3 9 14 2334
8/10/2019 16 queues_JAVA
49/55
27/49
Solution: wrap-around list
0 1 2 3 4 5
front
back
2
1
q 3 9 14 2334
if(back == 6) back = 0;
q[back] = new_cell;back++;
8/10/2019 16 queues_JAVA
50/55
27/50
JAVA declaration & initialization
public class Queue {public final static int MAX_QUEUE_SIZE = 50;private Object[] storage;
private int front = -1;private int back = 0;
public Queue() {storage = new Object[MAX_QUEUE_SIZE]; }
public void insert(Object x) { ... ...}public Object get() {... ...}public boolean isEmpty() {... ...}public boolean isFull() {... ...}
}
8/10/2019 16 queues_JAVA
51/55
27/51
Queue operations
public void insert (Object x) {
if (isFull()) { ... queue full ... }storage[back] = x;
if(isEmpty())
front = back;
back = (back + 1) % MAX_QUEUE_SIZE;}
insert
8/10/2019 16 queues_JAVA
52/55
27/52
Queue operations
public Object get () {if (isEmpty()) { ... queue empty ... }
Object result = storage[front];front = (front + 1) % MAX_QUEUE_SIZE;
if (front == back) {front = -1;
back = 0;}return result;
}
get
8/10/2019 16 queues_JAVA
53/55
27/53
Queue operations
isEmpty
public boolean isEmpty () {
return front == -1;}
8/10/2019 16 queues_JAVA
54/55
27/54
Queue operations
isFull
public boolean isFull () {return back == front;
}
8/10/2019 16 queues_JAVA
55/55
Use of the Queue ADT
Simulations of real-life queues (e.g., banks,
supermarkets, traffic lights, etc)
Program design independent of
implementation