版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 3 QUEUES,1. Specifications for Queues,2. Implementation of Queues,3. Contiguous Queues in C+,4. Demonstration and Testing,5. Application: Airport Simulation,6. Pointers and Pitfalls,A Queue is a data structure in which insertions take place at one end and deletions take place at the opposite
2、 end.,3.1 Specifications for Queue,Characteristic: First In First Out,2,3,4,1,1,1,2,2,front,rear,Queue : Queue( ); Post: The Queue has been created and is initialized to be empty.,Error_code Queue : append(const Queue entry Post: If there is space, x is added to the Queue as its rear.Otherwise an Er
3、ror code of overflow is returned.,Error_code Queue : serve( ); Post: If the Queue is not empty, the front of the Queue has been removed. Otherwise an Error code of underflow is returned.,Error_code Queue:retrieve(Queue entry Post: If the Queue is not empty, the front of the Queue has been recorded a
4、s x. Otherwise an Error code of underflow is returned.,bool Queue : empty( ) const; Post: Return true if the Queue is empty, otherwise return false.,Extended Queues class Extended queue: public Queue public: bool full( ) const; int size( ) const; void clear( ); Error_code serve and retrieve(Queue en
5、try ,Exist what problem?,Circular Implementation of Queues,Boundary Conditions,3.2 Implementations of 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 deleted. A linear array with two indices always increas
6、ing. A circular array with front and rear indices and one position left vacant.,A circular array with front and rear indices and a Boolean flag to indicate fullness (or emptiness). A circular array with front and rear indices and an integer counter of entries. A circular array with front and rear in
7、dices taking special values to indicate emptiness.,3.3 Circular Implementation of Queues in C+,/顺序循环队列的模板类接口定义以及基本运算的实现代码 #include enum Error_code success, overflow, underflow, can_land, can_depart, fail ; const int maxqueue = 10; / small value for testing template class Queue public: Queue( ); bool
8、 empty( ) const return count=0; Error_code serve( ); Error_code append(const Queue_entry ,protected: int count; int front, rear; Queue_entry entrymaxqueue; friend class Runway; ; / 结束类Queue声明 template Queue:Queue( ) / Post: The Queue is initialized to be empty. count=0; rear=maxqueue-1; front=0; ,te
9、mplate Error_code Queue:append(const Queue_entry ,template Error_code Queue:serve( ) / Post: The front of the Queue is removed. If the Queue is / / empty return an underflow if (count=0) return underflow; count-; front = (front+1) = = maxqueue) ? 0: (front+1); return success; ,template Error_code Qu
10、eue :retrieve(Queue_entry ,3.4 Demonstration and Testing,Skip, ask everyones oneself to see the book P93,3.5 Application of Queues: Simulation of an Airport,Simulation is the use of one system to imitate the behavior of another system. A computer simulation is a program to imitate the behavior of th
11、e system under study.,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 queue
12、s landing and takeoff, both of which have a strictly limited size.,#include #include #include random.h #include Plane.H #include Runway.H void main( ) / Airport simulation program /* Pre: The user must supply the number of time intervals the simulation is to run, the expected number of planes arrivi
13、ng, 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:
14、 Classes Runway ,Plane , Random and functions run run_idle ,initialize . */, int end_time; /time to run simulation int queue_limit; /size of Runway queues int flight_number = 0; double arrival_rate, departure_rate; void initialize(int i+), Plane current_plane(flight_number+, current_time, arriving);
15、 if(small_airport.can_land(current_plane)!=success) current_plane.refuse( ); int number_departures = variable.poisson(departure_rate); for(int j=0; jnumber_departures; j+) Plane current_plane(flight_number+, current_time, departing); if(small_airport.can_depart(current_plane)!=success) current_plane
16、.refuse( ); ,Plane moving_plane; switch(small_airport.activity(current_time, moving_plane) case land: moving_plane.land(current_time); break; case flyingoff: moving_plane.fly(current_time); break; case idle: run_idle(current_time); small_airport.shut_down(end_time); ,The Runway Class Specification,#
17、include Queue.h enum Runway_activity idle, land, flyingoff; class Runway public: Extended_queue landing; Extended_queue takeoff; Runway(int limit); Error_code can_land(const Plane ,#include Queue.h enum Runway_activity idle, land, flyingoff; class Runway public: Extended_queue landing; Extended_queu
18、e takeoff; Runway(int limit); Error_code can_land(const Plane ,private: int queue_limit; int num_land_requests; / number of planes asking to land int num_takeoff_requests; / number of planes asking to take off int num_landings; / number of planes that have landed int num_takeoffs; /number of planes
19、that have taken off int num_land_accepted; / number of planes queued to land int num_takeoff_accepted; / number of planes queued to take off int num_land_refused; / number of landing planes refused int num_takeoff_refused; / number of departing planes refused int land_wait; /total_time of planes_wai
20、ting to land int takeoff_wait; /total_time of planes_waiting to take off int idle_time; /total_time runway is idle ;,The Plane Class Specification,enum Plane_status null, arriving, departing ; class Plane public: Plane( ); Plane(int flt, int time, Plane_status status); void refuse( ) const; void lan
21、d(int time) const; void fly(int time) const; int started( ) const return clock_start; private: int flt_num; int clock_start; Plane_status state; ;,Simulation Initialization,void initialize(int ,coutqueue_limit; coutend_time; bool acceptable; do coutarrival_rate; coutdeparture_rate; if (arrival_rate1
22、.0),coutqueue_limit; coutend_time; bool acceptable; do coutarrival_rate; coutdeparture_rate; if (arrival_rate1.0),cerrSafety Warning: This airport will become saturated; while(!acceptable); ,Runway Methods,Runway:Runway(int limit) /* Post: The Runway data members are initialized to record no prior R
23、unway use and to record the limit on queue sizes. */ queue_limit=limit; num_land_requests=num_takeoff_requests=0; num_landings=num_takeoffs=0; num_land_refused=num_takeoff_refused=0; num_land_accepted = num_takeoff_accepted=0; land_wait=takeoff_wait=idle_time=0; ,Error_code Runway:can_land(const Pla
24、ne ,Error_code Runway:can_depart(const Plane ,Error_code Runway:activity(int time, Plane ,else if (!takeoff.empty( ) takeoff.retrieve(moving); takeoff_wait+=time-moving.started( ); num_takeoffs+; in_progress = flyingoff; takeoff.serve( ); else idle_time+; in_progress = idle; return in_progress; ,Pla
25、ne Initialization,Plane:Plane(int flt, int time, Plane_status status) /* Post: The Plane data members flt num, clock_start , and state are set to the values of the parameters flt ,time and status , respectively. */ flt_num=flt; clock_start = time; state = status; cout Plane number flt ready to ; if
26、(status=arriving) coutland.endl; else couttake off.endl; ,Plane:Plane( ) /* Post: The Plane data membersflt num,clock_start ,state are set to illegal default values. */ flt_num=-1; clock_start =-1; state=null; ,Plane Methods,void Plane:refuse( ) const /* Post: Processes a Plane wanting to use Runway
27、 , when the Queue is full. */ cout Plane number flt_num; if (state=arriving) cout directed to another airportendl; else cout told to try to takeoff again laterendl; void Plane:land(int time) const /* Post: Processes a Plane that is landing at the specified time. */ int wait = time-clock_start; coutt
28、ime: Plane number flt_num landed after ;,coutwait time unit(wait=1) ? : s); cout in the takeoff queue. endl; void Plane:fly(int time) const /* Post: Process aPlane that is taking off at the specied time. */ int wait = time-clock_start; couttime : Plane number flt_num ; cout took off after ; coutwait
29、 time unit (wait = 1) ? : s); cout in the takeoff queue.endl; ,Finishing the Simulation,void Runway:shut_down(int time) const /* Post: Runway usage statistics are summarized and printed. */ coutnnSimulation has concluded after time; cout time units.n Total number of planes processed ; cout(num_land_
30、requests + num_takeoff_requests)endl; coutTotal number of planes asking to land ; cout num_land_requestsendl; coutTotal number of planes asking to take off ; cout num_takeoff_requestsendl; coutTotal number of planes accepted for landing ; cout num_land_acceptedendl; coutTotal number of planes accept
31、ed for takeoff ; coutnum_takeoff_acceptedendl;,coutTotal number of planes refused for landing ; coutnum_land_refusedendl; cout Total number of planes refused for takeoff ; cout num_takeoff_refusedendl; cout Total number of planes that landed ; coutnum_landingsendl; cout Total number of planes that t
32、ook off ; coutnum_takeoffsendl; cout Total number of planes left in landing queue ; coutlanding.size( )endl; cout Total number of planes left in takeoff queue ; couttakeoff.size( )endl; cout Percentage of time runway idle ; cout(100.0*(float)idle_time)/(float)time)%endl;,coutAverage wait in landing queue ; cout(float)land_wait)/(f
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年智能客服系统在智能语音交互领域的应用与开发可行性
- 高中化学教师数字技能培养与数据可视化评价研究教学研究课题报告
- 2026年矿业自动化开采报告
- 生物制药工艺安全操作手册
- 人才职业道德素质保障承诺书7篇范文
- 2026年微电网储能系统优化报告及未来五至十年能源储备报告
- 2019电力系统继电保护事故案例分析
- 交通运输安全监管与执法规范
- 仓储物流管理规范及操作流程
- 2026年烹饪大师中式家常菜制作实操考试题
- 矛盾纠纷排查调处台账管理规范文件
- 《人工智能语言与伦理》章节测试题及答案
- 2025年中国20%噻唑锌悬浮剂数据监测研究报告
- 传播与策划课件
- 猪肉儿童营养食品创新创业项目商业计划书
- 项目整体实施方案(3篇)
- 工程部门员工职责培训
- 2025至2030年中国干葡萄酒行业发展研究报告
- 北京市建设工程施工现场安全生产标准化管理图集(2019版)
- ICP-MS在水质监测中的应用
- DZ/T 0462.8-2023 矿产资源“三率”指标要求 第8部分:硫铁矿、磷、硼、天然碱、钠硝石(正式版)
评论
0/150
提交评论