# 9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.

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

9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues in the Java class library. © 2001, D.A. Watt and D.F. Brown

9-2 Queue concepts (1) 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 length of a queue is the number of elements it contains. An empty queue has length zero.

9-3 Queue concepts (2) Illustration (queue of persons): BUS STOP

9-4 Queue applications Print server –maintains a queue of print jobs. Disk driver –maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system) –maintains a queue of processes awaiting a slice of machine time. Traffic simulation –Maintains many queues of vehicles

9-5 Example: demerging (1) Consider a file of person records, each of which contains a person’s name, gender, date-of-birth, etc. The records are sorted by date-of-birth. We are required to rearrange the records such that females precede males but they remain sorted by date-of- birth 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).

9-6 Example: demerging (2) Demerging algorithm: To rearrange a file of person records such that females precede males but their order is otherwise unchanged: 1.Make queues females and males empty. 2.Until the input file is empty, repeat: 2.1.Let p be the next person read in from the file. 2.2.If p is female, add p to the rear of females. 2.3.If p is male, add p to the rear of males. 3.Until females is empty, repeat: 3.1.Write out the person removed from the front of females. 4.Until males is empty, repeat: 4.1.Write out the person removed from the front of males. 5.Terminate.

9-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 length 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.

9-8 Queue ADT: contract (1) Possible contract, expressed as a Java interface: public interface Queue { // Each Queue object is a queue whose elements are objects. /////////////// Accessors /////////////// public boolean isEmpty (); // Return true if and only if this queue is empty. public int size (); // Return this queue’s length. public Object getFirst (); // Return the element at the front of this queue.

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

9-10 0frontrear–1maxlen–1 element Implementation of queues using arrays (1) Consider representing a bounded queue (length  maxlen) by:  a variable length, containing the current length  variables front and rear  an array elems of length maxlen, containing the queued elements in elems[front…rear–1]: Empty queue: 0maxlen–1front=rear unoccupied rear elementfront element

9-11 Implementation using arrays (2) Animation (with maxlen = 6): Initially: 012345 0 front 0 rear elems 0 length 0 Homer 1 Marge 2 Bart 3 Lisa 45 0 front 4 rear elems 4 length After adding Homer, Marge, Bart, Lisa: 0 Homer 1 Marge 2 Bart 3 Lisa 4 Maggie 5 0 front 5 rear elems 5 length After adding Maggie: 01 Marge 2 Bart 3 Lisa 4 Maggie 5 1 front 5 rear elems 4 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 2 front 5 rear elems 3 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 0 rear elems 4 length After adding Ralph:

9-12 Implementation using arrays (3) If we must shift elements along the array, operations addLast and removeFirst will have time complexity O(n). We can avoid this if we use a “cyclic array” instead of an ordinary array.

9-13 Cyclic arrays In a cyclic array a of length n, every component 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]. Alternative ways of visualizing a cyclic array (length 8): 0 1 2 3 4 5 6 7 01234567

9-14 Implementation of queues using cyclic arrays (1) Representing a bounded queue (length  maxlen) by:  a variable length, containing the current length  variables front and rear  a cyclic array elems of length maxlen, containing the queued elements either in elems[front…rear–1] or in elems[front…maxlen–1] and elems[0…rear–1]. 0frontrear–1maxlen–1 element 0maxlen–1 front rear–1 Empty queue: 0maxlen–1front=rear

9-15 Implementation using cyclic arrays (2) Animation (with maxlen = 6): Initially: 012345 0 front 0 rear elems 0 length 0 Homer 1 Marge 2 Bart 3 Lisa 45 0 front 4 rear elems 4 length After adding Homer, Marge, Bart, Lisa: 0 Homer 1 Marge 2 Bart 3 Lisa 4 Maggie 5 0 front 5 rear elems 5 length After adding Maggie: 01 Marge 2 Bart 3 Lisa 4 Maggie 5 1 front 5 rear elems 4 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 2 front 5 rear elems 3 length After removing the front element: 012 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 0 rear elems 4 length After adding Ralph: 0 Nelson 12 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 1 rear elems 5 length After adding Nelson: 0 Nelson 1 Martin 2 Bart 3 Lisa 4 Maggie 5 Ralph 2 front 2 rear elems 6 length After adding Martin: 0 Nelson 1 Martin 23 Lisa 4 Maggie 5 Ralph 3 front 2 rear elems 5 length After removing the front element:

9-16 Implementation using cyclic arrays (6) The Java implementation is included with this weeks exercises – study this! Analysis:  Most operations have time complexity O(1).  Operation clear has time complexity O(maxlen).

9-17 Implementation of queues using SLLs (1) Represent an (unbounded) queue by:  an SLL, whose first node contains the front element, and whose header contains links to the first node (front) and last node (rear).  a variable length (optional). element front rear Empty queue: front rear Illustration: front rear Homer Marge Bart Lisa

9-18 Implementation using SLLs (2) Java implementation: public class LinkedQueue implements Queue { private SLLNode front, rear; /////////////// Constructor /////////////// public LinkedQueue () { front = rear = null; } /////////////// Accessors /////////////// public boolean isEmpty () { return (front == null); }

9-19 Implementation using SLLs (3) Java implementation (continued): public int size () { int length = 0; for (SLLNode curr = front; curr != null; curr = curr.succ) length++; return length; } public Object getFirst () { if (front == null) throw … ; return front.element; }

9-20 Implementation using SLLs (4) Java implementation (continued): /////////////// Transformers /////////////// public void clear () { front = rear = null; } public void addLast (Object elem) { SLLNode newest = new SLLNode(elem, null); if (rear != null) rear.succ = newest; else front = newest; rear = newest; }

9-21 Implementation using SLLs (5) Java implementation (continued): public Object removeFirst () { if (front == null) throw … ; Object frontElem = front.element; front = front.succ; if (front == null) rear = null; return frontElem; } } Analysis: Most operations have time complexity O(1), but operation size is O(n). However, size too would be O(1) if we used a variable to hold the length of the structure.

9-22 Queues in the Java class library Java provides no Queue interface or class as such. However, the java.util.LinkedList class provides all the above Queue operations. Illustration: import java.util.LinkedList; LinkedList queue = new LinkedList(); queue.addLast("Homer"); queue.addLast("Marge"); queue.addLast("Bart"); queue.addLast("Lisa"); queue.addLast("Maggie"); System.out.println(queue.removeFirst());

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

Similar presentations