【李木华】队列
来源:互联网 发布:闪迪固态硬盘优化软件 编辑:程序博客网 时间:2024/05/18 03:01
队列
定义:
队列元素只能从队尾插入(称为入队操作,enqueue),从队首删除(称为出对操作,dequeue)
ADT:
template <typename E>class Queue{public: Queue() {} //构造函数 virtual ~Queue() {} //析构函数 virtual void clear() = 0; //清除 virtual void enqueue(const E&) = 0; //入队 virtual void dequeue() = 0; //出队 virtual const E& frontValue() const = 0; //队首元素 virtual int length() const = 0; //队列长度};
顺序队列:
实现方法:listArray是一个指向存放队列元素数组的指针,队列的构造函数提供可选参数,以设置队列的最大长度。为了区分空队列和满队列,数组大小实际要比队列允许的最大长度多 1。
代码如下:
template<typename E>class AQueue :public Queue < E > {private: int maxSize; int front; int rear; E *listArray;public: AQueue(int sz = defaultSzie) { maxSize = sz + 1; rear = 0; front = 1; listArray = new E[maxSize]; } ~AQueue(){ delete[] listArray; } void clear(){ rear = 0; front = 1; } void enqueue(const E& it) { assert(((rear + 2) % maxSize) != front, "Queue is full!"); rear = (rear + 1) % maxSize; listArray[rear] = it; } E dequeue() { assert(length() != 0, "Queue is empty!"); E it = listArrey[front]; front = (front + 1) % maxSize; return it; } const E& frontValue() const { assert(length != 0, "Queue is empty!"); return listArray[front]; } int length() const { retrun((rear + maxSize) - front + 1) % maxSize; }};
链式队列
在初始化的时候,front和rear同时指向头节点,之后front总是指向头节点,而rear指向队列的尾结点。
代码如下
template<typename E>class LQueue :public Queue < E > {private: Link<E>* front; Link<E>* rear; int size;public: LQueue(int sz = defaultSize) { front = rear = new Link<E>(); size = 0; } ~LQueue(){ clear(); delete front; } void clear() { while (front->next != NULL) { rear = front; delete rear; } rear = front; size = 0; } void enqueue(const E& it) { rear->next = new Link<E>(it, NULL); rear = rear->next; size++; } E dequeue() { assert(size != 0, "Queue is empty!"); E it = front->next->element; Link<E>* temp = front->next; front->next = temp->next; if (rear == temp) rear = front; delete temp; size--; return it; } const E& frontValue() const { assert(size != 0, "Queue is empty!"); return front->next->element; } int length()const { return szie; }};
队列嘛,太多例子了。比如食堂打饭窗口前那些排得老长老长了的队伍。这些例子就不说了。
存货的计量方法中,有的方法也包含了FIFO的思想。以先购入的存货先发出(销售或耗用)。通俗地说就是,先进回来的货,先卖出去(可以防止过期等等。)
Copyright© by 李木华
Date:2017.1.9
0 0
- 【李木华】队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- 队列
- Git push提交到远程仓库卡住的问题解决
- C++多态深度剖析
- Leetcode 238. Product of Array Except Self
- [19]Window管理框架WMF(Window Management Framework) 5.0 VS Window管理框架WMF(Window Management Framework) 5.1
- 统计分析基础-描述数据
- 【李木华】队列
- csdn博客第一天 支持浏览器离线Markdown
- Git忽略文件.gitignore的使用
- Java之lambda表达式
- 51nod-1277 字符串中的最大值(KMP)
- Leetcode 442. Find All Duplicates in an Array
- linux gcc编译常用命令之gun工具链的使用
- Leetcode 347. Top K Frequent Elements
- C++多态深度剖析