目录
1.头文件
1.1 Queue.h
lish.h 的文件地址:C++ 数据结构学习 ---- 列表_孤城寻欢的博客-CSDN博客_c++列表
#include"List.h"
//由列表派生
template<typename T> class Queue :public List<T> {
public://size()与empty()直接沿用
void enqueue(T const& e) { List<T>::insertAsLast(e); };//入队
//这里的::不能删除,必须保留用来指明函数的作用范围
T dequeue() {//出队
return List<T>::remove(List<T>::first());
};
T& front() { return List<T>::first()->data; }//队首
};//以列表首/末尾
1.2 主体头文件
#include<iostream>
#include "Queue.h"
#include<ctime>
2. 队列的应用
2.1 银行服务排队
int delay; //控制进度显示的延迟
struct Customer {
int window;
unsigned int time;
}; //顾客类,所属窗口(队列)、服务时长
int bestWindow(Queue<Customer> window[], int nWin);
void printCustomer(Customer c);
void displayProgress(Queue<Customer> window[], int nWin, int now);
void simulate(int, int);
//输出顾客c的状态
void printCustomer(Customer c)
{ cout << "time:" << c.time << " "; }
//为新到顾客确定最佳队列
int bestWindow(Queue<Customer> windows[], int nWin)
{
int minSize = windows[0].size(), optiWin = 0; //最优队列(窗口)
for (int i = 1; i < nWin; i++) //在所有窗口中
if (minSize > windows[i].size()) //挑选出
{
minSize = windows[i].size(); optiWin = i;
} //队列最短者
return optiWin; //返回
}
//显示当前各(窗口)队列情况
void displayProgress(Queue<Customer> windows[], int nWin, int now) {
// system("cls"); //清屏
cout << "============ Time: " << now << " ============ " << endl; // printf("============ Time: %4d ============\n", now);
for (int i = 0; i < nWin; i++) { //对每个窗口,分别
cout << "窗口:" << (char)('A'+i) << " || ";
windows[i].traverse(printCustomer);
cout << endl; //列出队列中的所有顾客
}
}
//按指定窗口数、服务总时间模拟银行业务
void simulate(int nWin, int servTime) {
Queue<Customer>*windows = new Queue<Customer>[nWin]; //为每一窗口创建一个队列
for (int now = 0; now < servTime; now++) { //在下班之前,每隔一个单位时间
displayProgress(windows,nWin,servTime);
if (rand() % (1 + nWin)) { //新顾客以nWin/(nWin + 1)的概率到达
Customer c; c.time = 1 + rand() % 98; //新顾客到达,服务时长随机确定
c.window = bestWindow(windows, nWin); //找出最佳(最短)的服务窗口
windows[c.window].enqueue(c); //新顾客加入对应的队列
}
for (int i = 0; i < nWin; i++) //分别检查
if (!windows[i].empty()) //各非空队列
if (--windows[i].front().time <= 0) //队首顾客的服务时长减少一个单位
windows[i].dequeue(); //服务完毕的顾客出列,由后继顾客接替
} //for
delete[] windows; //释放所有队列(此前,~List()会自动清空队列)
}
//启动函数
int start(int a,int b) {
cout << "使用:" << "<当前窗口> <服务时间> <延迟(ms)>" << endl;
srand((unsigned int)time(NULL)); //设置随机种子
delay = 5; // delay
simulate(a, b); //启动模拟,按指定窗口数、服务总时间模拟银行业务
return 0;
}
2.2 银行服务排队运行结果截图
3. 完整代码
#include<iostream>
#include "Queue.h"
#include<ctime>
using namespace std;
//测试编号
int testID = 0;
int dice(int n) {
n = rand() % n;
return n;
}
//队列测试
template < typename T> void testQueue(int n) {
Queue<T> Q;
cout << "===测试" << testID++ << "队列增加!" << endl; // printf("\n ==== Test %2d. Growing queue\n", testID++);
while (Q.size() < n) {
if (Q.empty() || 30 < dice(100))
Q.enqueue((T)dice(2 * n)); //70%入队
else Q.dequeue(); //30%出队
// cout << Q.first()->data<<endl;
}
cout << "===测试" << testID++ << "队列减少! " << endl;//printf("\n ==== Test %2d. Shrinking queue\n", testID++);
while (!Q.empty()) {
if (70 < dice(100))
Q.enqueue((T)dice(2 * n)); //30%入队
else Q.dequeue(); //70%出队
//cout << Q.first()->data<<endl;
}
}
int delay; //控制进度显示的延迟
struct Customer {
int window;
unsigned int time;
}; //顾客类,所属窗口(队列)、服务时长
int bestWindow(Queue<Customer> window[], int nWin);
void printCustomer(Customer c);
void displayProgress(Queue<Customer> window[], int nWin, int now);
void simulate(int, int);
//输出顾客c的状态
void printCustomer(Customer c)
{ cout << "time:" << c.time << " "; }
//为新到顾客确定最佳队列
int bestWindow(Queue<Customer> windows[], int nWin)
{
int minSize = windows[0].size(), optiWin = 0; //最优队列(窗口)
for (int i = 1; i < nWin; i++) //在所有窗口中
if (minSize > windows[i].size()) //挑选出
{
minSize = windows[i].size(); optiWin = i;
} //队列最短者
return optiWin; //返回
}
//显示当前各(窗口)队列情况
void displayProgress(Queue<Customer> windows[], int nWin, int now) {
// system("cls"); //清屏
cout << "============ Time: " << now << " ============ " << endl; // printf("============ Time: %4d ============\n", now);
for (int i = 0; i < nWin; i++) { //对每个窗口,分别
cout << "窗口:" << (char)('A'+i) << " || ";
windows[i].traverse(printCustomer);
cout << endl; //列出队列中的所有顾客
}
}
//按指定窗口数、服务总时间模拟银行业务
void simulate(int nWin, int servTime) {
Queue<Customer>*windows = new Queue<Customer>[nWin]; //为每一窗口创建一个队列
for (int now = 0; now < servTime; now++) { //在下班之前,每隔一个单位时间
displayProgress(windows,nWin,servTime);
if (rand() % (1 + nWin)) { //新顾客以nWin/(nWin + 1)的概率到达
Customer c; c.time = 1 + rand() % 98; //新顾客到达,服务时长随机确定
c.window = bestWindow(windows, nWin); //找出最佳(最短)的服务窗口
windows[c.window].enqueue(c); //新顾客加入对应的队列
}
for (int i = 0; i < nWin; i++) //分别检查
if (!windows[i].empty()) //各非空队列
if (--windows[i].front().time <= 0) //队首顾客的服务时长减少一个单位
windows[i].dequeue(); //服务完毕的顾客出列,由后继顾客接替
} //for
delete[] windows; //释放所有队列(此前,~List()会自动清空队列)
}
//启动函数
int start(int a,int b) {
cout << "使用:" << "<当前窗口> <服务时间> <延迟(ms)>" << endl;
srand((unsigned int)time(NULL)); //设置随机种子
delay = 5; // delay
simulate(a, b); //启动模拟,按指定窗口数、服务总时间模拟银行业务
return 0;
}
int main() {
//front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
//back():返回 queue 中最后一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
//push(const T& obj):在 queue 的尾部添加一个元素的副本。这是通过调用底层容器的成员函数 push_back() 来完成的。
//push(T&& obj):以移动的方式在 queue 的尾部添加元素。这是通过调用底层容器的具有右值引用参数的成员函数 push_back() 来完成的。
//pop():删除 queue 中的第一个元素。
//size():返回 queue 中元素的个数。
//empty():如果 queue 中没有元素的话,返回 true。
//emplace():用传给 emplace() 的参数调用 T 的构造函数,在 queue 的尾部生成对象。
//swap(queue<T> &other_q):将当前 queue 中的元素和参数 queue 中的元素交换。它们需要包含相同类型的元素。也可以调用全局函数模板 swap() 来完成同样的操作。
//queue<int> q;
int argc = 20;
srand((unsigned int)time(NULL));
start(3,5);
system("pause");
testQueue<int>(10);
Queue<int> a;
a.enqueue(5);//将e插入队尾
cout << a.size()<<" ";//长度检测
a.enqueue(6);
a.enqueue(7);
a.enqueue(5);
cout << a.empty()<<" ";
cout << a.front()<<" ";
a.dequeue();
cout << a.front() << " ";
cout << endl;
system("pause");
return 0;
}