Queues.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Fundamentals of Python: From First Programs Through Data Structures
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.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Data structures Abstract data types Java classes for Data structures and ADTs.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks And Queues Chapter 18.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
April 27, 2017 COSC Data Structures I Review & Final Exam
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Stacks, Queues, and Deques
Stacks and Queues.
CSC 143 Queues [Chapter 7].
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Queues.
Presentation transcript:

Queues

What is a queue? Stacks reverse the order of items added to them Last In-First Out What if we want to preserve the order in which items are added? Solution: queue First In-First Out: items removed in the same order they’re added Similar to line (queue) at bank, supermarket, etc. Uses in computer science include Buffering (keyboard, network, etc.) Simulations Lots of other stuff CMPS 12B, UC Santa Cruz Queues

Operations on queues Same kinds of operations as stacks, but slightly different results Create Enqueue: add to the tail of the queue Dequeue: remove from the head of the queue Peek: look at the element at the head of the queue IsEmpty: tell whether the queue is empty DequeueAll: clear all elements from the queue head A B C D tail CMPS 12B, UC Santa Cruz Queues

Queue using circular linked list Last element in list points back to the first one Enqueue (adding an element) newNode.next = lastNode.next lastNode.next = newNode lastNode = newNode Dequeue (removing an element) firstNode = lastNode.next lastNode.next = firstNode.next Peek (look at first element) firstNode A B C D lastNode CMPS 12B, UC Santa Cruz Queues

Details on queues with linked lists Some methods can throw QueueException (like StackException) Dequeue() on an empty queue Peek() on an empty queue Enqueue() on an empty queue is a bit different lastNode = newNode newNode.next = newNode Dequeue() on a queue with exactly one element is different firstNode = lastNode lastNode = null DequeueAll() can be done by lastNode = null CMPS 12B, UC Santa Cruz Queues

Queues with arrays? As with stacks, queues can be implemented with arrays Naïve implementation Insert at top of array Remove from bottom of array (element 0) and shift array contents down one place Problem: this can be slow for large arrays! Better implementation: circular array Keep track of start and end of queue Queue “wraps around” the end of the array Use modular arithmetic for array indexes Space-efficient and fast CMPS 12B, UC Santa Cruz Queues

Circular arrays for queues Enqueue queueArray[back] = item count++; back = (back+1)%max_queue Dequeue item = queueArray[front] count--; front = (front+1)%max_queue Wraps around when front or back reaches max_queue NOTE: this implementation is slightly different from that in Chapter 7 7 A 6 1 front B back 2 5 count 2 1 1 4 3 CMPS 12B, UC Santa Cruz Queues

Details on queues with arrays Some methods can throw QueueException, as with linked list queues Dequeue() on an empty queue Peek() on an empty queue Queue is empty when count==0 There can be two situations where front==back If count==max_queue, queue is full If count==0, queue is emptyˆ Array-based queue can fill up! Enqueue() can throw a QueueException if count==max_queue Make the queue array large enough to avoid this DequeueAll() can be done by setting front=0, last=0, count=0 Same code as used to initialize an array-based queue… CMPS 12B, UC Santa Cruz Queues

Implementing queues (and stacks) Three choices for implementing queue ADT List ADT Array (circular) Linked list (circular) List ADT is simpler: less code to write Array Fixed maximum size Low overhead (no link references) Linked list Grows to any size Requires more space for a given number of elements In languages other than Java, allocating and deleting elements is an issue This favors arrays, which don’t need to allocate and delete very frequently (array methods don’t call new) CMPS 12B, UC Santa Cruz Queues

Queue application: simulations Computers often used to simulate behavior Customers at a bank Requests serviced by a roomful of Web servers Traffic on roadways All of these simulations consist of events An event occurs at a given time, determined by the model used in the simulation Events could include Car N enters Highway 1 at Morrissey Car N switches lanes at mile marker X Car N leaves freeway at 41st Avenue Simulation must keep track of thousands of events Events ordered by the time they occur Must process events in time order Use a queue! CMPS 12B, UC Santa Cruz Queues

Sample simulation: supermarket N checkout lines Each is FIFO Each line services the shopper at the front Time to service is determined by simulation Shopper may choose a line Simulation decides how rapidly shoppers arrive Simulation decides which line a shopper picks Shortest line “Express” line? Test different strategies Questions to answer: How many lines should there be? How should a shopper pick the best line? Queue 1 Queue 2 Queue 3 CMPS 12B, UC Santa Cruz Queues

Simulating a supermarket Each line is ordered by time Customer at front of line is next to finish (in that line) Amount of time to finish determined by simulation Simulation picks next to finish from front of all queues Advances “time” to t Dequeues the customer who finishes at time t This repeats as long as simulation runs More advanced simulations may have more complex queueing Time spent in each aisle Time spent looking for items Even more detail… Queue 1 Queue 2 Queue 3 CMPS 12B, UC Santa Cruz Queues

For next time… Hash tables: Chapter 12 in C&P Work on assignments Due Friday, Feburary 1st at 10:00:00 PM PST Don’t forget: design documents must be named design.pdf or design.txt! Class with main must be named WordCount Resources available Help from TA’s, tutors, and professor MyFile.java class (available online) for reading files CMPS 12B, UC Santa Cruz Queues