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.

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
CSE Lecture 12 – Linked Lists …
Chapter 6 Queues and Deques.
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.
Queues Briana B. Morrison Adapted from Alan Eugenio.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
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.
Stacks.
More on the STL vector list stack queue priority_queue.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Object Oriented Data Structures
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
C++ STL CSCI 3110.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS342 Data Structures End-of-semester Review S2002.
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.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Data Structures: CSCI Chapter 1 Data Structures: CSCI Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
CS505 Data Structures and Algorithms
Data Structure Interview Question and Answers
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Briana B. Morrison Adapted from Alan Eugenio
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
Lab4 problems More about templates Some STL
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

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 Sort  Efficiency of the Radix Sort  The miniQueue Class

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Introduction  Queue: sequential storage structure that permits access only at two ends of the sequence  A queue inserts elements at the back and removes elements from the front  Follows FIFO/ FCFS order: items are retrieved in order of their appearance  Applications:  Process/CPU scheduling – FCFS, RR  Sorting Algorithms  Simulations studies involve queues (study of discrete evens in a system over a time interval)

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah The Queue ADT  Queue ADT provides essentially the same interface as does the stack  This is consistent with STL’s philosophy of using a common interface for its classes  Note the similar ADT operations in Queue and Stacks  push()  pop()  size()  The abstract concept of a queue allows for an arbitrarily large sequence of data, hence the push() operation has no preconditions  The same is not the case for the function pop() and front() which assumes that the queue is not empty and has at least one element 1 st 4 th 3 rd 2 nd last FrontBack …………………… Removes/ pop()Inserts/ push()

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Class Queue: Constructor and Operations  queue();  // create an empty queue  bool empty() const;  // return a reference to the value of the item at the front of the queue  // precondition: the queue is not empty  T& front()  // constant version of front()  const T& front() const;  // constant version of front()

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Class Queue: Constructor and Operations  void pop();  // remove the item from the front of the queue  // precondition: the queue is not empty  // postcondition: the element at the front of the queue is the element that had been added  // immediately after the element that has just been popped, or the queue is empty  void push(const T&item);  // insert the argument item at the back of the queue  // postcondition: the queue has a new item at the back  int size() const;  // return the number of elements in the queue

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Queue Push and Pop Operations A BA C BA C B C front back front back front back front back front back Push A Pop A Push C Push B Pop B

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Application: Scheduling Queuesschedule1.cpp // program outputs the interview schedule for a Personnel Director // the Executive Assistant constructs a queue of appointment times by // reading the times from the keyboard // cycling through the queue // EA outputs the time at which each appointment begins // and the available time duration of that interview #include // declares the predefined parameterized manipulators and provides macros for user-defined parameterized manipulators #include #include “d_time24.h” //see pg 13 [ addTime(), duration(), readTime(), writeTime(), getHour(), getMinute()…] using namespace std; int main() {

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Application: Scheduling Queuesschedule1.cpp Time24 interviewTime; // queue to hold appointment time for job applicant queue apptQ; // create the queue cout<<“First interview of the day: ”; cin>>interviewTime; while (interviewTime<time24(17,0)) //constructing the queue until the input is 5:00 PM or later { apptQ.push(interviewTime); // push the interview time on the queue cout <<“Next interview: “; cin >>interviewTime; // prompt for the next interview time and read as “interviewTime” } 9:00 push interviewTime 9:00 push 10:15 interviewTime Construction

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Application: Scheduling Queuesschedule1.cpp cout <<endl <<“Appointment Available Interview Time”<< endl; // output the day’s appointment schedule // pop the next applicant appointment time // determine available time for interview by checking time for applicant // at front of queue while (!apptQ.empty()) { interviewTime = apptQ.front(); apptQ.pop(); //output available time // if queue is empty, the interview ends at 5:00 PM cout<<“ “<< interviewTime<<setw(17)<<“ “; if (apptQ.empty()) cout<<(time24(17,0) – interviewTime) << endl; else cout<<(apptQ.front() – interviewTime) << endl; } return 0; } Output 9:00 cin/ read 10:15 difference/ duration pop interviewTime = apptQ.front() 16:30 pop 9:00 duration 10:15 – 9:00 1:15 16:30 Duration 17:00 – 16:30 0:30

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah  First interview of the day: 9:00  Next interview: 10:15  Next interview: 11:15  Next interview: 13:00  Next interview: 13:45  Next interview: 14:30  Next interview: 15:30  Next interview: 16:30  Next interview: 17:00  // 17:00 terminates input AppointmentAvailable Interview Time 9:001:15 Application: Scheduling Queuesschedule1.cpp Run NB: remember this for process scheduling in CSCI 380

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah The Radix Sort  Introduction:  Early days, data were stored on punched cards  To order data  An operator Ran the cards through a mechanical sorter  For integer data the machine dropped each card into one of 10 bins (0 – 9)  Each bin was a queue in which a card entered at the backa nd exited in the fron  This mechanical sorter implemented the radix sort algorithm  Assume the cards contain 2-digit numbers in the range  The numbers (cards) pass through the machine twice to separate the data – first by the ones digit and then by the tens digit  Each pass involves first distributing the cards into the bins and then collecting tem back into a sequence

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah The Radix Sort: Pass 0  Initial Sequence:  Distribute the cards into bins according to the ones digit (10^0)  Sequence after Pass 0:

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah The Radix Sort: Pass 1  Sequence after Pass 0:  Distribute the cards into bins according to the ones digit (10^1)  Sequence after Pass 1:

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Radix Sort Algorithm Pass 0: sorts ones digit (power 10^0) Pass 1: sorts tens digit (power 10^1) Pass 2: sorts hundreds digit (power 10^2) 3 main functions radixSort(vector & v, int d); distribute (const vector & v, queue digitQueue[ ], int power) collect(queue digitQueue[], vector,int.& v) queue digitQueue[10]; // an array of 10 queues simulates the sorting bins 0 to 9

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Radix Sort Algorithm: distribute( ) void distribute (const vector & v, queue digitQueue[ ], int power) { // the function implements the distribution of the numbers into 10 queues // include vector of elements // include queue containers // include the power that designates the queue int i; for (i=0; i <v.size(); i++) digitQueue[(v[i] / power)%10].push(v[i]); // loop through the vector, inserting each element into the queue (v[i]/ power)% 10 } Identifying the queue into which the digit needs to be pushed Example: 56343/1000 = 5656 % 10 = 6 digitQueue [ (v[i] / power) % 10].push(v[i]);

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Radix Sort Algorithm: collect( ) void collect(queue digitQueue[], vector,int.& v) // this function scans the array of queues in the order 0 to 9 // then gathers elements from the queue and copies back to the vector { int i = 0, digit; // scan the vector of queue using indices 0, 1, 2, etc, for (digit = 0; digit < 10; digit ++) while (!digitQueue[digit].empty( )) { v[i] = digitQueue[digit].front(); digitiQueue[digit].pop( ); i++ // collect items until queue is empty // then copy items back to the vector }

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Radix Sort Algorithm: radixSort( ) void radixSort(vector &v, int d) // this function calls distribute(), followed by collect(), for power = 1, 10,…10^d // implements the algorithm // sorts vector v using the radix sort // performs d iterations for each digit in the integer { int i; int power = 1; queue digitQueue[10]; for (i = 0;i<d;i++) { distribute (v, digitQueue, power); collect(digitQueue, v); power * = 10; } Next Steps: place the functions: radixSort(), distribute(), collect() into a the header file “d_sort.h” See Program 8-2 on page 393 for the.cpp portion

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Queue Implementations compared to Stack Implementations Stacks can be Implemented by: Vectors (miniStack) Linked Lists Queues can be Implemented by: Vectors (radixSort) Linked Lists (miniQueue)

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Implementing the miniQueue Class (implementation using list object)  Compare to miniStack example where the member functions such as size( ) and empty( ) of a miniStack object actually reflect the status of the underlying vector. To access or modify the top of the stack or to add and remove an element at the top, the miniStack class uses the functions back( ), push_back( ) and pop_back ( ) from the vector class  A similar approach is used in implementing the miniQueue Class – however instead of using a vector to store the data, we use a list object. Why??  while a vector has operations that access the back of the sequence, but it does not efficiently remove an element from the front  The list class operations prove to be an ideal interface for miniQueue class because its functions:  front( ) accesses the front of the sequence  pop_front( ) efficiently removes it  push_back( ) efficiently inserts an element at the back of the sequence

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Implementing the miniQueue Class (implementation using list object)  think of the elements of the queue as organized horizontally in a waiting line  initially the queue is empty and the size of the list is 0  we can add items to the back of the list (push_back( )), which increased the size by 1  we can also remove an item from the front of the list (pop_front( )), which decreases the size by 1  at all times we know the element at the front of the list (front( )) and  at all times we know the size of the queue and whether it is empty, by extracting this information fro m the underlying list object

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Implementing the miniQueue Class (implementation using list object)

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah CLASS miniQueuePartial Declaration“d_queue.h” Template Class miniQueue { public; miniQueue( ); //constructor; create an empty queue …………………….. // member functions push( ), pop( ), front( ), size( ), empty( ) private: list qlist; // a list object maintains the queue items and size };

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah CLASS miniQueuePush( ) // insert item into the queue by inserting it at // the back of the list template void miniQueue : :push(const T& item) { qlist.push_back(item); }

Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah CLASS miniQueuePop( ) // remove the element front the front of the queue // pop( ) and front( ) require the additional logic to test the “queue empty” condition // if the condition is true, the functions throw the underflowError exception specified in the header file “d_except.h” // front( ) uses the corresponding list function template void miniQueue : :pop( ) { // if queue is empty, throw uderflowError if (q.list.size( ) = = 0) throw underflowError(“miniQueue pop( ): empty queue”); //erase the front qlist.pop_front( ); }