© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.

Slides:



Advertisements
Similar presentations
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Advertisements

Queues.
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.
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.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
Queues and Priority Queues
COSC2006 Chapter 8 Queues III
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks, Queues & Deques CSC212.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
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.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
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’
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
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)
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
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++
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,
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Queues Chapter 8 (continued)
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Double-Ended Queues Chapter 5.
CC 215 Data Structures Queue ADT
Csc 2720 Data structure Instructor: Zhuojun Duan
CENG 213 Data Structure Queue 7/2/2018.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Chapter 13 Queues and Priority Queues
Queues, Deques and Priority Queues
Queues.
Queues, Deques and Priority Queues
Queues Chapter 8 (continued)
Queue and Priority Queue Implementations
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-2 An Implementation That Uses the ADT List If the item in position 1 of a list list represents the front of the queue, the following implementations can be used –dequeue() list.remove(1) –peek() list.get(1)

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-3 An Implementation That Uses the ADT List If the item at the end of the list represents the back of the queue, the following implementations can be used –enqueue(newItem) list.add(list.size()+1, newItem) Figure 8-13 An implementation that uses the ADT list

© 2011 Pearson Addison-Wesley. All rights reserved 5 B-4 The Java Collections Framework Interface Queue JCF has a queue interface called Queue Derived from interface Collection Adds methods : –element: retrieves, but does not remove head –offer: inserts element into queue –peek: retrieves, but does not remove head –poll: retrieves and removes head –remove: retrieves and removes head

© 2011 Pearson Addison-Wesley. All rights reserved 5 B-5 The Java Collections Framework Interface Deque Deque = double-ended queue –(pronounced “deck”) Allows us to insert and delete from either end –Useful methods: addFirst, addLast, peekFirst, peekLast, getFirst, getLast, removeFirst, removeLast Thus, may function as both a stack and a queue Example: text editor –Input characters using “stack” functionality: backspace event causes a pop. Output characters using “queue” functionality.

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-6 Comparing Implementations All of the implementations of the ADT queue mentioned are ultimately either –Array based –Reference based Fixed size versus dynamic size –A statically allocated array Prevents the enqueue operation from adding an item to the queue if the array is full –A resizable array or a reference-based implementation Does not impose this restriction on the enqueue operation

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-7 Comparing Implementations Reference-based implementations –A linked list implementation More efficient –The ADT list implementation Simpler to write

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-8 A Summary of Position-Oriented ADTs Position-oriented ADTs –List –Stack –Queue Stacks and queues –Only the end positions can be accessed Lists –All positions can be accessed

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-9 A Summary of Position-Oriented ADTs Stacks and queues are very similar –Operations of stacks and queues can be paired off as createStack and createQueue Stack isEmpty and queue isEmpty push and enqueue pop and dequeue Stack peek and queue peek

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-10 A Summary of Position-Oriented ADTs ADT list operations generalize stack and queue operations –length –add –remove –get

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-11 Application: Simulation Simulation –A technique for modeling the behavior of both natural and human-made systems –Goal Generate statistics that summarize the performance of an existing system Predict the performance of a proposed system –Example A simulation of the behavior of a bank

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-12 Application: Simulation Figure 8-14a and 8-14b A blank line at at time a) 0; b) 12

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-13 Application: Simulation Figure 8-14c and 8-14d A blank line at at time c) 20; d) 38

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-14 Application: Simulation An event-driven simulation –Simulated time is advanced to the time of the next event –Events are generated by a mathematical model that is based on statistics and probability A time-driven simulation –Simulated time is advanced by a single time unit –The time of an event, such as an arrival or departure, is determined randomly and compared with a simulated clock

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-15 Application: Simulation The bank simulation is concerned with –Arrival events Indicate the arrival at the bank of a new customer External events: the input file specifies the times at which the arrival events occur –Departure events Indicate the departure from the bank of a customer who has completed a transaction Internal events: the simulation determines the times at which the departure events occur

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-16 Application: Simulation An event list is needed to implement an event-driven simulation –An event list Keeps track of arrival and departure events that will occur but have not occurred yet Contains at most one arrival event and one departure event Figure 8-15 A typical instance of the event list

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-17 Summary The definition of the queue operations gives the ADT queue first-in, first-out (FIFO) behavior A reference-based implementation of a queue uses either –A circular linked list –A linear linked list with a head reference and a tail reference An array-based implementation of a queue is prone to rightward drift –A circular array eliminates the problem of rightward drift

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-18 Summary To distinguish between the queue-full and queue- empty conditions in a queue implementation that uses a circular array, you can –Count the number of items in the queue –Use a full flag –Leave one array location empty Models of real-world systems often use queues –The event-driven simulation in this chapter uses a queue to model a line of customers in a bank

© 2011 Pearson Addison-Wesley. All rights reserved 8 B-19 Summary Simulations –Central to a simulation is the notion of simulated time In a time-driven simulation –Simulated time is advanced by a single time unit In an event-driven simulation –Simulated time is advanced to the time of the next event –To implement an event-driven simulation, you maintain an event list that contains events that have not yet occurred