c++ 队列模拟
来源:互联网 发布:淘宝电话怎么转接人工 编辑:程序博客网 时间:2024/06/06 16:25
1.队列是一种抽象类型(ADT),是先进先出的。
2.Queue类接口
class Queue{enum{Q_SIZE=10};public: Queue(int qs=Q_SIZE); ~Queue(); bool isempty() const; bool isfull() const; int queuecount() const; bool enqueue(const Item &item); bool dequeue(Item &item);}
3.Queue类的实现
//表示节点struct Node{ Item item; struct Node * next;}
class Queue{private: struct Node{Item item;struct Node * next;} enum{Q_SIZE=10}; Node *front; Node *rear; int items;//队列当前数 const int qsize;//队列最大数//在类中嵌套结构,Node既可以用它来声明类成员,也可以用它作为类方法中的类型名称,但只能在类中使用(好难理解……)这样做的好处是不会与某些全局声明冲突。这种嵌套不会创建数据对象,只是指定了在类中使用的类型。}
4.队列模拟
//这里模拟到了一个银行处理顾客事务的队列,可模拟顾客与队列之间的交互。//queue.h#ifndef QUEUE_H_#define QUEUE_H_class Customer{private: long arrive; int processtime;public: Customer(){ arrive = processtime = 0; } void set(long when); long when() const{ return arrive; } int ptime() const { return processtime; }};typedef Customer Item;//模板化class Queue{private: struct Node{ Item item; struct Node * next; }; enum{ Q_SIZE = 10 }; Node *front; Node *rear; int items;//队列当前数 const int qsize;//队列最大数 Queue(const Queue&q) :qsize(0){} Queue & operator=(const Queue&q){ return *this;}public: Queue(int qs = Q_SIZE); ~Queue(); bool isempty() const; bool isfull() const; int queuecount() const;//队列计数 bool enqueue(const Item &item);//入队:加入队尾 bool dequeue(Item &item);//出队:删除队首};}#endif
//queue.cpp#include"coordin.h"#include<cstdlib>Queue::Queue(int qs) :qsize(qs), front(nullptr), rear(nullptr), items(0){ /*c++11前 因为qsize为常量,因此不能直接赋值,而采用成员初始化列表的方式进行初始化。 若为声明为引用的成员也同样如此: class Agency{..}; class Agent{ private: Agency & belong; }; Agent::Agent(Agency &a):belong(a){...} 不能讲成员初始化列表语法应用于构造函数之外的其他类方法。 */}Queue::~Queue(){ Node *temp; while (front != NULL) { temp = front; front = front->next; delete temp; }}bool Queue::isempty() const{ return items == 0;}bool Queue::isfull() const{ return items == qsize;}int Queue::queuecount() const{ return items;}bool Queue::enqueue(const Item&item){ //入队:加入队尾 if (isfull()) return false; Node *add = new Node; add->item = item; add->next = nullptr; items++; if (front ==nullptr) front = add; else rear->next = add; rear = add; return true;}bool Queue::dequeue(Item&item){ //出队:删除队首 if (front == nullptr) return false; item = front->item;//将队列的第一个项目提供给调用函数 items--; Node *temp = front;//将队首的地址暂时保存到temp节点中 front = front->next; delete temp; if (items == 0) rear = nullptr; return true;}void Customer::set(long when){ processtime = std::rand() % 3 + 1; arrive = when;}//每三分之一的顾客处理事务的时间分别为1,2,3分钟
5.补充
#include<cstdlib>//for rand() and srand()#include<ctime>//for timebool f(double x){return (std::rand()*x/RAND_MAX<1);}//rand()*x/RAND_MAX的值为0到x之间,具体的说,平均每隔x次,这个值会有一次小于1,函数返回true,其余均为false。
6.参考书籍
c++Primer Plus(第六版)第12章P460-P475
哇(书上还有一个交互实例ATM模拟,很好的利用了队列的性质,这里就不赘述了。)
阅读全文
0 0
- 【C++】模拟实现队列
- 双栈模拟队列 c
- CodeForces - 749C Voting 队列模拟
- 【c++】模拟实现顺序队列方法一
- 【c++】模拟实现顺序队列方法二
- 模拟队列(一)-C描述
- 【C++】优先级队列的模拟实现
- (C++)栈和队列的模拟实现
- 队列模拟
- 模拟队列
- 模拟队列
- 数据结构:用队列模拟理发店的排队情况(C#)
- 多级反馈队列调度算法 C语言模拟实现
- 【队列模拟】集卡片 card.pas/c/cpp
- java多线程模拟M/M/C(马科洛夫队列)
- 用两个栈模拟队列实例(C++)
- CodeForces 546C Soldier and Cards(队列模拟)
- Codeforces Round #366 (Div. 2) C. Thor (队列模拟)
- python中的import和from
- NOIP历年题目分析
- G1垃圾收集器
- 序列化和反序列化
- 分页SQL
- c++ 队列模拟
- 50. Pow(x, n)
- L1-003. 个位数统计
- 动物声音模拟器, 模仿许多动物的叫声
- Python每日小结(二)
- 洛谷P1007 独木桥
- HDU 1241 Oil Deposits
- Java:求奇数的乘积
- 接口和抽象类的区别