Presentation is loading. Please wait.

Presentation is loading. Please wait.

7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.

Similar presentations


Presentation on theme: "7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues."— Presentation transcript:

1 7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues in the Java class library © 2008 David A Watt, University of Glasgow Algorithms & Data Structures (M)

2 7-2 Queue concepts  A queue is a first-in-first-out sequence of elements. Elements can added only at one end (the rear of the queue) and removed only at the other end (the front of the queue).  The size (or length) of a queue is the number of elements it contains.

3 7-3 Example: bus queue  Consider a queue of persons at a bus-stop: BUS STOP

4 7-4 Queue applications  Print server – Uses a queue of print jobs.  Operating system – Disk driver uses a queue of disk input/output requests. – Scheduler uses a queue of processes awaiting a slice of processor time.

5 7-5 Example: demerging (1)  Consider a file of person records, each of which contains a person ’ s name, gender, birth-date, etc. The records are sorted by birth-date. We are required to rearrange the records such that females precede males but they remain sorted by birth-date within each gender group.  Bad idea: use a sorting algorithm. Time complexity is O(n log n) at best.  Good idea: use a demerging algorithm. Time complexity is O(n).

6 7-6 Example: demerging (2)  Demerging algorithm: To copy a file of person records from input to output, rearranged such that females precede males but their order is otherwise unchanged: 1.Make queues females and males empty. 2.For each person p in input, repeat: 2.1.If p is female, add p at the rear of females. 2.2.If p is male, add p at the rear of males. 3.While females is not empty, repeat: 3.1.Remove a person f from the front of females. 3.2.Write f to output. 4.While males is not empty, repeat: 4.1.Remove a person m from the front of males. 4.2.Write m to output. 5.Terminate.

7 7-7 Queue ADT: requirements  Requirements: 1)It must be possible to make a queue empty. 2)It must be possible to test whether a queue is empty. 3)It must be possible to obtain the size of a queue. 4)It must be possible to add an element at the rear of a queue. 5)It must be possible to remove the front element from a queue. 6)It must be possible to access the front element in a queue without removing it.

8 7-8 Queue ADT: contract (1)  Possible contract for homogeneous queues (expressed as a Java generic interface): public interface Queue { // Each Queue object is a homogeneous queue // whose elements are of type E. /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this queue is empty. public int size (); // Return this queue’s size. public E getFirst (); // Return the element at the front of this queue.

9 7-9 Queue ADT: contract (2)  Possible contract (continued): ////////////// Transformers ////////////// public void clear (); // Make this queue empty. public void addLast (E it); // Add it as the rear element of this queue. public E removeFirst (); // Remove and return the front element of this queue. }

10 7-10 Implementation of queues using arrays (1)  Consider representing a bounded queue (size  cap) by: –variables size, front, rear –an array elems of length cap, containing the elements in elems[front…rear–1]. Empty queue: 0cap–1front=rear Invariant: element 0frontrear–1cap–1 unoccupiedfront elementrear element

11 7-11 Initially: front 0 rear elems 0 size 0 Homer 1 Marge 2 Maggie 3 Lisa 45 0 front 4 rear elems 4 size After adding Homer, Marge, Maggie, Lisa: 0 Homer 1 Marge 2 Maggie 3 Lisa 4 Bart 5 0 front 5 rear elems 5 size After adding Bart: 01 Marge 2 Maggie 3 Lisa 4 Bart 5 1 front 5 rear elems 4 size After removing the front element: 012 Maggie 3 Lisa 4 Bart 5 2 front 5 rear elems 3 size After removing the front element: 012 Maggie 3 Lisa 4 Bart 5 Ralph 2 front 0 rear elems 4 size After adding Ralph: Implementation of queues using arrays (2)  Animation (with cap = 6):

12 7-12 Implementation of queues using arrays (3)  Once the rightmost array slot is occupied, no more elements can be added, unless we shift elements to fill up any unoccupied leftmost slots.  But then operation addLast would have time complexity O(n), rather than O(1).  We can avoid this if we use a “cyclic array” instead of an ordinary array.

13 7-13 Cyclic arrays  In a cyclic array a of length n, every slot has both a successor and a predecessor. In particular: –the successor of a[n–1] is a[0] –the predecessor of a[0] is a[n–1].  Visualizing a cyclic array (of length 8): or

14 7-14 Implementation of queues using cyclic arrays (1)  Represent a bounded queue (size  cap) by: –variables size, front, rear –a cyclic array elems of length cap, containing the elements either (a) in elems[front…rear–1] or (b) in elems[front…cap–1] and elems[0…rear–1]. cap–1 front rear–1 0 (b) element cap–1 front rear–1 0 Invariant: (a) element Empty queue: 0cap–1front=rear

15 7-15 Initially: 0 front 0 rear elems 0 size Implementation of queues using cyclic arrays (2)  Animation (with cap = 6): HomerMargeMaggieLisa 0 front 4 rear elems 4 size After adding Homer, Marge, Maggie, Lisa: HomerMargeMaggieLisaBart 0 front 5 rear elems 5 size After adding Bart: MargeMaggieLisaBart 1 front 5 rear elems 4 size After removing the front element: MaggieLisaBart 2 front 5 rear elems 3 size After removing the front element: MaggieLisaBartRalph 2 front 0 rear elems 4 size After adding Ralph: NelsonMaggieLisaBartRalph 2 front 1 rear elems 5 size After adding Nelson: NelsonMartinMaggieLisaBartRalph 2 front 2 rear elems 6 size After adding Martin: NelsonMartinLisaBartRalph 3 front 2 rear elems 5 size After removing the front element:

16 7-16 Implementation of queues using cyclic arrays (3)  Java implementation: public class ArrayQueue implements Queue { private E[] elems; private int size, front, rear; /////////////// Constructor /////////////// public ArrayQueue (int cap) { elems = (E[]) new Object[cap]; size = front = rear = 0; }

17 7-17 Implementation of queues using cyclic arrays (4)  Java implementation (continued): /////////////// Accessors /////////////// public boolean isEmpty () { return (size == 0); } public int size () { return size; } public E getFirst () { if (size == 0) throw … ; return elems[front]; }

18 7-18 Implementation of queues using cyclic arrays (5)  Java implementation (continued): ////////////// Transformers /////////////// public void clear () { size = front = rear = 0; } public void addLast (E it) { if (size == elems.length) … elems[rear++] = it; if (rear == elems.length) rear = 0; size++; } NB

19 7-19 Implementation of queues using cyclic arrays (6)  Java implementation (continued): public E removeFirst () { if (size == 0) throw … ; E frontElem = elems[front]; elems[front++] = null; if (front == elems.length) front = 0; size--; return frontElem; } }  Analysis: –All operations have time complexity O(1). NB

20 7-20 Implementation of queues using SLLs (1)  Represent an (unbounded) queue by: –an SLL, whose header contains links to the first node (front) and last node (rear). –a variable size (optional). Invariant: element front rear size Empty queue: front rear size 0 Illustration: Homer Marge Maggie Lisa front rear size 4

21 7-21 Implementation of queues using SLLs (2)  Java implementation: public class LinkedQueue implements Queue { private Node front, rear; private int size; /////////////// Inner class /////////////// private static class Node { public E element; public Node succ; public Node (E x, Node s) { element = x; succ = s; } }

22 7-22 Implementation of queues using SLLs (3)  Java implementation (continued): /////////////// Constructor /////////////// public LinkedQueue () { front = rear = null; size = 0; } /////////////// Accessors /////////////// public boolean isEmpty () { return (front == null); }

23 7-23 Implementation of queues using SLLs (4)  Java implementation (continued): public int size () { return size; } public E getFirst () { if (front == null) throw … ; return front.element; }

24 7-24 Implementation of queues using SLLs (5)  Java implementation (continued): ////////////// Transformers /////////////// public void clear () { front = rear = null; size = 0; } public void addLast (E it) { Node newest = new Node(it, null); if (rear != null) rear.succ = newest; else front = newest; rear = newest; size++; }

25 7-25 Implementation of queues using SLLs (6)  Java implementation (continued): public E removeFirst () { if (front == null) throw … ; E frontElem = front.element; front = front.succ; if (front == null) rear = null; size--; return frontElem; } }  Analysis: –All operations have time complexity O(1).

26 7-26 Queues in the Java class library  The library interface java.util.Queue is similar to the above interface Queue.  The library class java.util.LinkedList implements java.util.Queue, representing each queue by a doubly-linked-list. (This is overkill!)  Illustration: import java.util.*; Queue busQ = new LinkedList (); busQ.addLast(homer); busQ.addLast(marge); busQ.addLast(maggie); busQ.addLast(lisa); busQ.addLast(bart); Person p = busQ.removeFirst();

27 7-27 Example: demerging again (1)  Implementation of the demerging algorithm: public static void reSort ( BufferedReader input, BufferedWriter output) throws IOException { // Copy a file of person records from input to output, // rearranged such that females precede males but their // order is otherwise unchanged. Queue females = new LinkedList (), males = new LinkedList (); for (;;) { Person p = readPerson(input); if (p == null) break; // end of input

28 7-28 Example: demerging again (2)  Implementation (continued): if (p.female) females.addLast(p); else males.addLast(p); } while (! females.isEmpty()) { Person f = females.removeFirst(); writePerson(output, f); } while (! males.isEmpty()) { Person m = males.removeFirst(); writePerson(output, m); } }


Download ppt "7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues."

Similar presentations


Ads by Google