1 / 19

Queue

Queue. Queue. ADT Queue. Types : Object, Boolean, Integer Functions Create: ( )  Queue Dissolve: Queue  ( ) enqueue: Queue, Object  Queue dequeue: Queue -/-> (Queue, Object) front: Queue -/-> Object isEmpty: Queue  Boolean size: Queue  Integer. Queue.

katina
Download Presentation

Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queue

  2. Queue ADT Queue Types: Object, Boolean, Integer Functions Create: ( )  Queue Dissolve: Queue  ( ) enqueue: Queue, Object  Queue dequeue: Queue -/-> (Queue, Object) front: Queue -/-> Object isEmpty: Queue  Boolean size: Queue  Integer

  3. Queue ADT Queue (cont.) Preconditions for all q Queue; x  Object pre-dequeue(q, x) ::= !isEmpty(q) pre-front(q) ::= !isEmpty(q) Post-conditions for all q, q’ Queue; x  Object post-Create( ; q’) ::= isEmpty(q’) post-enqueue(q, x; q’) ::= !isEmpty(q’) && (size(q’) == size(q)+1) && if (!isEmpty(q) then front(q’) == front(q)) else front(q’)==x post-dequeue(q; (q’, x)) ::=(size(q’) == size(q) – 1) && (front(q) == x) && front(q’) != front(q)

  4. Queue Implementation using Composition Use a LinkedList with insertions at the back and removals at the front publicclass Queue { LinkedList L; public Queue( ) { //precondition: none //post-condition: Queue is empty L = new LinkedList( ); } public void enqueue (Object x ) { //precondition: none //post-condition: Queue is not empty; size is increased by 1 L.add(x); }

  5. Must also create this class that extends Exception Queue Implementation using Composition (cont.) public Object dequeue ( ) throwsQueueEmptyException{ //precondition: Queue not empty //post-condition: size decreased by 1, front object removed Object myObj = null; if (!L.isEmpty( ) myObj = L.remove(0); elsethrownew QueueEmptyException( ); return myObj; } public Object front( ) throws QueueEmptyException { //precondition: Queue not empty //post-condition: Queue unchanged, copy of front object returned Object myObj = null; if (!L.isEmpty( ) myObj = L.get(0); elsethrownew QueueEmptyException( ); return myObj; }

  6. Queue Implementation using composition (cont.) publicboolean isEmpty( ) { //precondition: none //postcondition: Queue unchanged, boolean result returned return L.isEmpty( ); } public int size( ) { //precondition: none //postcondition: Queue unchanged, number of objects contained is returned return L.isEmpty( ); } } // end of class description In this implementation, class Queue serves as an “adapter” – a class that provides an interface to the client for calling certain methods from the class LinkedList.

  7. Queue Using an array to implement class Queue An array implementation of a Queue can be very inefficient if every time an object is removed from the front of the queue, all of the remaining elements have to be moved forward by one position. front back q.dequeue( ) Now all of the remaining objects must be moved one position to the left.

  8. Queue Array implementation of a Queue Implementing a Queue using an array or using composition with an ArrayList is inefficient. Enqueue( ) operations are performed by adding an object to the end of the list and can be performed in “constant” time – independent of the number of items queues. However, dequeue( ) operations remove the first object in the array and cause or require all of the remaining N objects to be moved one position to the left. The cost of a dequeue( ) depends upon N – the size of the queue These problems can be alleviated by using a circular array – an array in which the location of the front and back index are allowed to change and the contents to wrap around when the last position in the buffer is filled.

  9. 0 1 2 3 4 5 6 7 8 9 2 3 1 4 back 0 front 5 9 6 8 7 Queue Queue implemented by a circular array The Queue maintains two indices, front and back, that initially are both 0 To construct a queue using an array of length 10 …. Imagine that the two ends are joined together to form a circle

  10. 2 3 back 1 4 back 0 front 5 9 6 8 7 Queue Queue implemented using circular array Add an object x to the queue q --- q.enqueue(x) if(q.size( ) != buffer.length) { buffer[back] = x; back = (back + 1) % capacity; } x buffer

  11. 2 3 front 1 4 back 0 front 5 9 back 6 8 7 Queue Queue implemented as a circular array Assume that X denotes the indices in the buffer that hold an object. Now remove an object from the front of the queue – q.dequeue( ) if (!q.isEmpty( )) { myObj = buffer[front]; front = (front + 1) % capacity; } x x x x When the front index is incremented, the object at this location is effectively removed from the queue. It can no longer be accessed and will be overwritten when the back of the queue next reaches this point. x buffer

  12. front X X 2 3 X X 1 back 4 X X 0 X X X 5 9 6 8 7 Queue Inserting into almost full queue if ((back + 1) % capacity == front) resize( ); //the queue is full

  13. Queue Queue implemented as a circular array In the circular array implementation outlined on the previous slides, if the capacity of the queue is fixed, a method isFull( ) would need to be added to the ADT for a fixed-length queue. Alternatively, a private method resize( ) could be added to the implementation that grows a larger buffer before an enqueue( ) when the size is equal to the capacity. In a circular array implementation, “wrap around” is used to confine a “moving queue” – location of the front is not fixed – within a fixed unit of memory.

  14. Queue Resizing the buffer privatevoid resize( ) { Object [ ] newBuffer = new Object[2 * capacity]; int i = front, j = 0; while (i != back) { newBuffer[j++] = buffer[i]; i = (i + 1) % capacity; } capacity *= 2; front = 0; back = j; buffer = newBuffer; } Restart the circular array at front = 0 when you resize.

  15. Queue Putting it all together public class Queue<E> { private E[ ] elements; private int capacity, front, back; public Queue (int capacity) { this.capacity = capacity; elements = (E[ ]) new Object[capacity]; front = back = 0; size = 0; } private void resize( ) { E[ ] newElements = (E[ ]) new Object[2*capacity]; int i = front, j = 0; while (i != back) { newElements[j++] = elements[i]; i = (i + 1) % capacity; } elements = newElements; capacity *= 2; } continued

  16. Queue public void enqueue(E item) { if (size >= capacity) resize( ); elements[back] = item; back = (back + 1) % capacity; size++; } public E dequeue( ) { if (size == 0) throw new IllegalStateException( ); E temp = elements[front]; front = (front + 1) % capacity; size--; return temp; } }

  17. Queue Variety of implementations of a Queue • Using composition with class LinkedList from the java.util library package. Efficient – both enqueue( ) and dequeue( ) are “constant” in their cost – time to execute – independent of the number of items queued, N. • Using its own, privately maintained linked list. Again the cost of enqueue( ) and dequeue( ) operations are independent of N, and this time the overhead of a second method call is eliminated. The only disadvantage is that the programmer has to supply the list operations from scratch. • Using a buffer implemented as a circular array. Operations enqueue( ) and dequeue( ) are of constant cost BUT the size of the buffer is fixed or a resize( ) method allows the queue to grow arbitrarily long but the cost of each resize is proportional to the value of N at the time. If the initial capacity is made very large to avoid having to exceed the initial capacity of the buffer, the efficiency in time is gained at the expense of wasted memory.

  18. Queue: q 0 listA listB listC 0 Queue Priority Queue Consider the case where there are 3 classes of traffic with class A traffic getting priority over class B and B over class C. Here is a sketch of one possible implementation. 0 if (!listA.isEmpty( )) {remove object from front of listA} else if (!listB.isEmpty( )) {remove object from front of listB} else {remove object from front of listC} Algorithm for method dequeue( ) Determine the priority class from the job or customer object and add the object to the end of the appropriate list. Algorithm for method enqueue( )

  19. Queue Applications Process control in Operating systems -- Queues are used to allocate shared resources (such as processor time) on a first-come-first-served basis. Priority queues are used to allow more urgent processes or more important messages to jump ahead of less urgent processes or messages that have been waiting longer. Simulation Queues are used to hold jobs or customers waiting for a service. A priority queue is used as an events list – where the priority is the next event to be selected is the one that is scheduled to occur most recently. Breadth-first search or traversal of a tree or graph A queue is used as an auxiliary data structure in a breadth-first search.

More Related