Unformatted text preview:

Topic 16 QQueues "FISH queue: n.[acronym, by analogy with FIFO (First In, First Out)] ‘First In, Still Here’. A joking way of pointing out that processing of a particular sequence of events or requests has stopped dead Also FISH mode and FISHnet; thedead. Also FISH mode and FISHnet; the latter may be applied to any network that is running really slowly or exhibiting extremerunning really slowly or exhibiting extreme flakiness."-The Jargon File 4.4.7CS 307 Fundamentals of Computer Science Queues1gQueuesSi il t St kSimilar to StacksLike a line–In Britain people don’t “get in line” they“queue up”.they queue up.CS 307 Fundamentals of Computer Science Queues2Queue PropertiesQueues are a first in first out data structure– FIFO (or LILO, but that sounds a bit silly)Add items to the end of the queueAccess and remove from the frontAccess and remove from the front– Access to the element that has been in the structure the longest amount of timegUsed extensively in operating systemsQueues of processes I/O requests and much–Queues of processes, I/O requests, and much moreCS 307 Fundamentals of Computer Science Queues3Queues in Operating SystemsOih1CPUb hOn a computer with 1 CPU, but many processes how many processes can actually use the CPU at a time?O jbfOS hdlth f thCPUOne job of OS, schedule the processes for the CPUissues: fairness, responsiveness, progressCS 307 Fundamentals of Computer Science Queues4Queue operationsadd(Object item)– a.k.a. enqueue(Object item)Object get()–a.k.a. Object front(), Object peek()jjpObject remove()–akaObject dequeue()a.k.a. Object dequeue()boolean isEmpty()Sifi itf ll idSpecify in an interface, allow varied implementationsCS 307 Fundamentals of Computer Science Queues5Queue interface, version 1public interface Queue{ //place item at back of this Queueenqueue(Object item);q(j );//access item at front of this queue//pre: !isEmpty()//pre: !isEmpty()Object front();//remove item at front of this queue//pre: !isEmpty()Object dequeue();j q ();boolean isEmpty();}CS 307 Fundamentals of Computer Science Queues6}Implementing a QueueGi th i t l t t i dGiven the internal storage container and choice for front and back of queue what are th Bi O f th ti ?the Big O of the queue operations?ArrayList LinkedListLinkeListArrayList LinkedListLinkeList(Singly Linked) (Doubly Linked)enqueuefrontdequeuedequeueisEmptyCS 307 Fundamentals of Computer Science Queues7Attendance Question 1If implementing a queue with a singly linked list with references to the first and last nodes (head and tail) which end of the list should be the front of the queue in order to have all O( )?queue operations O(1)?A. The front of the list should be the front of the queueB. The back of the list should be the front of the queue.C. D. E. I don’t know, but I am sure looking forwardt t ki 307 i tiCS 307 Fundamentals of Computer Science Queues8to taking 307 again some time.Alternate ImplementationHow about implementing a Queue with a native array?– Seems like a step backwardsCS 307 Fundamentals of Computer Science Queues9Application of QueuesRdi S tRadix Sort– radix is a synonym for base. base 10, base 2M lti ti l ith th tllkMulti pass sorting algorithm that onlylooks at individual digits during each passUbkttt l tUse queues as bucketsto store elementsCreate an array of 10 queuesStarting with the least significant digit place value in queue that matches digitempty queues back into arrayrepeat, moving to next least significant digitCS 307 Fundamentals of Computer Science Queues10pg ggRadix Sort in Action: 1soriginal values in array113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12Look at ones place113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12,,,,,,,,,,Queues:070405070, 40516 86212252127377212, 252, 127 37, 73 113, 93 849979CS 307 Fundamentals of Computer Science Queues1149 9, 79Radix Sort in Action: 10sEt i df 0t9bkitEmpty queues in order from 0 to 9 back into array70401225212113938637797970, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79Now look at 10's place70401225212113938637797970, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79Queues:079525207, 9 5 252 112, 12, 113 6 27707927 70, 79 337 8 86440993CS 307 Fundamentals of Computer Science Queues12440 9 93Radix Sort in Action: 100sEidf09bkiEmpty queues in order from 0 to 9 back into array7, 9, 12, 12, 113, 37, 40, 252, 70, 79, 86, 93N l k t 100' lNow look at 100's place__7, __9, _12, _12, 113, _37, _40, 252, _70, _79, _86, _93QQueues:0 7, 9, _12, _12, _40, _70, _79, _86, _93 5 111361113 6 2252 7 383849CS 307 Fundamentals of Computer Science Queues13Radix Sort in Action: Final StepEmpty queues in order from 0 to 9 back into array7, 9, 12, 12, 40, 70, 79, 86, 93, 113, 252CS 307 Fundamentals of Computer Science Queues14Radix Sort Codepublic static void sort(int[] list){public static void sort(int[] list){ArrayList<Queue<Integer>> queues = new ArrayList<Queue<Integer>>();for(int i = 0; i < 10; i++)queues.add( new LinkedList<Integer>() );int passes=numDigits( list[0] );int passes numDigits( list[0] );int temp;for(int i = 1; i < list.length; i++){temp = numDigits(list[i]);if( temp > passes )if( temp > passes )passes = temp;}for(int i = 0; i < passes; i++){for(int j=0;j<list.length;j++){for(int j 0; j < list.length; j++){queues.get(valueOfDigit(list[j], i)).add(list[j]);}int pos = 0;for(Queue<Integer>q:queues){for(Queue<Integer> q : queues){while( !q.isEmpty())list[pos++] = q.remove();} }CS 307 Fundamentals of Computer Science


View Full Document

UT CS 307 - Topic 16 Queues

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Topic 16 Queues
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Topic 16 Queues and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Topic 16 Queues 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?