the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

Similar documents
Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Queue Implementations

Priority Queues and Huffman Trees

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Quadratic Sorting Algorithms

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Templates and Vectors

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Abstract Data Types 1

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Computer Programming

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

CSCE 110 PROGRAMMING FUNDAMENTALS

Abstract Data Types 1

Topics. bool and string types input/output library functions comments memory allocation templates classes

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Lab 8 The Stack (LIFO) Structure

STL: C++ Standard Library

Expression Trees and the Heap

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Welcome to MCS 360. content expectations. using g++ input and output streams the namespace std. Euclid s algorithm the while and do-while statements

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

COEN244: Class & function templates

Data Structures. Lecture 5 : The Queues

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Abstract Data Types. Stack ADT

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CHAPTER 5. QUEUE v2 by

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Why Is Repetition Needed?

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Stacks and Queues. CSE 373 Data Structures Lecture 6

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

Computer Programming

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Programming Abstractions

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Lab Instructor : Jean Lai

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Stack Applications. parsing a postfix expression evaluating postfix expressions. converting infix sums from infix to postfix

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Programming Language. Functions. Eng. Anis Nazer First Semester

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Local and Global Variables

Concurrent Data Structures in C++ CSInParallel Project

Linked List using a Sentinel

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

Unified Modeling Language a case study

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

Data Structures. Lecture 4 : The Queues. Dr. Essam Halim Houssein Lecturer, Faculty of Computers and Informatics, Benha University

CSI33 Data Structures

Chapter 10 - Notes Applications of Arrays

G52CPP C++ Programming Lecture 18

Computer Programming

Chapter 16: Graphs and Digraphs

CSCE 110 PROGRAMMING FUNDAMENTALS

1. The term STL stands for?

Chapter 7: Stacks. Exercises 7.2

Module 9. Templates & STL

Introduction to the C programming language

Introduction to the C programming language

Due Date: See Blackboard

CS 106B Lecture 5: Stacks and Queues

Computer Science II Lecture 2 Strings, Vectors and Recursion

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Due Date: See Blackboard

EECE.3220: Data Structures Spring 2017

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Unit 6. Thinking with Functions

Name SECTION: 12:45 2:20. True or False (12 Points)

1 Unit 8 'for' Loops

The Stack and Queue Types

C++ Exception Handling 1

Data Abstraction and Specification of ADTs

Data Structures and Algorithms

Partha Sarathi Mandal

Lecture 7. Log into Linux New documents posted to course webpage

STACKS AND QUEUES. Problem Solving with Computers-II

Transcription:

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue MCS 360 Lecture 16 Introduction to Data Structures Jan Verschelde, 4 October 2017 Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 1 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 2 / 28

queue A stack is a FIFO (First In First Out) sequence: pop: remove the first element, push: append last element. Main applications: fair protocol to share resources, run simulations. A queue can be linear or circular. Applications for circular queues: waiting room with fixed #seats, load balancing, round-robin scheduling. Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 3 / 28

queue ADT abstract <typename T> queue; /* a queue is a sequence of elements, FIFO = First In First Out */ abstract bool empty ( queue q ); postcondition: empty(s) == true if queue is empty, == false otherwise; abstract T pop ( queue q ); precondition: not empty(q); postcondition: first element is removed from q; abstract void push ( queue q, elements e ); postcondition: element e is at end of queue q; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 4 / 28

the STL queue #include <queue> The queue is a templated class, we can specify the typetof the elements of the queue at instantiation, e.g.: queue<string> q; declares a queue of strings. Operations onqfortof typet: q.push(t) : t = q.front() : q.pop() : q.empty() : q.size() : appendst to the end ofq returnstat front ofq removes first element of q true ifqis empty returns number of elements ofq Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 5 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 6 / 28

using the STL queue #include <iostream> #include <string> #include <queue> using namespace std; int main() { queue<string> q; cout << "pushing A, B, and C..." << endl; q.push("a"); q.push("b"); q.push("c"); cout << "size of queue : " << q.size() << endl; cout << "front of queue : " << q.front() << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 7 / 28

printing a queue cout << "popping front..." << endl; q.pop(); for( ;!q.empty(); q.pop()) cout << "front of queue : " << q.front() << endl; if(q.empty()) cout << "queue is empty" << endl; else cout << "queue is not empty" << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 8 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 9 / 28

Simulating a Printer Queue Consider a sequence of printer jobs. Assume every job takes between 1 and 10 pages; arrival time between jobs is in range 10..20 minutes. For simplicity, we use uniform distributions. If printer has a capacity of 100 pages, how many times we need to reload for 100 jobs? Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 10 / 28

Our Simulation Algorithm Two major subroutines: 1 generate n jobs, 2 process the jobs. The user is prompted to provide n. Parameters are set with#define. For each job we store two integers: size: number of pages to print, arrival: elapsed time in minutes. We use astruct to define a job. Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 11 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 12 / 28

preprocessor directives #include <iostream> #include <ctime> #include <cstdlib> #include <queue> using namespace std; // parameters for the simulation // 1. size of the jobs in [1,10] #define min_size 1 #define max_size 10 // 2. arrival time between jobs #define min_time 10 #define max_time 20 // 3. capacity of printer #define capacity 100 Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 13 / 28

type and function declarations struct Job { int arrival; // arrival time in minutes int size; // size of job in pages }; int random_number ( int a, int b ); // returns a random integer number, // uniformly distributed between a and b queue<job> generate_jobs ( int n ); // returns a queue of n jobs void process_jobs ( queue<job> q ); // runs the simulation, processing jobs in q Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 14 / 28

the main program int main() { int n; cout << "Give number of jobs : "; cin >> n; srand(time(0)); queue<job> q = generate_jobs(n); process_jobs(q); } return 0; int random_number ( int a, int b ) { int r = rand() % (b-a+1); return a + r; } Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 15 / 28

generating jobs queue<job> generate_jobs ( int n ) { queue<job> q; int elapsed = 0; for(int i=0; i<n; i++) { Job J; J.size = random_number(min_size,max_size); cout << "job " << i << " has size " << J.size; J.arrival = elapsed + random_number(min_time,max_time); cout << " arrived at time " << J.arrival << endl; elapsed = J.arrival; q.push(j); } return q; } Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 16 / 28

processing jobs void process_jobs ( queue<job> q ) { int printed = 0; int packs = 0; for(int i=0;!q.empty(); q.pop(), i++) { Job J = q.front(); cout << "job " << i << " arrived at " << J.arrival << " has size " << J.size << endl; printed = printed + J.size; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 17 / 28

pause to reload... if(printed > capacity) { cout << "please provide paper...\n"; cout << "continue? (y/n) "; char c; cin >> c; if(c!= y ) break; } printed = printed - capacity; packs = packs + 1; } } cout << "printed " << packs*capacity + printed << " pages" << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 18 / 28

collecting statistics Withsrand(time(0)), the answer will differ at each run. Running N times, answers a k, k = 1, 2,...,N, compute average ā = 1 N a k ; N k=1 standard deviation: d = N (a k µ) 2. Law of large numbers, for N : ā µ and d σ, where µ and σ are the true average and standard deviation for the answer. k=1 Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 19 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 20 / 28

a dictionary We map list operations to queue operations. For anytof typet: queue<t> q list<t> L q.push(t) L.push_back(t) q.pop() L.pop_front() t = q.front() t = L.front() q.empty() L.empty() q.size() L.size() Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 21 / 28

use STL list as queue #include <iostream> #include <string> #include <list> using namespace std; int main() { list<string> q; cout << "pushing A, B, and C..." << endl; q.push_back("a"); q.push_back("b"); q.push_back("c"); cout << "size of queue : " << q.size() << endl; cout << "front of queue : " << q.front() << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 22 / 28

printing a queue cout << "popping front..." << endl; q.pop_front(); for( ;!q.empty(); q.pop_front()) cout << "front of queue : " << q.front() << endl; if(q.empty()) cout << "queue is empty" << endl; else cout << "queue is not empty" << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 23 / 28

the Queue 1 The Queue Abstract Data Type queue ADT using the STL queue 2 Simulating a Printer Queue designing the simulation simulation with STL queue 3 adapting STL list and vector using STL list as queue using STL vector as queue Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 24 / 28

a dictionary We map vector operations to queue operations. For anytof typet: queue<t> q q.push(t) q.pop() t = q.front() q.empty() q.size() vector<t> v v.push_back(t) v.erase(v.begin()) t = v.front() v.empty() v.size() Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 25 / 28

use STL vector as queue #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector<string> q; cout << "pushing A, B, and C..." << endl; q.push_back("a"); q.push_back("b"); q.push_back("c"); cout << "size of queue : " << q.size() << endl; cout << "front of queue : " << q.front() << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 26 / 28

printing a queue cout << "popping front..." << endl; q.erase(q.begin()); for( ;!q.empty(); q.erase(q.begin())) cout << "front of queue : " << q.front() << endl; if(q.empty()) cout << "queue is empty" << endl; else cout << "queue is not empty" << endl; Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 27 / 28

Summary + Exercises Started Chapter 6 on Queues, covered ADT and STL, illustrated with a basic simulation program. Exercises: 1 Use a stack to reverse the order of the elements in a queue of strings. Write an interactive program to test your reversal function. 2 Combine the use of stack and queue to test whether a string is a palindrome. 3 Extend theprinter_queue.cpp simulation to do multiple runs (for convenience of the user, toggle off the pause statement) and compute the average and standard deviation of the runs. Introduction to Data Structures (MCS 360) the Queue L-16 4 October 2017 28 / 28