Document 7508254

Download Report

Transcript Document 7508254

CS 362: Queues
Overview of Lecture

Introduction

The Queue ADT

The Radix Sort

Efficiency of the Radix Sort

The miniQueue Class
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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:



Dr. Nazli Mollah
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
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
Removes/ pop()
1st
Front
Dr. Nazli Mollah
Inserts/ push()
2nd
3rd
4th
……………………
last
Back
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Class Queue: Constructor and Operations

queue();



// return a reference to the value of the item at the front of the queue
// precondition: the queue is not empty
T& front()


// create an empty queue
bool empty() const;


<queue>
// constant version of front()
const T& front() const;

Dr. Nazli Mollah
// constant version of front()
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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);



<queue>
// insert the argument item at the back of the queue
// postcondition: the queue has a new item at the back
int size() const;

Dr. Nazli Mollah
// return the number of elements in the queue
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Queue Push and Pop Operations
Push A
A
front
back
A
B
front
back
A
B
C
Push C
back
front
B
C
front
back
C
Push B
Pop A
Pop B
front
back
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Application: Scheduling Queues
schedule1.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 <iostream>
#include <iomanip>
// declares the predefined parameterized manipulators and provides macros for user-defined
parameterized manipulators
#include <queue>
#include “d_time24.h”
//see pg 13 [ addTime(), duration(), readTime(), writeTime(), getHour(), getMinute()…]
using namespace std;
int main()
{
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Application: Scheduling Queues
schedule1.cpp
Construction
Time24 interviewTime;
// queue to hold appointment time for job applicant
queue<time24> 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
{
push
apptQ.push(interviewTime);
9:00
// push the interview time on the queue
interviewTime
cout <<“Next interview: “;
cin >>interviewTime;
push
// prompt for the next interview time and read as “interviewTime”
}
9:00
10:15
interviewTime
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Application: Scheduling Queues
cout <<endl <<“Appointment
Output
schedule1.cpp
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();
pop
cin/ read
9:00
10:15
difference/ duration
pop
16:30
interviewTime = apptQ.front()
//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;
Duration
17:00 – 16:30
16:30
0:30
duration
10:15 – 9:00
else
cout<<(apptQ.front() – interviewTime) << endl;
9:00
1:15
}
return 0;
}
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Application: Scheduling Queues
schedule1.cpp
Run

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

Appointment
9:00
Available Interview Time
1:15
// 17:00 terminates input
NB: remember this for process scheduling in CSCI 380
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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



Dr. Nazli Mollah
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
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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)


30
91
22
92
0
1
2
3
4
35
15
85
6
5
6
39
7
8
9
Sequence after Pass 0: 30 91 92 22 85 15 35 6 39
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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)
6
15
22
39
35
30
0
1
2
3

4
5
6
7
85
92
91
8
9
Sequence after Pass 1: 6 15 22 30 35 39 85 91 92
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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<int>& v, int d);
distribute (const vector<int>& v, queue<int> digitQueue[ ], int power)
collect(queue<int> digitQueue[], vector,int.& v)
queue<int> digitQueue[10];
// an array of 10 queues simulates the sorting bins 0 to 9
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Radix Sort Algorithm: distribute( )
void distribute (const vector<int>& v, queue<int> 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 = 56
56 % 10 = 6
digitQueue [ (v[i] / power) % 10].push(v[i]);
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Radix Sort Algorithm: collect( )
void collect(queue<int> 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
}
}
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Radix Sort Algorithm: radixSort( )
void radixSort(vector<int>&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<int>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
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Queue Implementations compared to Stack Implementations
Stacks can be Implemented by:
Vectors
(miniStack)
Linked Lists
Queues can be Implemented by:
Vectors
(radixSort)
Dr. Nazli Mollah
Linked Lists
(miniQueue)
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
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:



Dr. Nazli Mollah
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
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
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
Implementing the miniQueue Class (implementation using list object)
miniQueue<int> miniQ; // declare an empty queue
Queue Statement Queue
miniQ.push(10)
List Statement
qlist.push_back(10)
10
frontback
miniQ.push(25)
10
List
front back
qlist.push_back(25)
25
front back
miniQ.push(50)
10
25
front
50
qlist.push_back(50)
back
back
10
25
50
front
return qlist.front()
miniQ.pop()
qlist.pop_front()
50
25
10
front
n = miniQ.front() // n = 10
25
10
back
// return 10
25
50
front back
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
CLASS miniQueue
Partial Declaration
“d_queue.h”
Template<typename T>
Class miniQueue
{
public;
miniQueue( );
//constructor; create an empty queue
……………………..
// member functions push( ), pop( ), front( ), size( ), empty( )
private:
list<T> qlist;
// a list object maintains the queue items and size
};
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
CLASS miniQueue
Push( )
// insert item into the queue by inserting it at
// the back of the list
template<typename T>
void miniQueue<T>: :push(const T& item)
{
qlist.push_back(item);
}
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp
CS 362: Queues
CLASS miniQueue
Pop( )
// 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<typename T>
void miniQueue<T>: :pop( )
{
// if queue is empty, throw uderflowError
if (q.list.size( ) = = 0)
throw underflowError(“miniQueue pop( ): empty queue”);
//erase the front
qlist.pop_front( );
}
Dr. Nazli Mollah
Adapted from Data Structures with C++ using STL:
Ford, Topp