C++数据结构学习之队列的应用

开发 后端
在C++数据结构中,队列的应用很广泛。本文以银行挂牌排号的营业模式为例,模拟队列运用,详述队列的应用的具体过程,为读者学习C++队列的应用,提供一些参考。

  在学习C++队列的运用中,我看了两本书,都是讲解队列应用的,而且都是银行营业模拟。细比较,这两本书模拟的银行营业的方式还是不同的。老式的营业模式,现在的很多地方还是这种营业模式——几个窗口同时排队。这种方式其实不太合理,经常会出现先来的还没有后来的先办理业务(常常前面一个人磨磨蹭蹭,别的队越来越短,让你恨不得把前面那人干掉)。另一种营业模式——挂牌的营业方式,每个来到的顾客发一个号码,如果哪个柜台空闲了,就叫号码最靠前的顾客来办理业务;如果同时几个柜台空闲,就按照一种法则来决定这几个柜台叫号的顺序(最简单的是按柜台号码顺序)。这样,就能保证顾客按照先来后到的顺序接受服务——因为大家排在一个队里。这样的营业模式我在北京的西直门工商银行见过,应该说这是比较合理的一种营业模式。

  我按照实际情况模拟,实现如下:

  1. #ifndef Simulation_H  
  2. #define Simulation_H  
  3.  
  4. #include <iostream.h>  
  5. #include <stdlib.h>  
  6. #include <time.h>  
  7.  
  8.  
  9. class Teller  
  10. {  
  11.  public:  
  12.   int totalCustomerCount;  
  13.   int totalServiceTime;  
  14.   int finishServiceTime;  
  15.   Teller() :totalCustomerCount(0), totalServiceTime(0),  
  16.   finishServiceTime(0) {}  
  17. };  
  18.  
  19. //#define PRINTPROCESS  
  20.  
  21. class Simulation  
  22. {  
  23.  public:  
  24.   Simulation()  
  25.   {  
  26.    cout << endl << "输入模拟参数" << endl;  
  27.    cout << "柜台数量:"; cin >> tellerNum;  
  28.    cout << "营业时间:"; cin >> simuTime;  
  29.    cout << "两个顾客来到的最小间隔时间:"; cin >> arrivalLow;  
  30.    cout << "两个顾客来到的最大间隔时间:"; cin >> arrivalHigh;  
  31.    cout << "柜台服务最短时间:"; cin >> serviceLow;  
  32.    cout << "柜台服务最长时间:"; cin >> serviceHigh;  
  33.    arrivalRange = arrivalHigh - arrivalLow + 1;  
  34.    serviceRange = serviceHigh - serviceLow + 1;  
  35.    srand((unsigned)time(NULL));  
  36.   }  
  37.   Simulation(int tellerNum, int simuTime, int arrivalLow, int arrivalHigh, int serviceLow, int serviceHigh)  
  38.  
  39. : tellerNum(tellerNum), simuTime(simuTime), arrivalLow(arrivalLow), arrivalHigh(arrivalHigh),  
  40.  
  41.   serviceLow(serviceLow), serviceHigh(serviceHigh),  
  42.   arrivalRange(arrivalHigh - arrivalLow + 1), serviceRange(serviceHigh - serviceLow + 1)  
  43.   { srand((unsigned)time(NULL)); }  
  44.  
  45. void Initialize()  
  46. {  
  47.  curTime = nextTime = 0;  
  48.  customerNum = customerTime = 0;  
  49.  for (int i = 1; i <= tellerNum; i++)  
  50.  {  
  51.   tellers[i].totalCustomerCount = 0;  
  52.   tellers[i].totalServiceTime = 0;  
  53.   tellers[i].finishServiceTime = 0;  
  54.  }  
  55.  customer.MakeEmpty();  
  56. }  
  57.  
  58. void Run()  
  59. {  
  60.  Initialize();   
  61.  NextArrived();  
  62.  #ifdef PRINTPROCESS  
  63.  
  64.   cout << endl;  
  65.   cout << "tellerID";  
  66.   for (int k = 1; k <= tellerNum; k++) cout << "\tTELLER " << k;  
  67.   cout << endl;  
  68.  #endif  
  69.  
  70.  for (curTime = 0; curTime <= simuTime; curTime++)  
  71.  {  
  72.   if (curTime >= nextTime)  
  73.   {  
  74.    CustomerArrived();  
  75.    NextArrived();  
  76.   }  
  77.   #ifdef PRINTPROCESS   
  78.    cout << "Time: " << curTime << " ";  
  79.   #endif  
  80.   for (int i = 1; i <= tellerNum; i++)  
  81.   {  
  82.    if (tellers[i].finishServiceTime < curTime) tellers[i].finishServiceTime = curTime;  
  83.    if (tellers[i].finishServiceTime == curTime && !customer.IsEmpty())  
  84.    {  
  85.     int t = NextService();  
  86.     #ifdef PRINTPROCESS   
  87.      cout << '\t' << customerNum + 1 << '(' << customer.GetFront() << ',' << t << ')';  
  88.     #endif  
  89.     CustomerDeparture();  
  90.     tellers[i].totalCustomerCount++;  
  91.     tellers[i].totalServiceTime += t;  
  92.     tellers[i].finishServiceTime += t;  
  93.    }  
  94.  
  95.    #ifdef PRINTPROCESS   
  96.    else cout << "\t ";  
  97.    #endif  
  98.   }  
  99.   #ifdef PRINTPROCESS   
  100.    cout << endl;  
  101.   #endif  
  102.  }  
  103.  PrintResult();  
  104. }  
  105.  
  106. void PtintSimuPara()  
  107. {  
  108.  cout << endl << "模拟参数" << endl;  
  109.  cout << "柜台数量: " << tellerNum << "\t营业时间:" << simuTime << endl;  
  110.  cout << "两个顾客来到的最小间隔时间:" << arrivalLow << endl;  
  111.  cout << "两个顾客来到的最大间隔时间:" << arrivalHigh << endl;;  
  112.  cout << "柜台服务最短时间:" << serviceLow << endl;  
  113.  cout << "柜台服务最长时间:" << serviceHigh << endl;  
  114. }  
  115.  
  116. void PrintResult()  
  117. {  
  118.  int tSN = 0;  
  119.  long tST = 0;  
  120.  cout << endl;  
  121.  cout << "-------------模拟结果-------------------";  
  122.  cout << endl << "tellerID\tServiceNum\tServiceTime\tAverageTime" << endl;  
  123.  for (int i = 1; i <= tellerNum; i++)  
  124.  {  
  125.   cout << "TELLER " << i;  
  126.   cout << '\t' << tellers[i].totalCustomerCount << " "; tSN += tellers[i].totalCustomerCount;  
  127.   cout << '\t' << tellers[i].totalServiceTime << " "; tST += (long)tellers[i].totalServiceTime;  
  128.  
  129.   cout << '\t';  
  130.   if (tellers[i].totalCustomerCount)  
  131.    cout << (float)tellers[i].totalServiceTime/(float)tellers[i].totalCustomerCount;  
  132.   else cout << 0;  
  133.    cout << " " << endl;  
  134.  }  
  135.  cout << "TOTAL \t" << tSN << " \t" << tST << " \t";  
  136.  if (tSN) cout << (float)tST/(float)tSN; else cout << 0;  
  137.  cout << " " << endl;  
  138.  cout << "Customer Number:\t" << customerNum << "\tno Service:\t" << customerNum - tSN << endl;  
  139.  
  140.  cout << "Customer WaitTime:\t" << customerTime << "\tAvgWaitTime:\t";  
  141.  if (tSN) cout << (float)customerTime/(float)tSN; else cout << 0;  
  142.  cout << endl;  
  143. }  
  144.  
  145. private:  
  146.  int tellerNum;  
  147.  int simuTime;  
  148.  int curTime, nextTime;  
  149.  int customerNum;  
  150.  long customerTime;  
  151.  int arrivalLow, arrivalHigh, arrivalRange;  
  152.  int serviceLow, serviceHigh, serviceRange;  
  153.  Teller tellers[21];  
  154.  Queue<int> customer;  
  155.  
  156.  void NextArrived()  
  157.  {  
  158.   nextTime += arrivalLow + rand() % arrivalRange;  
  159.  }  
  160.  
  161.  int NextService()  
  162.  {  
  163.   return serviceLow + rand() % serviceRange;  
  164.  }  
  165.  
  166. void CustomerArrived()  
  167. {  
  168.  customerNum++;  
  169.  customer.EnQueue(nextTime);  
  170. }  
  171.  
  172. void CustomerDeparture()  
  173. {  
  174.  customerTime += (long)curTime - (long)customer.DeQueue();  
  175. }  
  176.  
  177. };  
  178.  
  179. #endif 

  几点说明

  1、Run()的过程是这样的:curTime是时钟,从开始营业计时,自然流逝到停止营业。当顾客到的事件发生时(顾客到时间等于当前时间,小于判定是因为个别时候顾客同时到达——输入arrivalLow=0的情况,而在同一时间,只给一个顾客发号码),给这个顾客发号码(用顾客到时间标示这个顾客,入队,来到顾客数增1)。当柜台服务完毕时(柜台服务完时间等于当前时间),该柜台服务人数增1,服务时间累加,顾客离开事件发生,下一个顾客到该柜台。因为柜台开始都是空闲的,所以实际代码和这个有点出入。最后,停止营业的时候,停止发号码,还在接受服务的顾客继续到服务完,其他还在排队的就散伙了。

  2、模拟结果分别是:各个柜台的服务人数、服务时间、平均服务时间,总的服务人数、服务时间、平均服务时间,来的顾客总数、没被服务的数目(来的太晚了)、接受服务顾客总等待时间、平均等待时间。

  3、这个算法效率是比较低的,实际上可以不用队列完成这个模拟(用顾客到时间推动当前时钟,柜台直接公告服务完成时间),但这样就和实际情况有很大差别了——出纳员没等看见人就知道什么时候完?虽然结果是一样的,但是理解起来很莫名其妙,尤其是作为教学目的讲解的时候。当然了,实际中为了提高模拟效率,本文的这个算法是不值得提倡的。

  4、注释掉的#define PRINTPROCESS,去掉注释符后,在运行模拟的时候,能打印出每个时刻柜台的服务情况(第几个顾客,顾客到达时间,接受服务时间),但只限4个柜台以下,多了的话屏幕就满了(格式就乱了)。

  这是数据结构中第一个实际应用的例子,而且也有现实意义。你可以看出各个柜台在不同的业务密度下的工作强度(要么给哪个柜台出纳员发奖金,要么轮换柜台),各种情况下顾客的等待时间(人都是轮到自己就不着急了),还有各种情况下设立几个柜台合理(很少的空闲时间,很短的等待时间,几乎为零的未服务人数)。例如这样:

  1. for (int i = 1; i < 16; i++)  
  2. {  
  3.  Simulation a(i,240,1,4,8,15);  
  4.  a.Run();  
  5. }  

  你模拟一下就会得出,在不太繁忙的银行,4~5个柜台是合适的——现在的银行大部分都是这样的。

【编辑推荐】

  1. 18.3.1 队列的概念 
  2. 数据库使用C++数据结构
  3. 程序员必看 c++笔试题汇总
  4. C++数据结构学习之栈的应用
  5. C++数据结构学习之栈和队列
责任编辑:韩亚珊 来源: 天极网
相关推荐

2011-04-11 11:23:17

队列数据结构

2011-04-11 12:22:11

数据结构C++

2011-04-11 17:09:37

稀疏矩阵矩阵C++

2022-03-31 11:17:58

JavaScript数组方法

2012-02-02 10:21:05

单链表nexthead

2021-07-16 07:57:34

Python数据结构

2009-08-11 14:43:42

C#数据结构与算法

2009-08-13 16:02:29

C#结构

2010-01-27 15:58:35

C++数据结构

2023-12-13 10:01:15

数据结构c++编程

2021-06-08 06:01:00

C++数据结构向量和数组

2024-01-15 06:01:36

C++数组

2010-07-19 11:07:13

Perl控制结构

2011-07-20 17:10:54

C++

2009-08-12 18:35:17

C#数据结构

2022-09-01 16:27:19

JavaScriptWeb开发

2018-06-13 08:53:39

HadoopHBase存储

2009-08-11 14:51:11

C#数据结构与算法

2023-03-28 07:44:23

数据结构数组

2020-12-17 10:12:33

数据结构算法队列
点赞
收藏

51CTO技术栈公众号