【Weiss】【第03章】练习3.25:数组模拟队列
2015-03-21 02:53
309 查看
【练习3.25】
编写实现队列的例程,使用
a.链表
b.数组
Answer:
在这章一开头就已经写了个链表的队列例程了,所以实际上只要做b小题就可以。
数组模拟队列和链表的两点小不同是:
①、数组空间有限,入队需要检测数组是否已经满
②、数组经过几次操作后,rear可能绕回front前面,所以许多操作都要用模来实现。
测试代码:
View Code
实现代码:
编写实现队列的例程,使用
a.链表
b.数组
Answer:
在这章一开头就已经写了个链表的队列例程了,所以实际上只要做b小题就可以。
数组模拟队列和链表的两点小不同是:
①、数组空间有限,入队需要检测数组是否已经满
②、数组经过几次操作后,rear可能绕回front前面,所以许多操作都要用模来实现。
测试代码:
#include <iostream> #include "queue.h" using namespace std; using namespace queue; template class Queue<int>; int main(void) { Simu_Queue<int> test(6); //测试插入 test.enqueue(2); test.enqueue(3); test.enqueue(5); test.enqueue(7); test.enqueue(11); test.enqueue(13); test.enqueue(17); test.traverse(); cout << endl; //测试删除 test.dequeue(); test.dequeue(); test.dequeue(); test.traverse(); cout << endl; //测试绕数组遍历 test.enqueue(17); test.enqueue(19); test.enqueue(23); test.traverse(); cout << endl; system("pause"); }
View Code
实现代码:
//练习3.25新增,用数组模拟队列 template <typename T> class Simu_Queue { public: Simu_Queue() :head(nullptr), front(0), rear(0), size(0){} Simu_Queue(unsigned int _maxsize) :front(0), rear(0), maxsize(_maxsize){ head = new T[maxsize + 1]; } Simu_Queue(const Simu_Queue& another) { front = another.front; rear = another.rear; maxsize = another.maxsize; head = new T[maxsize + 1]; for (unsigned i = 0; i < maxsize + 1; ++i) head[i] = another.head[i]; } ~Simu_Queue() { delete[] head; front = rear = maxsize = 0; head = nullptr; } Simu_Queue& operator=(const Simu_Queue& another) { if (this != &another) { delete[] head; front = another.front; rear = another.rear; maxsize = another.maxsize; head = new T[maxsize + 1]; for (unsigned i = 0; i < maxsize + 1; ++i) head[i] = another.head[i]; } } public: //返回最大元素量 unsigned int size()const{ return maxsize; } //返回当前元素量 unsigned int length()const{ return front <= rear ? rear - front : rear + maxsize + 1 - front; } //判断是否为空 bool empty()const{ return front == rear; } //入队 bool enqueue(const T &item) { if ((rear + 1) % (maxsize + 1) != front) { head[rear] = item; rear = (rear + 1) % (maxsize + 1); return true; } return false; } //出队 bool dequeue() { if (rear != front) { front = (front + 1) % (maxsize + 1); return true; } return false; } //输出队列元素 void traverse()const { unsigned int temp = front; while (temp != rear) { cout << " " << head[temp] << flush; temp = (temp + 1) % (maxsize + 1); } } private: T* head = nullptr; unsigned int front; unsigned int rear; unsigned int maxsize; };
相关文章推荐
- 【Weiss】【第03章】练习3.21:单数组模拟双栈
- joj 2431: Shift and Increment (模板队列与数组模拟队列的对比练习)
- 【Weiss】【第03章】练习3.26:双端队列
- bash shell数组模拟队列queue和shell数组使用技巧
- java例程练习(数三退一[用数组模拟])
- wait, notify, notifyAll, 简单数组模拟队列实现读者写者问题。
- 【Weiss】【第03章】练习3.6:有序多项式相加
- 【Weiss】【第03章】练习3.4、3.5:有序链表求交、并
- 【Weiss】【第03章】练习3.11:比较单链表递归与非递归查找元素
- Java基本功练习八(多维数组[二维、三维、模拟评卷系统、九宫格验证])
- 数组模拟队列和栈
- 【Weiss】【第03章】练习3.8:有序多项式求幂
- java例程练习(数三退一[用数组模拟])
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
- C# 小程序之新手练习(三)数组模拟约瑟夫环
- 数组模拟队列
- java例程练习(数三退一[用数组模拟])
- 1443. Printer Queue(用一个数组模拟队列,优先级高的先出队列)
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
- 【Weiss】【第03章】练习3.2