1 CSC 222: Computer Programming II Spring 2004 Queues and simulation  queue ADT  enqueue (push), dequeue (pop), front, empty, size  library  application:

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.”
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Data Structure Dr. Mohamed Khafagy.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Comp 245 Data Structures Queues. Introduction to the Queue ADT It is a FIFO (first-in, first-out) structure Access to the Queue can take place at two.
CS Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of List, Stack and Queue.
COSC2006 Chapter 8 Queues III
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
1 CSC 222: Computer Programming II Spring 2005 Queues and simulation  queue ADT enqueue (offer), dequeue (remove), peek, empty, size  java.util.Queue.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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.”
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
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,
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
1 CSC 321: Data Structures Fall 2012 Lists, stacks & queues  Collection classes: ─List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
1 CSC 321: Data Structures Fall 2013 Lists, stacks & queues  Collection classes: ─List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap,
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 Data Organization Example 1: A simple text editor –Store the text buffer as a list of lines. –How would we implement the UNDO operation? Example 2: Parsing.
Mark Redekopp David Kempe
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Queues.
Queue.
Stacks and Queues.
Queues Queues Queues.
Monday, February 26, 2018 Announcements… For Today…
CSC 143 Queues [Chapter 7].
CSC 321: Data Structures Fall 2018 Lists, stacks & queues
Visit for more Learning Resources
CE 221 Data Structures and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Queues.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CSCS-200 Data Structure and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Getting queues right … finally (?)
Chapter 6 – Queues and Deques
Data Structures & Programming
Presentation transcript:

1 CSC 222: Computer Programming II Spring 2004 Queues and simulation  queue ADT  enqueue (push), dequeue (pop), front, empty, size  library  application: bank simulation  OOP design, testing strategies

2 Lists & queues recall: a stack is a simplified list  add/delete/inspect at one end  useful in many real-word situations (e.g., delimiter matching, run-time stack) queue ADT  a queue is another kind of simplified list  add at one end (the back), delete/inspect at other end (the front) DATA:sequence of items OPERATIONS:enqueue (push at back), dequeue (pop off front), look at front, check if empty, get size these are the ONLY operations allowed on a queue — queues are useful because they are simple, easy to understand — each operation is O(1)

3 Queue examples  line at bank, bus stop, grocery store, …  printer jobs  CPU processes  voice mail a queue is also known as  first-in-first-out (FIFO) list 321 frontback enqueue (push): adds item at the back 321 front 4 back deueue (pop): removes item at the front 32 front back front: returns item at the front  frontback

4 Queue exercise start with empty queue ENQUEUE 1 ENQUEUE 2 ENQUEUE 3 FRONT ENQUEUE 4 DEQUEUE FRONT ENQUEUE 5

5 library queue numQ; int num; while (cin >> num) { numQ.push(num); } while ( !numQ.empty() ) { cout << numQ.front() << endl; numQ.pop(); } since a queue is a common data structure, a predefined C++ library exists #include the standard queue class has the following member functions void push(const TYPE & item); void pop(); TYPE & front() const; bool empty() const; int size() const; queue numQ1, numQ2; int num; while (cin >> num) { numQ1.push(num); } while ( !numQ1.empty() ) { numQ2.push( numQ1.front() ); numQ1.pop(); } while ( !numQ2.empty() ) { cout << numQ2.front() << endl; numQ2.pop(); }

6 Queues and simulation queues are especially useful for simulating events e.g., consider simulating a small-town bank  1 teller, customers are served on a first-come-first-served basis  at any given minute of the day, there is a constant probability of a customer arriving  the length of a customer's transaction is a random int between 1 and some MAX What is the time duration (in minutes) to be simulated? 10 What percentage of the time (0-100) does a customer arrive? 30 2: Adding customer 1 (job length = 4) 2: Serving customer 1 (finish at 6) 4: Adding customer 2 (job length = 3) 5: Adding customer 3 (job length = 1) 6: Finished customer 1 6: Serving customer 2 (finish at 9) 9: Adding customer 4 (job length = 3) 9: Finished customer 2 9: Serving customer 3 (finish at 10) 10: Finished customer 3 10: Serving customer 4 (finish at 13) 13: Finished customer 4

7 OOP design of bank simulation what are the objects/actors involved in the bank simulation? Customer  get the customer's ID  get the customer's arrival time  get the customer's job length Teller  get the teller's ID  get the teller's status (busy? free? finished?)  start serving a customer  get information on the customer being served ServiceCenter  add a customer to the queue  check to see if any customers waiting  check to see if a customer is being served  perform the next step (serve customer if available)

8 Bank simulation classes class Customer { public: Customer(int id = 0, int arrTime = 0); int getID(); int getArrivalTime(); int getJobLength(); private: ??? }; given this general scheme, can design the interfaces for the classes will worry about the data fields & implementation details later enum Status { FREE, BUSY, FINISHED }; class Teller { public: Teller(int id = 0); int getID(); Status getStatus(int time); void serveCustomer(Customer c, int time); Customer getCurrentCustomer(int time); private: ??? }; class ServiceCenter { public: ServiceCenter(); void addCustomer(); bool customersWaiting(); bool customersBeingServed(); void serveCustomers(); private: ??? };

9 Banksim.cpp #include "ServiceCenter.h" #include "Die.h" bool CustomerArrived(int arrivalProb); void main() { int maxTime, arrivalProb; cout << "What is the time duration (in minutes) to be simulated? "; cin >> maxTime; cout << "What percentage of the time (0-100) does a customer arrive? "; cin >> arrivalProb; cout << endl; ServiceCenter bank; for (int time = 1; time <= maxTime || bank.customersBeingServed() || bank.customersWaiting(); time++) { if ( time <= maxTime && CustomerArrived(arrivalProb) ) { bank.addCustomer(); } bank.serveCustomers(); } ////////////////////////////////////////////////////////////////////// bool CustomerArrived(int arrivalProb) // Assumes: 0 <= arrivalProb <= 100 // Returns: true with probability ARRIVAL_PROB, else false { Die d(100); return (d.Roll() <= arrivalProb); }

10 Customer class class Customer { public: Customer(int id = 0, int arrTime = 0); int getID(); int getArrivalTime(); int getJobLength(); private: static const int MAX_LENGTH = 8; int customerID; int arrivalTime; int jobLength; }; what data fields are needed to implement the member functions? let's do it! how do we test the class?  don't wait until all the classes are implemented and test them all together #include #include "Customer.h" using namespace std; void main() { for (int i = 0; i < 10; i++) { Customer c(100+i, i); cout << "ID: " << c.getID() << endl << "arrival time: " << c.getArrivalTime() << endl << "job length: " << c.getJobLength() << endl << endl; } return 0; }

11 Teller class enum Status { FREE, BUSY, FINISHED }; class Teller { public: Teller(int id = 0); int getID(); Status getStatus(int time); void serveCustomer(Customer c, int time); Customer getCurrentCustomer(int time); private: int tellerID; int busyUntil; Customer serving; }; what data fields are needed to implement the member functions? let's do it! testing? #include #include "Teller.h" using namespace std; void main() { Teller t(1234); cout << "ID: " << t.getID() << endl << "Status: " << t.getStatus(1) << " (0=FREE,1=BUSY,2=FINISHED)" << endl; Customer c(999, 3); t.serveCustomer(c, 5); for (int i = 5; i < 14; i++) { cout << "Status at time " << i << ": " << t.getStatus(i) << endl; } cout << "Customer at time 5: " << t.getCurrentCustomer(5).getID() << endl; return 0; }

12 ServiceCenter class class ServiceCenter { public: ServiceCenter(); void addCustomer(); bool customersWaiting(); bool customersBeingServed(); void serveCustomers(); private: queue waiting; Teller loneTeller; int currentTime; int numEntered; }; what data fields are needed to implement the member functions? HW5: implement this class  constructor initializes the data fields  addCustomer adds a new customer to the waiting queue (& display a message) customer ID is # entered (1 st customer = 1, 2 nd customer = 2, …) arrival time is the current time, as maintained by the class  customerWaiting returns true if any customers waiting in queue (not being served)  customerBeingServed returns true if a customer is currently being served  serveCustomers does one step in the simulation if the teller finishes with a customer, remove them (& display a message) if the teller is free and there is a customer waiting, start serving them (& display a message) increment the current time