Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.

Similar presentations


Presentation on theme: "© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow."— Presentation transcript:

1 © 2004 Goodrich, Tamassia Queues

2 © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: insert(Object): inserts an element at the end of the queue Object remove(): removes and returns the element at the front of the queue Auxiliary queue operations: Object peek(): returns the element at the front without removing it int size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException

3 © 2004 Goodrich, Tamassia Stacks3 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

4 © 2004 Goodrich, Tamassia Stacks4 Array-based Queue Use an array of capacity N in a circular fashion Two variables keep track of the front and rear f index of the front element r index of the last element Initially f =0, r = -1 size: number of elements in the queue Q 012 r f normal configuration Q 012f r wrapped-around configuration

5 © 2004 Goodrich, Tamassia Stacks5 Queue Operations (cont.) Algorithm insert(o) if size = N then throw FullQueueException else r  (r + 1) mod N Q[r]  o size++ Operation enqueue throws an exception if the array is full OR reallocate This exception is implementation- dependent Q 012 r f Q 012f r

6 © 2004 Goodrich, Tamassia Stacks6 Queue Operations (cont.) Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Algorithm remove() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N size-- return o Q 012rf

7 © 2004 Goodrich, Tamassia Stacks7 Queue Interface in Java 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 peek() throws EmptyQueueException; public void insert(Object o); public Object remove() throws EmptyQueueException; }

8 © 2004 Goodrich, Tamassia Stacks8 Application: Round Robin Schedulers We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: 1. e = Q.remove() 2. Service element e 3. Q.insert(e) The Queue Shared Service 1.Remove the next element 3.Insert the serviced element 2.Service the next element

9 © 2004 Goodrich, Tamassia Stacks9 Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time f r  nodes elements

10 © 2004 Goodrich, Tamassia Stacks10 Double-Ended Queue ADT (Deque) Insertion/Deletion at both ends addFirst(e) addLast(e) removeFirst() removeLast() getFirst() getLast() size() isEmpty()

11 © 2004 Goodrich, Tamassia Stacks11 Implementing a deque? singly linked list? Probably not. Why?

12 © 2004 Goodrich, Tamassia Stacks12 Deque with a doubly linked list all operations are O(1)


Download ppt "© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow."

Similar presentations


Ads by Google