View
216
Download
0
Category
Preview:
Citation preview
QueuesThis picture shows 12 trams, each with a carrying capacity of well over 100 people when 'crush loaded'.
Definition: A queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. rear: Objects are inserted here front: Objects are removed from here Two fundamental operations: enqueue: Insert an object to the container. dequeue: Remove an object from the container.
… …
front rear
dequeue enqueue
T h e e n q u e u e o p e r a t i o n :
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
T h e d e q u e u e o p e r a t i o n :
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
F r o n t R e a rI n t h e q u e u e t h e l o n g e s t
Comparison with a stack:
1. A queue is also a container of objects 2. In a stack, objects are inserted and removed according to
the LIFO principle. 3. In a queue, objects are inserted and removed according to
the FIFO principle. 4. A stack has a top. 5. A queue has a rear and a front. 6. Both stacks and queues have two fundamental operations
for inserting and removing objects.
Message queues in an operating system There are times that programs need to communicate with eachother. Unix operating system provides message queue as oneof the mechanisms to facilitate communication.
Program #1
Program #2front
rear
Send a message to the queue
Take a message fromthe queue
Printing in a computer network is organized as print jobs. Prnt jobs are placed in a queue before they are sent to a printer.
Printerfront
rear
Computer 3
Computer 1
Computer 2
Print jobs
The Queue Abstract Data Type
A queue Q is an abstract data type that supports the following two fundamental methods: enqueue(o): Insert object o at the rear of the queue
Input: Object; Output: None. dequeue( ): Remove and return from the queue the object at the
front; an error occurs if the queue is empty. Input: None; Output: Object
Other supporting methods:
size(): Return the number of objects in the queue. Input: None; Output: Integer
isEmpty(): Return a Boolean indicating if the queue is empty. Input: None; Output: Boolean
front(): Return, but do not remove the front object in the queue; an error occurs if the queue is empty. Input: None; Output: Object
This table shows a series of queue operations and their effects.The queue is empty initially.
Operation Output front<- Q<- rear
enqueue(5) - (5) enqueue(3) - (5,3) dequeue() 5 (3) enqueue(7) - (3,7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) - (9) enqueue(7) - (9,7) size() 2 (9,7) enqueue(3) - (9,7,3) enqueue(5) - (9,7,3,5) dequeue() 9 (7,3,5)
55 3
3
Operation Output front Q rear
enqueue(5) - (5) enqueue(3) - (5,3) dequeue() 5 (3) enqueue(7) - (3,7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) - (9) enqueue(7) - (9,7) size() 2 (9,7) enqueue(3) - (9,7,3) enqueue(5) - (9,7,3,5) dequeue() 9 (7,3,5)
A Queue Interface in Java
public interface Queue { public void enqueue( Object element );
throws QueueFullException; public Object dequeue() throws QueueEmptyException; public int size(); public boolean isEmpty(); public Object front() throws QueueEmptyException; } When we define an interface, we just indicate that a class which implements it should provide all the methods specified in it.
A Simple Array-Based Implementation
To implement a queue with an array, we need: 1. An array of the size N2. An index f for the front element3. An index r for next empty slot or cell
Q:
…
0 1 2
r N - 1f
As objects are enqueued and dequeued, the queue moves alongin the array. For example:
f rAfter enqueue:
f rAfter dequeue:
f r
W h e n t h e q u e u e r e a c h e s t h e e n d o f t h e a r r a y , i t “ w r a p s a r o u n d ” a n d t h e r e a r o f t h e q u e u e s t a r t s f r o m i n d e x 0 . T h e f i g u r e b e l o w d e m o n s t r a t e s t h e s i t u a t i o n . Q : 0 1 N - 1
. . .f
2
r
H o w t o c a l c u l a t e r a n d f w h e n t h e q u e u e w r a p s a r o u n d ?
When we increment f or r, we compute the result as(f + 1) mod N or (r + 1) mod N .In Java, the modulo operator is % (remainder operator). For example, if r = N - 1, then (r + 1) = N , therefore,(r + 1) mod N = 0 The value of r wraps around from N to 0.
Q:
…
0 1 2
r f
N - 1
Initially, we assign r = f = 0, indicating that the queue is empty.During the process, we may meet a situation where
r = f = i (0 < i < N),which also indicates the queue is empty.Now we assume that we enqueue N objects into Q withoutdequeueing any of them:
Q:
…
0 1 2
rf
N - 1
Then, we have r = f = 0. But in this case, we have a full queue.In general, when r = f = i (0 < i < N), it is possible that we havea full queue.
Problem: r = f may indicate that the queue is empty or that thequeue is full.
Solution: when |r – f| = N – 1, report that the queue is full.
Algorithms: size():
return the number (N - f + r) mod N
Qfr
0 1 2 N-1 N+r-1
If r f, then r - f 0 N + (r - f ) N
(N - f + r) mod N = - f + r = r - f
If r < f, then f - r > 0 N - f + r = N – (f - r ) < N
(N - f + r) mod N = N - f + r
Algorithms: front( ):
if the queue is emptythrow a QueueEmptyException
else return element Q[f]
Q:
…
0 1 2
r N - 1f
enqueue(o):if queue size is N - 1
throw a QueueFullExceptionelse
store the object to Q[r]assign (r + 1) mod N to r
Q:
…
0 1 2
r N - 1f
(r + 1) mod N
dequeue( ):if queue size is empty
throw a QueueEmptyExceptionelse
save the element Q[f] to a variable temp make the element Q[f] a null objectassign (f + 1) mod N to f
Q:
…
0 1 2
r N - 1f
(f + 1) mod N
Memory Allocation in JavaR e c a l l th a t J a v a p ro g ra m s u s e s ta c k fo r fu n c t io n a rg u m e n ts a n d lo c a l v a r ia b le s . T h e J a v a v ir tu a l m a c h in e p ro v id e s a n o th e r a re a o f s to ra g e fo r J a v a p ro g ra m s . E a c h J a v a p ro g ra m h a s a m e m o ry h e a p . A ty p ic a l m e m o ry s p a c e fo r a J a v a p ro g ra m is s h o w n h e re .
B y te c o d e
G r o w sin toh ig h e rm e m o r y
H e a pF r e em e m o r y
F ix e d s iz e
S ta c k
G r o w sin tolo w e rm e m o r y
In Java, when a new object is created, the Java virtual machine allocates a memory block for the object from the heap. For simplicity, assume that blocks have a fixed size. The Java virtual machine places all available memory blocks in a queue. When the Java program needs a memory block, the Java virtual machine dequeues a memory block from the queue. When the Java virtual machine determines that a memory block is no longer in use, it places the block in the queue (garbage collection).
T h e m e m o r y b l o c k q u e u e :
B l o c k 1B l o c k 2 B l o c k 4B l o c k 3
F r o n t R e a r
W h e n a n e w o b j e c t i s c r e a t e d , f o r e x a m p l e , a n e w 1 2 - e l e m e n t
V e c t o r o b j e c t i s g e n e r a t e d :
V e c t o r i t e m s = n e w V e c t o r ( 1 2 ) ;
A b l o c k o f M e m o r y - b l o c k # 3 i s a l l o c a t e d f o r t h e v a r i a b l e i t e m s a s s h o w n b e l o w .
B l o c k 1B l o c k 2 B l o c k 4
B l o c k 3
F r o n t R e a r
i t e m s
Recommended