CS210- Lecture 5 Jun 9, 2005  Agenda

advertisement
CS210- Lecture 5
Jun 9, 2005
 Agenda
 Queues
 Array Based Implementation of Queues
 Applications of Queues
 Linked Lists
6/30/2016
CS210-Summer 2005, Lecture 5
1
Queues



Queue is a container of objects that are
inserted and removed according to the
first-in first-out (FIFO) principle.
Objects can be inserted into a queue at
any time, but only the objects that has
been in the queue the longest can be
removed.
Objects enter the queue at the rear and
are removed from the front.
6/30/2016
CS210-Summer 2005, Lecture 5
2
Queue ADT

Queue ADT supports following
methods:

enqueue(o): Insert object o at the rear of
the queue.



Input: Object o
Output: None
dequeue(): Remove and return from the
queue the object at the front. Error occurs
if the queue is empty.


6/30/2016
Input: None
Output: Front object
CS210-Summer 2005, Lecture 5
3
Queue ADT

size(): Return the number of objects in
the queue



Input: none
Output: Integer (total number of objects)
isEmpty(): Return a boolean indicating if
the queue is empty.


6/30/2016
Input: None
Output: boolean (true if queue is empty, false
otherwise)
CS210-Summer 2005, Lecture 5
4
Queue ADT

front(): Return but do not remove, the
front object in the queue. Error occurs if
the queue is empty.



Input: None
Output: Front object
Exceptions
Attempting the execution of dequeue or
front on an empty queue throws an
EmptyQueueException.
6/30/2016
CS210-Summer 2005, Lecture 5
5
Example
Operation
Output
Q
enqueue(5)
enqueue(3)
dequeue()
enqueue(7)
dequeue()
front()
dequeue()
dequeue()
isEmpty()
enqueue(9)
enqueue(7)
size()
enqueue(3)
enqueue(5)
dequeue()
–
–
5
–
3
7
7
“error”
true
–
–
2
–
–
9
(5)
(5, 3)
(3)
(3, 7)
(7)
(7)
()
()
()
(9)
(9, 7)
(9, 7)
(9, 7, 3)
(9, 7, 3, 5)
(7, 3, 5)
6/30/2016
CS210-Summer 2005, Lecture 5
6
Applications of Queues

Waiting lists: Stores, Theaters etc.

Access to Shared resources (printer).

Multiprogramming
6/30/2016
CS210-Summer 2005, Lecture 5
7
Queue Interface



Java interface
corresponding to
our Queue ADT
Requires the
definition of class
EmptyQueueException
No corresponding
built-in Java class
public interface Queue {
public int size();
public boolean isEmpty();
public Object front()
throws EmptyQueueException;
public void enqueue(Object o);
public Object dequeue()
throws EmptyQueueException;
}
6/30/2016
CS210-Summer 2005, Lecture 5
8
A Simple Array based implementation


We must decide how we are going to
keep track of the rare and front of the
queue.
One possibility is to adapt the approach
we used in stack. Q[0] be the front. Not
efficient as need to move all elements
forward one array cell each time we
perform a dequeue operation. Dequeue
operation will take O(n) time.
6/30/2016
CS210-Summer 2005, Lecture 5
9
A Simple Array based implementation

Two variables keep track of the front
and rear





f
r
index of the front element
index immediately past the rear
element
Array location r is kept empty.
Initially f = r = 0 (initially empty)
When we remove an element from
front of the queue, we increment f to
index the next cell.
6/30/2016
CS210-Summer 2005, Lecture 5
10
A Simple Array based implementation



When we add an element, we store it in
cell Q[r] and increment r to index the
next available cell in Q.
This allows us to implement all methods
in O(1) time.
There is still a problem with this
approach.
6/30/2016
CS210-Summer 2005, Lecture 5
11
A Simple Array based implementation
Q
f, r
Q
4
enqueue(4)
f r
dequeue(4)
Q
f, r
Q
4
enqueue(4)
f r
If we do enqueue(4) and dequeue(4) 7 times. We would have f = r =
7. Next time we will get ArrayOutOfBoundsException.
6/30/2016
CS210-Summer 2005, Lecture 5
12
A Simple Array based implementation


Solution: Use an array of size N in a
circular fashion
We let the f and r indices wrap
around the end of Q.
normal configuration
Q
0 1 2
f
r
wrapped-around configuration
Q
0 1 2
6/30/2016
r
f
CS210-Summer 2005, Lecture 5
13
A Simple Array based implementation

We compute the increment of f and r as



(f + 1) mod N
(r + 1) mod N
Another problem:

If we enqueue N objects into Q without
dequeuing any of them. We would have
f = r, which is the same condition that
occurs when the queue is empty. No
difference between full queue and empty
queue.
6/30/2016
CS210-Summer 2005, Lecture 5
14
A Simple Array based implementation


Solution: Insist that Q can never hold more
than N – 1 objects.
Size ?


(N – f + r) mod N.
When list is empty



f=r=0
Size = N mod N = 0
When list is full i.e N – 1 elements



6/30/2016
f=0
r=N–1
Size = (N – 0 + N – 1) mod N = N - 1
CS210-Summer 2005, Lecture 5
15
Queue Operations

We use the
modulo
operator
(remainder of
division)
Algorithm size()
return (N - f + r) mod N
Algorithm isEmpty()
return (f = r)
Q
0 1 2
f
0 1 2
r
r
Q
6/30/2016
f
CS210-Summer 2005, Lecture 5
16
Queue Operations


Operation
enqueue throws
an exception if
the array is full
This exception is
implementationdependent Q
Algorithm enqueue(o)
if size() = N - 1 then
throw FullQueueException
else
Q[r]  o
r  (r + 1) mod N
0 1 2
f
0 1 2
r
r
Q
6/30/2016
CS210-Summer 2005, Lecture 5
f
17
Queue Operations


Operation
dequeue throws
an exception if
the queue is
empty
This exception is
specified in the
queue ADT
Algorithm dequeue()
if isEmpty() then
throw EmptyQueueException
else
o  Q[f]
f  (f + 1) mod N
return o
Q
0 1 2
f
0 1 2
r
r
Q
6/30/2016
CS210-Summer 2005, Lecture 5
f
18
Analyzing the Array Based Queue Implementation
6/30/2016
Method
Time
size
O(1)
isEmpty
O(1)
front
O(1)
enqueue
O(1)
dequeue
O(1)
CS210-Summer 2005, Lecture 5
19
Drawbacks of Array based implementation
 Array based implementation must
assume a fixed upper bound N on the
ultimate size of the stack.
 An application may actually need more
or less queue capacity than this.
 Still in cases where we have a good
estimate on the number of items to be
stored in the queue, the array based
implementation is hard to beat.
6/30/2016
CS210-Summer 2005, Lecture 5
20
Round Robin Schedulers

We can implement a round robin scheduler using a
queue, Q, by repeatedly performing the following
steps:

e = Q.dequeue()

Service element e

Q.enqueue(e)
The Queue
1 . Deque the
2 . Service the
next element
next element
3 . Enqueue the
serviced element
Shared
Service
6/30/2016
CS210-Summer 2005, Lecture 5
21
The “Pass the Pillow” game



A group of n children sit in a circle passing a
pillow around the circle.
Children continue passing the pillow until a
leader rings a bell, at which point the child
holding the pillow must leave the game after
handling the pillow to the next child in the
circle.
The process is then continued until there is
only one child remaining, who is declared the
winner. If leader always rings the bell after
the pillow has been passed k times, then
determining the winner for a given list of
children in known as “Josephus Problem”.
6/30/2016
CS210-Summer 2005, Lecture 5
22
Singly Linked Lists


A singly linked list is a
concrete data structure
consisting of a sequence
of nodes
Each node stores

head

next
element
link to the next node
node
elem

A
6/30/2016
B
C
CS210-Summer 2005, Lecture 5
D
23
Singly Linked Lists




The next reference inside a node can be
viewed as a link or pointer to another
node.
Moving from one node to another by
following a next reference is known as
link hopping or pointer hopping.
The first and last node are usually
called head and tail of the list resp.
Tail has a null next reference which
indicates the termination of the list.
6/30/2016
CS210-Summer 2005, Lecture 5
24
Singly linked list vs. Arrays


Like an array, a singly linked list keeps
the elements in a certain linear order,
which is determined by the chain of
next links between the nodes.
Unlike an array, a singly linked list does
not have a predetermined fixed size,
and uses space proportional to the
number of its elements.
6/30/2016
CS210-Summer 2005, Lecture 5
25
Inserting at the head
6/30/2016
CS210-Summer 2005, Lecture 5
26
Inserting at the head
1.
2.
3.
4.
Allocate a new node
Insert new element
Have new node point to old head
Update head to point to new node
6/30/2016
CS210-Summer 2005, Lecture 5
27
Removing at the head


6/30/2016
Update head to
point to next
node in the list
Allow garbage
collector to
reclaim the
former first node
CS210-Summer 2005, Lecture 5
28
Inserting at the tail
6/30/2016
CS210-Summer 2005, Lecture 5
29
Inserting at the tail
1.
2.
3.
4.
5.
Allocate a new node
Insert new element
Have new node point to null
Have old last node point to new node
Update tail to point to new node
6/30/2016
CS210-Summer 2005, Lecture 5
30
Removing at tail


Removing at the tail
of a singly linked list
is not efficient!
There is no
constant-time way
to update the tail to
point to the previous
node.
6/30/2016
CS210-Summer 2005, Lecture 5
31
Analyzing the Singly Linked List
Method
6/30/2016
Time
Inserting at the head
O(1)
Inserting at the tail
O(1)
Deleting at the head
O(1)
Deleting at the tail
O(n)
CS210-Summer 2005, Lecture 5
32
Download