这里我们模拟一下银行排队叫号系统的实现:
假设一个银行有4个窗口对外接待客户。由于每个窗口在某一时刻只能接待一个客户,在客户众多的时候需要排队,对于刚进入银行的客户,如果某个窗口正空闲,
则可上前办理业务,如果所有窗口都不空闲则排在人数最少的窗口。
现在要求模拟银行的某一时间段内的4个窗口的客户排队情况。这里客户到达的时刻和办理业务的时间都是随机的。
首先我们银行发生事件,我们得有一个类表示事件对象
/*
*功能:这个实现的是我们事件的数据单元节点
*文件:Event.h
*时间:2015年7月8日20:55:32
*作者:cutter_point
*/
#ifndef EVENT_H
#define EVENT_H
class Event
{
int OccurTime; //事件发生的时刻
int NType; //事件类型,0表示到达事件,1到4表示四个窗口的离开事件
public:
Event() //初始化,我们把事件发生的事件和事件类型都首先视为0
{
this->OccurTime = 0;
this->NType = 0;
}
Event(int Occur, int type)
{
this->OccurTime = Occur;
this->NType = type;
}
//重写=操作符,运算符的重载
/*
Event& operator=(const Event& event)
{
if (event == NULL)
{
}
}
*/
//set和get方法
void setOccurTime(int OccurTime) { this->OccurTime = OccurTime; }
int getOccurTime() { return this->OccurTime; }
void setNType(int type) { this->NType = type; }
int getNType() { return this->NType; }
};
#endif
接着我们用一个链表来存放我们的事件发生链
/*
*功能:这个实现的是链表的功能
*文件:LinkList.h
*时间:2015年7月7日20:33:18,2015年7月8日20:01:31
*作者:cutter_point
*/
#ifndef LINKLIST_H
#define LINKLIST_H
#include "Event.h"
#include <stdio.h>
using namespace std;
//链表的一个节点
class Node
{
Event event; //创建我们的数据元对象,包含一个事件发生时间和一个事件类型
Node* next;
public:
Node() { this->next = nullptr; }
Node(Event e) { this->event = e; this->next = nullptr; }
//set和get方法
void setEvent(Event e) { this->event = e; }
Event getEvent() { return this->event; }
void setNext(Node* n) { this->next = n; }
Node* getNext() { return this->next; }
};
//我们的链表
class LinkList
{
Node* head; //头指针
Node* getTail(); //得到尾部指针
public:
LinkList(){ this->head = new Node(); }
void insert(Event e); //给链表末尾添加一个元素
Node* pop(); //删除最后一个节点
bool listEmpty(); //判断是否是空链表
Node* getHead(); //取得头指针
};
#endif
实现:
/*
*功能:这个实现的是链表的功能
*文件:LinkList.cpp
*时间:2015年7月7日20:33:18,2015年7月8日20:01:31,2015年7月9日20:42:11
*作者:cutter_point
*/
#include "LinkList.h"
#include <iostream>
using namespace std;
/*******************************************************Node****************************************************************************/
/*******************************************************LinkList********************************