Presentation is loading. Please wait.

Presentation is loading. Please wait.

queues1 Queues Data structures that wait their turn.

Similar presentations


Presentation on theme: "queues1 Queues Data structures that wait their turn."— Presentation transcript:

1

2 queues1 Queues Data structures that wait their turn

3 queues2 Queue characteristics FIFO: first in, first out insertion of items occurs at one end, removal occurs at the other end first item inserted is the first item removed; second inserted is second removed, third is third, etc.

4 queues3 Queue characteristics Structure is very similar to stack, with much the same considerations -- still subject to overflow and underflow Unlike stack, queue is accessible at both ends Entry and removal still occur at one end -- but each operation occurs at a different end

5 Java’s Queue interface Unlike the Stack ADT, the Java API doesn’t provide a full implementation of a generic Queue The Queue interface specifies methods for working with a queue, most of which are listed on the next slide There are several API classes that implement the interface, but each of these adds methods not specified by the interface queues4

6 5 Queue ADT member methods Constructor(s) Modifiers –enqueue (insert item at back): add –dequeue (remove item at front): remove Observers –size –isEmpty

7 Queue implementations The API classes that implement the Queue interface are designed for more sophisticated uses than the simple interface implies We can implement a simple queue using either an array or a linked list as the basic structure queues6

8 7 Array implementation public class ArrayQueue implements Cloneable { private E[ ] data; private int manyItems; private int front; private int rear; …

9 queues8 Array implementation public ArrayQueue( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = (E[]) new Object[INITIAL_CAPACITY]; } // Since queue is empty, front and rear values // don’t matter

10 queues9 Array implementation public ArrayQueue(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException ("initialCapacity is negative: " + initialCapacity); manyItems = 0; data = (E[]) new Object[initialCapacity]; }

11 queues10 Array implementation public ArrayQueue clone( ) { ArrayQueue answer; try { answer = (ArrayQueue ) super.clone( ); } catch (CloneNotSupportedException e) { throw new RuntimeException ("This class does not implement Cloneable"); } answer.data = data.clone( ); return answer; }

12 queues11 Enqueue and dequeue – not as simple as they look! // first attempt at enqueue public void add (E item) { if (manyItems == 0) { front = 0; rear = 0; } rear++; data[rear]=item; manyItems++; }

13 queues12 dequeue (first attempt) public E remove( ) { E answer; if (manyItems == 0) throw new NoSuchElementException("Queue underflow"); answer = data[front]; front++; manyItems--; return answer; }

14 queues13 Consider a queue with a capacity of 3: As items are added, rear approaches capacity: As items are removed, front approaches back: Problems!!! Situation: queue isn’t full (manyItems = 0) but attempt to add an item will go beyond array boundary

15 queues14 Possible solution Maintain fixed front of queue: // dequeue method: answer = data[0]; for (int x=0; x<rear; x++) data[x]=data[x+1]; Increases complexity of algorithm considerably -- we’ve gone from O(1) operation in original function to O(N)

16 queues15 Better solution: circular array Let front continue to float, but add ability for rear to float as well When rear reaches index capacity-1, if queue isn’t full, rear=0 In effect, the successor of the last array index is the first array index -- array can be thought of as circular Can also grow array as necessary

17 queues16 Circular queue implementation Add helper function nextIndex as private method of queue class: private int nextIndex(int i) { if (++i == data.length) return 0; else return i; } Call method from enqueue and dequeue

18 queues17 New enqueue method public void add(E item) { if (manyItems == data.length) ensureCapacity(manyItems*2 + 1); if (manyItems == 0) { front = 0; rear = 0; } else rear = nextIndex(rear); data[rear] = item; manyItems++; }

19 queues18 New dequeue method public E remove( ) { E answer; if (manyItems == 0) throw new NoSuchElementException("Queue underflow"); answer = data[front]; front = nextIndex(front); manyItems--; return answer; }

20 Other methods Besides the queue-specific methods (and clone()), the ArrayQueue implementation includes a few other methods: –ensureCapacity –trimToSize –getCapacity queues19

21 queues20 Invariant for revised queue Number of items on queue stored in variable manyItems Items are stored in circular array from data[front] to data[rear] If queue is empty, manyItems == 0 and the values of front and rear are undefined

22 queues21 Queue as linked list Implementation using linked list is actually easier Ironically, the Java API’s LinkedList class implements the Queue interface, and will be our preferred implementation when we look at queue applications Need to decide which end of list is which; easiest implementation is to have the head pointer point to the front of the list, and maintain another pointer to keep track of the back

23 queues22 Code for linked list queue public class LinkedQueue implements Cloneable{ private int manyNodes; private Node front; private Node rear; public LinkedQueue( ) { front = null; rear = null; }

24 queues23 Code for linked list queue public void add(E item) { if (isEmpty( )) { front = new Node (item, null); rear = front; } else { rear.addNodeAfter(item); rear = rear.getLink( ); } manyNodes++; }

25 queues24 Code for linked list queue public LinkedQueue clone( ) { LinkedQueue answer; Node [ ] cloneInfo; try { answer = (LinkedQueue ) super.clone( ); } catch (CloneNotSupportedException e) { throw new RuntimeException ("This class does not implement Cloneable"); }

26 queues25 Clone method continued cloneInfo = Node.listCopyWithTail(front); answer.front = cloneInfo[0]; answer.rear = cloneInfo[1]; return answer; }

27 queues26 Code for linked list queue public boolean isEmpty( ) { return (manyNodes == 0); } public int size( ) { return manyNodes; }

28 queues27 Code for linked list queue public E remove( ) { E answer; if (manyNodes == 0) throw new NoSuchElementException("Queue underflow"); answer = front.getData( ); front = front.getLink( ); manyNodes--; if (manyNodes == 0) rear = null; return answer; }

29 Invariant for linked list implementation The number of items in the queue is stored in the instance variable manyNodes. The items in the queue are stored in a linked list, with the front of the queue stored at the head node, and the rear of the queue at the final node. For a non-empty queue, the instance variable front is the head reference of the linked list and the instance variable rear is the tail reference. For an empty queue, both front and rear are the null reference. queues28

30 queues229 Priority Queues Variation on an ordinary queue -- stores entries and a priority value for each entry Elements are dequeued according to priority, highest first In case of a tie, priority queue reverts to FIFO behavior

31 queues230 PQ implementation One strategy is to create an array of ordinary queues –each element in the array would be a queue of items –all items in any given queue have equal priority Array index indicates priority level

32 queues231 PQ Implementation public class PQ { private ArrayQueue [] queues; public int highest; public int total; public int highCurrent; public PQ (int h){ highest = h; queues = ArrayQueue [] new Object[h+1]; total = 0; highCurrent = 0; }

33 queues232 PQ implementation public int size () { return total; } public boolean is_empty() { return (total == 0); }

34 queues233 Enqueue function template void PQ ::PQenqueue(const Item& entry, int priority) { assert (priority <= HIGHEST); // if this is highest priority entry so far, so note: if (priority > highest_current) highest_current = priority; // place entry in queue: queues[priority].enqueue(entry); // increment count of total entries: count++; }

35 queues234 Dequeue function template Item PQ ::PQdequeue() { assert (PQsize() > 0); int p = highest_current; count--; for(p; p>=0; p--) if (!queues[p].is_empty()) return queues[p].dequeue(); }


Download ppt "queues1 Queues Data structures that wait their turn."

Similar presentations


Ads by Google