Topic 16 Queues "FISH queue: n. [acronym, by analogy with FIFO (First In, First Out)] ‘First In, Still Here’. A joking way of pointing out that processing.

Slides:



Advertisements
Similar presentations
Queues and Linked Lists
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
CHAPTER 7 Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
TCSS 342, Winter 2005 Lecture Notes
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Implementing and Using Stacks
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Scott Grissom, copyright 2004 Ch 6 Data Structures Slide 1 Linear Data Structures Chapter 6 focuses on: Lists Stacks Queues You can skip the Array implementation.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Linked Data Structures
QueueStack CS1020.
Week 4 - Friday CS221.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Topic 16 Queues "FISH queue: n.
Topic 16 Queues Adapted from Mike Scott’s materials.
structures and their relationships." - Linus Torvalds
Java Methods Stacks and Queues A & AB Object-Oriented Programming
Queues.
Topic 16 Queues "FISH queue: n.
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Lecture 16 Stacks and Queues CSE /26/2018.
structures and their relationships." - Linus Torvalds
Presentation transcript:

Topic 16 Queues "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; the latter may be applied to any network that is running really slowly or exhibiting extreme flakiness." -The Jargon File 4.4.7

CS314 Queues 2  A sharp tool like stacks  Like a line –In England people don’t “get in line” they “queue up”.

CS314 Queues 3 Queue 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 front –Access to the element that has been in the structure the longest amount of time  Used extensively in operating systems –Queues of processes, I/O requests, and much more

CS314 Queues 4 Queues in Operating Systems  On a computer with N cores on the CPU, but more than N processes, how many processes can actually be executing at one time?  One job of OS, schedule the processes for the CPU

CS314 Queues 5 Queue operations  enqueue(E item) –a.k.a. add(E item)  E front() – a.k.a. E peek()  E dequeue() – a.k.a. E remove()  boolean isEmpty()  Specify methods in an interface, allow multiple implementations.

CS314 Queues 6 Queue interface, version 1 public interface Queue { //place item at back of this Queue enqueue(E item); //access item at front of this Queue //pre: !isEmpty() E front(); //remove item at front of this Queue //pre: !isEmpty() E dequeue(); boolean isEmpty(); }

CS314 Queues 7 Implementing a Queue  Given the internal storage container and choice for front and back of queue what are the Big O of the queue operations? ArrayList LinkedListLinkedList (Singly Linked)(Doubly Linked) enqueue front dequeue isEmpty

Clicker 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 queue operations O(1)? A.The front of the list should be the front of the queue B.The back of the list should be the front of the queue. C.Either end will work to make all ops O(1) D.Neither end will allow all ops to be O(1) CS314 Queues 8

CS314 Queues 9 Alternate Implementation  How about implementing a Queue with a native array? –Seems like a step backwards

CS314 Queues 10 Application of Queues  Radix Sort –radix is a synonym for base. base 10, base 2  Multi pass sorting algorithm that only looks at individual digits during each pass  Use queues as buckets to 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 digit

CS314 Queues 11 Radix Sort in Action: 1s  original values in array 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12  Look at ones place 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12  Queues: 070, , 252, , , , 79

CS314 Queues 12 Radix Sort in Action: 10s  Empty queues in order from 0 to 9 back into array 70, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79  Now look at 10's place 70, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79  Queues: 0 7, , 12, ,

CS314 Queues 13 Radix Sort in Action: 100s  Empty queues in order from 0 to 9 back into array 7, 9, 12, 12, 113, 37, 40, 252, 70, 79, 86, 93  Now look at 100's place __7, __9, _12, _12, 113, _37, _40, 252, _70, _79, _86, _93  Queues: 0 7, 9, _12, _12, _37, _40, _70, _79, _86, _

CS314 Queues 14 Radix Sort in Action: Final Step  Empty queues in order from 0 to 9 back into array 7, 9, 12, 12, 40, 70, 79, 86, 93, 113, 252

CS314 Queues 15 Radix Sort Code public static void sort(int[] list){ ArrayList > queues = new ArrayList >(); for(int i = 0; i < 10; i++) queues.add( new LinkedList () ); int passes = numDigits(list[0]); // helper method // or int passes = (int) Math.log10(list[0]); for(int i = 1; i < list.length; i++){ int temp = numDigits(list[i]); if( temp > passes ) passes = temp; } for(int i = 0; i < passes; i++){ for(int j = 0; j < list.length; j++) queues.get(valueOfDigit(list[j], i)).add(list[j]); int pos = 0; for(Queue q : queues){ while(!q.isEmpty()) list[pos++] = q.remove(); }