Download f - Piazza

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Presentation for use with the textbook Data Structures and
Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
and M. H. Goldwasser, Wiley, 2014
Queues
© 2014 Goodrich, Tamassia, Goldwasser
Queues
1
The Queue ADT




The Queue ADT stores arbitrary  Auxiliary queue
objects
operations:
Insertions and deletions follow
 object first(): returns the
element at the front without
the first-in first-out scheme
removing it
Insertions are at the rear of the
 integer size(): returns the
queue and removals are at the
number of elements stored
front of the queue
 boolean isEmpty(): indicates
Main queue operations:
whether no elements are
 enqueue(object): inserts an
stored

element at the end of the

queue
object dequeue(): removes and
returns the element at the front
of the queue
© 2014 Goodrich, Tamassia, Goldwasser
Queues
Boundary cases:

Attempting the execution of
dequeue or first on an
empty queue returns null
2
Example
Operation
enqueue(5)
enqueue(3)
dequeue()
enqueue(7)
dequeue()
first()
dequeue()
dequeue()
isEmpty()
enqueue(9)
enqueue(7)
size()
enqueue(3)
enqueue(5)
dequeue()
© 2014 Goodrich, Tamassia, Goldwasser
–
–
5
–
3
7
7
null
true
–
–
2
–
–
9
Output Q
(5)
(5, 3)
(3)
(3, 7)
(7)
(7)
()
()
()
(9)
(9, 7)
(9, 7)
(9, 7, 3)
(9, 7, 3, 5)
(7, 3, 5)
Queues
3
Applications of Queues

Direct applications




Waiting lists, bureaucracy
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications


Auxiliary data structure for algorithms
Component of other data structures
© 2014 Goodrich, Tamassia, Goldwasser
Queues
4
Queue Interface in Java


Java interface
corresponding to
our Queue ADT
Assumes that
first() and
dequeue() return
null if queue is
empty
© 2014 Goodrich, Tamassia, Goldwasser
public interface Queue<E> {
int size();
boolean isEmpty();
E first();
void enqueue(E e);
E dequeue();
}
Queues
5
Array-based Queue


Use an array of size N in a circular fashion
Two variables keep track of the front and size
f index of the front element
sz number of stored elements

When the queue has fewer than N elements, array
location r = (f + sz) mod N is the first empty slot
past the rear of the queue
normal configuration
Q
0 1 2
f
r
wrapped-around configuration
Q
0 1 2
r
© 2014 Goodrich, Tamassia, Goldwasser
Queues
f
6
Queue Operations

We use the
modulo operator
(remainder of
division)
Algorithm size()
return sz
Algorithm isEmpty()
return (sz == 0)
Q
0 1 2
f
0 1 2
r
r
Q
© 2014 Goodrich, Tamassia, Goldwasser
f
Queues
7
Queue Operations (cont.)


Operation enqueue
throws an exception if
the array is full
This exception is
implementationdependent
Algorithm enqueue(o)
if size() = N then
throw IllegalStateException
else
r  (f + sz) mod N
Q[r]  o
sz  (sz + 1)
Q
0 1 2
f
0 1 2
r
r
Q
© 2014 Goodrich, Tamassia, Goldwasser
f
Queues
8
Queue Operations (cont.)

Note that operation
dequeue returns null
if the queue is empty
Algorithm dequeue()
if isEmpty() then
return null
else
o  Q[f]
f  (f + 1) mod N
sz  (sz - 1)
return o
Q
0 1 2
f
0 1 2
r
r
Q
© 2014 Goodrich, Tamassia, Goldwasser
f
Queues
9
Array-based Implementation
© 2014 Goodrich, Tamassia, Goldwasser
Queues
10
Array-based Implementation (2)
© 2014 Goodrich, Tamassia, Goldwasser
Queues
11

All operations O(1)
© 2014 Goodrich, Tamassia, Goldwasser
Queues
12
Implementing a Queue with a Linked List
public class LinkedQueue<E> implements Queue<E> {
/** The primary storage for elements of the stack */
private SinglyLinkedList<E> list = new
SinglyLinkedList<>(); // an empty list
/** Constructs an initially empty stack. */
public LinkedQueue() { }
.
.
© 2014 Goodrich, Tamassia, Goldwasser
Stacks
13
Implementing a Queue with a Linked List
public int size() { return list.size(); }
public boolean isEmpty() { return list.isEmpty(); }
public void enqueue(E element) {
list.addLast(element);
}
public E first() { return list.first(); }
public E dequeue() { return list.removeFirst(); }
public String toString() {
return list.toString();
}
}
All operations O(1)
© 2014 Goodrich, Tamassia, Goldwasser
Stacks
14
Comparison to java.util.Queue
interface

Our Queue methods and corresponding
methods of java.util.Queue:
© 2014 Goodrich, Tamassia, Goldwasser
Queues
15
Application: Round Robin Schedulers
We can implement a round robin scheduler using a
queue Q by repeatedly performing the following
steps:

1.
2.
3.
e = Q.dequeue()
Service element e
Q.enqueue(e)
Queue
Dequeue
Enqueue
Shared
Service
© 2014 Goodrich, Tamassia, Goldwasser
Queues
16
Can we do this more efficiently?
© 2014 Goodrich, Tamassia, Goldwasser
Queues
17
Can we do this more efficiently?

Add a rotate method that just detachs
the first node and attaches it to the rear

Without creating a new node
© 2014 Goodrich, Tamassia, Goldwasser
Queues
18
Related documents