241-423 Advanced Data Structures and Algorithms: 9. Queues
241-423 Advanced Data Structures and Algorithms: 9. Queues
9. Queues
Objective
implement queues, bounded queues, and priority queues. Explain Radix sort.
241-423 ADSA: Queues/9 1
Contents
1. 2. 3. 4. 5. The Queue Collection Interview Scheduler Radix Sort A Bounded Queue Priority Queue Collection
boolean isEmpty() Returns true if this collection contains no elements and false if the collection has at least 1 element. T peek() Refer to element at the front of the queue. If empty, throws a NoSuchElementException.
continued
interface Queue<T>
ds.util
T pop() Removes the element at the front of the queue and returns its value. If the queue is empty, throws a NoSuchElementException. void push(T item) Inserts item at the back of the queue. int size() Returns the number of elements in this queue
public T pop() { if (isEmpty()) throw new NoSuchElementException( "LinkedQueue pop(): queue empty"); // remove and return first element in list return qlist.removeFirst(); } } // end of LinkedQueue class
2. Interview Scheduler
The interview scheduler is a queue of
Time24 objects. It outputs a list of the times and the potential length of each interview.
Execution
public class InterviewScheduler { public static void main(String[] args) throws IOException { Time24 END_DAY = new Time24(17,00); // read times from "appt.txt" Scanner input = new Scanner( new FileReader("appt.txt") ); :
10
// create a queue for the appointment times LinkedQueue<Time24> apptQ = new LinkedQueue<Time24>(); while (input.hasNext()) { String apptStr = input.nextLine(); // read time apptQ.push( Time24.parseTime(apptStr) ); // add time to queue }
// output the day's appointment schedule System.out.println("Appointment Interview"); :
11
Time24 apptTime, interviewTime; while (!apptQ.isEmpty()) { apptTime = apptQ.pop(); // get next appointment
/* interview time is time to next appointment or to END_DAY */ if (!apptQ.isEmpty()) interviewTime = apptTime.interval( apptQ.peek()); else interviewTime = apptTime.interval(END_DAY);
System.out.println(" "
} } // end of main()
12
3. Radix Sort
Radix sort uses the digits in each array element
to sort the array. The array elements are passed to ten queues (index 0 to 9) using each element's digit as the index. Elements are copied from the queues back to the original array, in partially sorted order.
13
After Pass 0: [30, 91, 92, 22, 85, 15, 35, 6, 39]
partially sorted
After Pass 1: [6, 15, 22, 30, 35, 39, 85, 91, 92]
241-423 ADSA: Queues/9
fully sorted
continued
14
continued
15
16
private static void distribute(int[] arr, LinkedQueue[] digitQueues, int power) // distribute array elements between 10 queues { for (int i = 0; i < arr.length; i++) { int digit = (arr[i] / power) % 10; // get digit from element digitQueues[digit].push( arr[i] ); /* use digit to decide which queue should hold the element */ } }
18
private static void collect(LinkedQueue[] digitQueues, int[] arr) // copy elements from the queues back to the array { int i = 0; // scan the array of queues for (int digit = 0; digit < 10; digit++) while (!digitQueues[digit].isEmpty()) { arr[i] = digitQueues[digit].pop(); // queue-->arr i++; } }
19
20
private static void displayArray(int[] arr) { for (int i = 0; i < arr.length; i++) { String s = String.valueOf(arr[i]); // int --> string // right justify s inside 8 char positions String just = ""; for (int j=0; j < 8-s.length(); j++) just += " "; just += s; System.out.print(just); if ((i+1) % 6 == 0) // newline every 6 numbers System.out.println(); } System.out.println(); // end of displayArray()
} }
// end of UseRadixSort
241-423 ADSA: Queues/9 21
Execution
22
4. A Bounded Queue
A bounded queue is a queue that has a
maximum size (capacity).
24
25
// print element at the q front, and queue size System.out.println(q.peek() + ", " + q.size()); try { q.push(40); // try to add 40 to full bqueue } catch (IndexOutOfBoundsException e) { System.out.println(e); } 1, 15 java.lang.IndexOutOfBoundsException: BQueue push(): queue full
241-423 ADSA: Queues/9 27
BQueue Class
public class BQueue<T> implements Queue<T> { private T[] queueArray; // array for queue elements // index of front and back of queue private int qfront, qback; // queue capacity, and current size private int qcapacity, qcount;
public BQueue(int size) // create an empty bounded queue with the specified size { queueArray = (T[])new Object[size]; qfront = 0; qback = 0; qcapacity = size; qcount = 0; }
241-423 ADSA: Queues/9 28
public BQueue() // a bounded queue of max size 50 { BQueue(50); } // method full() and other methods in the Queue interface } // end of BQueue class
29
continued
30
Think of the queue as a circular sequence. Element are added in a clockwise order.
the element at index qfront exits the queue an element enters the queue at index qback.
continued
31
34
35
qcount--;
37
PQueue Interface
The generic PQueue resembles a queue
with the same method names.
interface PQueue<T> ds.util
boolean isEmpty() Returns true if the priority queue is empty and false otherwise. T peek() Refer to value of the highest-priority item. If empty, throws a NoSuchElementException.
continued
38
interface PQueue<T>
ds.util
T pop() Removes the highest priority item from the queue and returns its value. If it is empty, throws a NoSuchElementException. void push(T item) Inserts item into the priority queue. int size() Returns the number of elements in this priority queue
39
HeapPQueue Class
The collection class HeapPQueue implements
the PQueue interface, using a Heap data structure which I'll explain in a later part.
40
blue
41