Vous êtes sur la page 1sur 55

27/1

Queues
Reading: Lewis and Loftus, JAVA:
Software Solutions, (3
rd
ed), Chapter 12.2
Savitch Chapter 10.2

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


27/3
Queues
27/4
What is a queue?
27/5
What is a queue?
27/6
What is a queue?
27/7
What is a queue?
27/8
What is a queue?
27/9
What is a queue?
27/10
What is a queue?
27/11
What is a queue?
27/12
What is a queue?
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)
27/14
Queue Operations
insert to add an element at the rear of the
queue
get to get the value and delete the element
at the front of the queue
isEmpty to return true if the queue has no
element
isFull to return true if the queue is full
(array implementation)
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?

27/16
Linked list implementation
JAVA declaration

class QueueNode {
Object data;
QueueNode next;
QueueNode(Object _d) {
data = _d;
next = null;
}
... ...
}

Variables are used
with package access,
just for this teaching
session, normally
these variables
would have private
access
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() {... ...}
}
27/18
Linked list implementation
As with StackNode and Stack, QueueNode
may be defined as an inner class within the
class Queue.
27/19
Linked list implementation
front
45 51
.. 13
back
27/20
Linked list implementation - get
front
45 51
.. 13
back
Object result = front.data
result
45
27/21
Linked list implementation - get
front
45 51
.. 13
back
front = front.next
result
45
27/22
Linked list implementation - get
front
51
.. 13
back
result
45
27/23
Get from a singleton queue
front back

8
27/24
Get from a singleton queue
front back
result = front.data
result
8
8
27/25
Get from a singleton queue
front / back
front = front .next
result
8
8
27/26
Get from a singleton queue
front / back
??
result
8
27/27
Special case
front = front.next;

if (front == null)
back = null;

27/28
Linked list implementation -
insert
.. 13
18 51
front back

27/29
Linked list implementation -
insert
.. 13
18 51
front back
7
temp
27/30
Linked list implementation -
insert
.. 13
18 51
front back
7
temp
back.next = temp;
27/31
Linked list implementation -
insert
.. 13
18 51
front back
7
temp
back = temp;
27/32
Insert to an empty queue
/ front / back
27/33
Insert to an empty queue
/ front / back 15
temp
27/34
Insert to an empty queue
/ front / back 15
temp
back.next = temp ??
27/35
Insert to an empty queue
/ front / back 15
temp
front still null!!
27/36
Special case
if (front == null) {
front = temp;
back = temp;
}
else
back.next = temp;

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
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
27/39
Class exercise
public boolean isEmpty () {
.
}
Write code for isEmpty
27/40
Array implementation of a queue
0 1 2 3 4 5
front
back
0
3
q
6 51 3
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
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
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
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
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 6 6
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
27/47
Problem: insert 34 below
0 1 2 3 4 5
front
back
2
6
q
3 9 14 23
27/48
Solution: wrap-around list
0 1 2 3 4 5
front
back
2
1
q
3 9 14 23 34
27/49
Solution: wrap-around list
0 1 2 3 4 5
front
back
2
1
q
3 9 14 23 34
if (back == 6) back = 0;
q[back] = new_cell;
back++;
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() {... ...}
}
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
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
27/53
Queue operations
isEmpty

public boolean isEmpty () {
return front == -1;
}
27/54
Queue operations
isFull

public boolean isFull () {
return back == front;
}
27/55
Use of the Queue ADT

Simulations of real-life queues (e.g., banks,
supermarkets, traffic lights, etc)
Program design independent of
implementation

Vous aimerez peut-être aussi