Download presentation
Presentation is loading. Please wait.
Published byIris Hodges Modified over 9 years ago
1
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
2
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)
3
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()
4
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()
5
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
6
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
7
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() {
8
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
9
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
10
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
11
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 00-99 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
12
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah The Radix Sort: Pass 0 Initial Sequence: 91 6 85 15 92 35 30 22 39 Distribute the cards into bins according to the ones digit (10^0) Sequence after Pass 0: 30 91 92 22 85 15 35 6 39
13
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: 30 91 92 22 85 15 35 6 39 Distribute the cards into bins according to the ones digit (10^1) Sequence after Pass 1: 6 15 22 30 35 39 85 91 92
14
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
15
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]);
16
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 }
17
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
18
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)
19
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
20
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
21
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Implementing the miniQueue Class (implementation using list object)
22
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 };
23
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); }
24
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( ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.