Chapter 3 Queues Content Points Definitions Definitions Implementations of Queues Implementations...

54
Chapter 3 Queues

Transcript of Chapter 3 Queues Content Points Definitions Definitions Implementations of Queues Implementations...

Page 1: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Chapter 3 Queues

Page 2: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Content Points

Definitions

Implementations of Queues

Circular Implementation of Queues in C++

Demonstration and Testing

Application of Queues:Simulation

Page 3: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

A queue is a data structure modeled after a line of people waiting to be served. Along with stacks, queues are one of the simplest kinds of data structures. This chapter develops properties of queues, studies how they are applied, and examines different implementations. The implementations illustrate the use of derived classes in C++ and the important object-oriented technique of class inheritance.

队列是一个模拟日常生活中人们排队等候服务的数据结构。与栈一样,队列是最简单的数据结构。本章描述了队列的性质、应用及不同的实现方法。描述了 C++ 中派生类的使用和类的继承中重要的面向对象技术。

Page 4: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Definitions

Queue : we similarly define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.

Queues are also called first-in, first-out lists, or FIFO (先进先出表) for short.

Applications: a computer system ,queues of tasks waiting for printer,disk storage,with multitasking ,use of the CPU.

Front: the first entry in the queuerear: the last entry in the queue

2

3

4

1

1

1

2

2

front (head)

rear (tail)

Page 5: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Queue Operations(队列的基本操作)

As in our treatment of stacks, we shall implement queues whose entries have a generic type, which we call Queue_entry.

Queue :: Queue( );postcondition: The Queue has been created and is initialized to be empty.

Page 6: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Error_code Queue :: append(const Queue_entry &x);postcondition: If there is space, x is added to the Queue as its

rear. Otherwise an Error_code of overflow is returned.

Error_code Queue :: serve( );postcondition: If the Queue is not empty, the front of the Queue has been removed.Otherwise an Error_code of underflow is returned.

Queue Operations(队列的基本操作)

Page 7: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Error_code Queue :: retrieve(Queue_entry &x) const;postcondition: If the Queue is not empty, the front of the Qu

eue has been recorded as x. Otherwise an Error_code of underflow is returned. (取队头元素)

bool Queue :: empty( ) const;postcondition: Return true if the Queue is empty, otherwise return false.

Queue Operations(队列的基本操作)

Page 8: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

class Queue {

public:

Queue( );

bool empty( ) const;

Error_code append(const Queue_entry &x);

Error_code serve( );

Error_code retrieve(Queue_entry &x) const;

// Additional members will represent queue data.

};

Queue Operations(队列的基本操作)

Remove front or underflowRecord front as x

or underflow

pushpopfront

Add x to rear or overflow

Page 9: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Extended Queue OperationsExtended Queue Operations:

full clear size serve_and_retrieve

class Queue

methods: Queue (constructor) append serve retrieve emptydata members

class Queue class Extended_Queue

methods: Extended_queue (constructor) append serve retrieve empty full clear size serve_and_retrievedata membersadditional data members

inheritance

Base class

Derived class

Page 10: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Extended Queue Operations

class Extended_queue: public Queue {

public:

bool full( ) const;

int size( ) const;

void clear( );

Error_code serve_and_retrieve(Queue_entry &item);

};

Page 11: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Extended Queue Operations

The new operations for our class Extended_queue have the following specifications.

bool Extended_queue :: full( ) const;postcondition: Return true if the Extended_queue is full; return false otherwise.

void Extended_queue :: clear( );postcondition: All entries in the Extended_queue have been removed; it is now empty.

Page 12: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Extended Queue Operationsint Extended_queue :: size( ) const;postcondition: Return the number of entries in the Extended_queue.

Error_code Extended_queue :: serve_and_retrieve(Queue_entry &item);postcondition: Return underflow if the Extended_queue is empty. Otherwise remove and copy the item at the front of the Extended_queue to item and return success.

Page 13: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Extended Queue Operations

Every A “is a” B

class A is derived from class B

Every A “has a” B

class A has a data member of type B

The relationship between the class Extended_queue and the class Queue is often called an is-a relationship. This is because every Extended_queue object “is a” Queue object with other features—namely, the methods serve_and_retrieve, full,size, and clear.

Page 14: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

As another illustration of the is-a relationship between classes, consider C++ classes that might be used in a program to manage a university budget. Some of these classes are University, Student, University_president, and Person. Every student is a person, and therefore we might create the class Student as derived from the class Person to reflect the is-a relationship between the corresponding concepts. The class University_president could also be implemented as a derived class of Person to reflect another obvious is-a relationship.

The classes University and University_president do not reflect an is-a relationship, however the classes are related, because every university does have a president. We shall say that these classes reflect a has-a relationship, and in an implementation we would make this relationship clear by layering the classes, that is, by including a data member of type University_president in the definition of the class University.

Page 15: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Home work

P84 exercise E2 E3(b)(e)

Page 16: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES(队列的实现)

.The Physical Model (物理模型)

One strategy (策略) would be to keep the front of the queue always in the first location of the array.

Then an entry could be appended to the queue simply by increasing the counter showing the rear, in exactly the same way as we added an entry to a stack. To remove an entry from the queue,however, would be very expensive indeed.

Page 17: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES(队列的实现)

.

Linear Implementation (队列的顺序实现) To append an entry to the queue, we simply increase the rear

by one and put the entry in that position. To serve an entry, we take it from the position at the front and then increase the front by one.

Job 1 Job 2 Job 3 Job 4 Job 5 Job 6 Job 7

1 0 1 2 3 4 5 6

append Job 1 append Job 2 append Job 3

serve Job 1 append Job 4 append Job 5

append Job 6 serve Job 2 append Job 7

append Job 8

Page 18: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

fault:discarded space•This method has a major defect (缺点) : Both the front and rear indices are increased but never decreased. as the queue moves down the array, the storage space at the beginning of the array is discarded (丢弃) and never used again.•Advantage:for applications where the queue is regularly emptied(such as when a series of requests is allowed to build up to a certain point, and then a task is initiated that clears all the requests before returning), then at a time when the queue is empty, the front and rear can both be reset to the beginning of the array, and the simple scheme of using two indices and straight-line storage becomes a very efficient implementation.

Page 19: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES(队列的实现)

. Circular Arrays (顺序循环队列)

In concept, we can overcome the inefficient use of space simply by thinking of the array as a circle rather than a straight line.

At different times, the queue will occupy different parts of the array, but we never need worry about running out of space unless the array is fully occupied,in which case we truly have overflow.

Page 20: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Page 21: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

[ 5 ]

[ 4]

[ 3 ][ 2 ]

[ 1 ]

[ 0 ]

[ 5 ]

[ 4]

[ 3 ][ 2 ]

[ 1 ]

[ 0 ]

Page 22: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Implementation of Circular Arrays (循环队列的实现)

Our next problem is to implement a circular array as an ordinary linear (that is, straight-line) array. To do so, we think of the positions around the circle as numbered from 0 to max - 1, where max is the total number of entries in the circular array, and to implement the circular array, we use the same numbered entries of a linear array. Then moving the indices is just the same as doing modular arithmetic

When we increase an index past max - 1,we start over again at 0. This is like doing arithmetic on a circular clock face; the hours are numbered from 1 to 12, and if we add four hours to ten o’clock, we obtain two o’clock.

Page 23: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Circular Arrays in C++

In C++, we can increase an index i of a circular array by using the ternary operator ? : and writing

i = ((i + 1) == max) ? 0 : (i + 1);

Or we can use the modulus operator and write

i = (i + 1) % max

You should check to verify that the result of the latter expression is always between 0 and max-1.

Page 24: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Boundary Conditions (边界条件)

存在问题 : 如何区别队空和队满?

Page 25: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Possible Solutions (解决方法)empty position (少用一个空间)

One is to insist on leaving one empty position in the array, so that the queue is considered full when the rear index has moved within two positions of the front.

flag (设定标志位) A second method is to introduce a new variable. This can be a Bool

ean flag that is set as true when the rear comes just before the front to indicate that the queue is full or an integer variable that counts the number of entries in the queue.

special values (采用特殊值) The third method is to set one or both of the indices to some value

(s) that would otherwise never occur in order to indicate an empty (or full) queue. For example, an empty queue could be indicated by setting the rear index to -1.

Page 26: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Summary of ImplementationsTo summarize the discussion of queues, let us list all the methods we have discussed for implementing queues.

The physical model: a linear array with the front always in the first position and all entries moved up the array whenever the front is removed. This is generally a poor method for use in computers. A linear array with two indices always

increasing. This is a good method if the queue can be emptied all at once. A circular array with front and rear indices and

one position left vacant.

Page 27: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

IMPLEMENTATIONS OF QUEUES

.

Summary of Implementations

A circular array with front and rear indices and a flag to indicate fullness (or emptiness).

A circular array with front and rear indices and an integer variable counting entries.

A circular array with front and rear indices taking special values to indicate emptiness.

Page 28: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++ (循环队列实现)

. The implementation in a circular array which uses a counter to keep track of the number of entries in the queue both illustrates techniques for handling circular arrays and simplifies the programming of some of the extended-queue operations. We shall take the queue as stored in an array indexed with the range

0 to (maxqueue - 1)

Page 29: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

.

const int maxqueue = 10; // small value for testing

class Queue {

public:

Queue( );

bool empty( ) const;

Error_code serve( );

Error_code append(const Queue_entry &item);

Error_code retrieve(Queue_entry &item) const;

protected:

int count;

int front, rear;

Queue_entry entry[maxqueue];

};

Page 30: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

.

Queue :: Queue( )

/* Post: The Queue is initialized to be empty. */

{count = 0;

rear = maxqueue - 1;

front = 0;

}bool Queue :: empty( ) const

/* Post: Return true if the Queue is empty, otherwise return false. */

{return count == 0;

}

Page 31: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

.

Error_code Queue :: append(const Queue_entry &item)

/* Post: item is added to the rear of the Queue. If the Queue is full return an Error_code of overflow and leave the Queue unchanged. */

{

if (count >= maxqueue) return overflow;

count++;

rear = ((rear + 1) == maxqueue) ? 0 : (rear + 1);

entry[rear] = item;

return success;

}

Page 32: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

.

Error_code Queue :: serve( )

/* Post: The front of the Queue is removed. If the Queue is empty return an Error_code of underflow. */

{

if (count <= 0) return underflow;

count--;

front = ((front + 1) == maxqueue) ? 0 : (front + 1);

return success;

}

Page 33: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

Error_code Queue :: retrieve(Queue_entry &item) const/* Post: The front of the Queue retrieved to the output parameter item. If the Queue is empty return an Error_code of underflow. */{if (count <= 0) return underflow;item = entry[front];return success;}

int Extended_queue :: size( ) const/* Post: Return the number of entries in the Extended_queue. */{return count;}

Page 34: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTING Menu-driven demonstration programint main( )

/* Post: Accepts commands from user as a menu-driven demonstration program for the class Extended_queue.

Uses: The class Extended_queue and the functions introduction, get_command,and do_command. */

{

Extended_queue test_queue;

introduction( );

while (do_command(get_command( ), test_queue));

}

Page 35: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTING Menu-driven demonstration program

Page 36: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTING

Menu-driven demonstration program

Page 37: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTINGvoid help( )

/* Post: A help screen for the program is printed, giving the meaning of each command that the user may enter. */

{

cout << endl

<< "This program allows the user to enter one command" << endl

<< "(but only one) on each input line." << endl

<< "For example, if the command S is entered, then" << endl

<< "the program will serve the front of the queue." << endl

<< endl

<< " The valid commands are:" << endl

<< "A - Append the next input character to the extended queue" << endl

Page 38: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTING<< "S - Serve the front of the extended queue" << endl

<< "R - Retrieve and print the front entry." << endl

<< "# - The current size of the extended queue" << endl

<< "C - Clear the extended queue (same as delete)" << endl

<< "P - Print the extended queue" << endl

<< "H - This help screen" << endl

<< "Q - Quit" << endl

<< "Press < Enter > to continue." << flush;

char c;

do {

cin.get(c);

} while (c != ‘\n’);

}

Page 39: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTING

bool do_command(char c, Extended_queue &test_queue)

/* Pre: c represents a valid command.

Post: Performs the given command c on the Extended_queue test_queue. Returns false if c == ‘q’, otherwise returns true.

Uses: The class Extended_queue. */

{

bool continue_input = true;

Queue_entry x;

Page 40: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

DEMONSTRATION AND TESTINGswitch (c) {case ’ r’:if (test_queue.retrieve(x) == underflow)cout << "Queue is empty." << endl;elsecout << endl<< "The first entry is: " << x<< endl;break;case ’q’:cout << "Extended queue demonstration finished." << endl;continue_input = false;break;// Additional cases will cover other commands.}return continue_input;}

Page 41: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

1 、 A circular queue has the problem in which it is not easy to distinguish between full and empty queues.  Draw two situations to illustrate this point.  The front and rear pointers should be in the same position in each situation.

2 、 Evaluate the following sentence if it is true or false and simply explain why?A queue is a FILO data structure. An array based queue implementation is usually implemented as a circular queue. An array based queue is better than a linked list implementation of a queue.

Page 42: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: SIMULATION

Introduction Simulation is the use of one system to imitate the behavior of an

other system.Simulations are often used when it would be too expensive or dangerous to experiment with the real system. 仿真是指用一个系统模拟另一个系统的行为。经常使用于实际系统比较昂贵或危险的情形。

Such as :wind tunnel 、 flight simulators

computer simulation: uses the steps of a program to imitate the behavior of the system under study..

Page 43: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

In a computer simulation, the objects being studied are usually represented as data, often as data structures given by classes whose members describe the properties of the objects.Actions being studied are represented as methods of the classes.And the rules describing these actions are translated into computer algorithms. By changing the values of the data or by modifying these algorithms,we can observe the changes in the computer simulation, and then we can draw worthwhile inferences concerning the behavior of the actual system.( 可以得到关于实际系统的有价值的结论 )

APPLICATION OF QUEUES: SIMULATION

Page 44: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.
Page 45: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

CIRCULAR IMPLEMENTATION OF QUEUES IN C++

The same runway is used for both landings and takeoffs. (跑道可以用来降落,也可以起飞。)

One plane can land or take off in a unit of time, but not both. (在单位时间内,一辆飞机可以起飞或降落。)

A random number of planes arrive in each time unit.(每单位时间内,到达的飞机数目是随机的。

A plane waiting to land goes before one waiting to take off. (降落优先)

The planes that are waiting are kept in queues landing and takeoff, both of which have a strictly limited size. (等待起飞或降落的飞机数目是有限制的)

Page 46: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: IMULATION

class Plane (飞机类)In simulating the airport, it will be useful to create a class Plane whose objects represent individual planes. This class will definitely need an initialization method and methods to represent takeoff and landing. Moreover, when we write the main program for the simulation, the need for other Plane methods will become apparent. class Runway (跑道类)We will also use a class Runway to hold information about the state and operation of the runway. This class will maintain members representing queues of planes waiting to land and take off. class Random (随机数类)We shall need one other class in our simulation, a class Random to encapsulate the random nature of plane arrivals and departures from the runway. We shall discuss this class in more detail in Section 3.5.3.

Page 47: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: SIMULATION

int main( ) // Airport simulation program

/* Pre: The user must supply the number of time intervals the simulation is to run,the expected number of planes arriving, the expected number of planes departing per time interval, and the maximum allowed size for runway queues.

Post: The program performs a random simulation of the airport, showing the status of the runway at each time interval, and prints out a summary of airport operation at the conclusion.

Uses: Classes Runway, Plane, Random and functions run_idle, initialize. */

Page 48: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: SIMULATION

{int end_time; // time to run simulation

int queue_limit; // size of Runway queues

int flight_number = 0;

double arrival_rate, departure_rate;

initialize(end_time, queue_limit, arrival_rate, departure_rate);

Random variable;

Runway small_airport(queue_limit);

for (int current_time = 0; current_time < end_time; current_time++) {

// loop over time intervals

int number_arrivals = variable.poisson(arrival_rate);

// current arrival requests

Page 49: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: SIMULATION

for (int i = 0; i < number_arrivals; i++) {

Plane current_plane(flight_number++, current_time, arriving);

if (small_airport.can_land(current_plane) != success)

current_plane.refuse( );

}

int number_departures = variable.poisson(departure_rate);

// current departure requests

for (int j = 0; j < number_departures; j++) {

Plane current_plane(flight_number++, current_time, departing);

if (small_airport.can_depart(current_plane) != success)

current_plane.refuse( );

}

Page 50: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

APPLICATION OF QUEUES: IMULATION

Plane moving_plane;switch (small_airport.activity(current_time, moving_plane)) {// Let at most one Plane onto the Runway at current_time.case land:

moving_plane.land(current_time);break;

case takeoff:moving_plane.fly(current_time);break;

case idle:run_idle(current_time);

}}small_airport.shut_down(end_time);}

Page 51: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

enum Runway_activity {idle, land, takeoff};class Runway {public:

Runway(int limit);Error_code can_land(const Plane &current);Error_code can_depart(const Plane &current);Runway_activity activity(int time, Plane &moving);void shut_down(int time) const;

private:Extended_queue landing;Extended_queue takeoff;int queue_limit;int num_land_requests; // number of planes asking to landint num_takeoff_requests; // number of planes asking to take offint num_landings; // number of planes that have landedint num_takeoffs; // number of planes that have taken offint num_land_accepted; // number of planes queued to landint num_takeoff_accepted; // number of planes queued to take offint num_land_refused; // number of landing planes refusedint num_takeoff_refused; // number of departing planes refusedint land_wait; // total time of planes waiting to landint takeoff_wait; // total time of planes waiting to take offint idle_time; // total time runway is idle

};

Page 52: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

enum Plane_status {null, arriving, departing};class Plane {public:

Plane( );Plane(int flt, int time, Plane_status status);void refuse( ) const;void land(int time) const;void fly(int time) const;int started( ) const;

private:int flt_num;int clock_start;Plane_status state;

};

Page 53: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

Home work

P91 E5 E6 E8

Page 54: Chapter 3 Queues Content Points  Definitions Definitions  Implementations of Queues Implementations of Queues  Circular Implementation of Queues in.

POINTERS AND PITFALLS Before choosing implementations, be sure that all the data structures and their associated operations are fully specified on the abstract level. In choosing between implementations, consider the necessary operations on the data structure. If every object of class A has all the properties of an object of class B, implement class A as a derived class of B. Consider the requirements of derived classes when declaring the members of a base class. Implement is-a relationships between classes by using public inheritance. Implement has-a relationships between classes by layering. Use Poisson random variables to model random event occurrences.