C++类模板 实现循环队列的顺序存储结构算法 《数据结构》(北京科海) 部分摘抄 自己编写实现
2012-11-19 15:16
891 查看
循环队列的顺序存储结构
这里需说明一点:满队列时实际仍有一个元素的空间未使用,(rear+1)%maxSize == front 为满队列判断条件。若不留这个元素空间,则队尾指针rear一定指向该元素空间,使得满队列时的判断条件也是front == rear,则与空队列的判断条件相同而导致无法区分。
代码实现如下:
有什么错误之处,请大家指出来,互相学习,希望我写的代码,能帮到你.......
这里需说明一点:满队列时实际仍有一个元素的空间未使用,(rear+1)%maxSize == front 为满队列判断条件。若不留这个元素空间,则队尾指针rear一定指向该元素空间,使得满队列时的判断条件也是front == rear,则与空队列的判断条件相同而导致无法区分。
代码实现如下:
/* Filename: SeqQueue.h Description: The Sequential storage structure of quequ. Date: November 19, 2012 */ #ifndef SEQQUEUE_H #define SEQQUEUE_H //#include <queue> #include <iostream> using namespace std; const int MAXSIZE = 5; //循环队列类定义 template<class T> class SeqQueue { public: SeqQueue() { new_q = new T[MAXSIZE]; front = rear = 0; } ~SeqQueue() { delete new_q; } public: bool IsEmpty(); bool IsFull(); bool Front(T &x); //获得队列首值 int Length(); bool EnQueue(T x); //进队列 bool DeQeue(); //出队列 void Print(); int Getfront(); int Getrear(); private: int front, rear; T *new_q; }; //循环队列类实现 template<class T> bool SeqQueue<T>::IsEmpty() { if (front == rear) { return true; } return false; } template<class T> bool SeqQueue<T>::IsFull() { if (front == (rear+1) % MAXSIZE) { return true; } return false; } template<class T> int SeqQueue<T>::Length() { return (rear-front+MAXSIZE) % MAXSIZE; } template<class T> bool SeqQueue<T>::Front(T &x) { if (!IsEmpty()) { x = new_q[front]; return true; } else { cout << "Empty" << endl; return false; } } template<class T> bool SeqQueue<T>::EnQueue(T x) { if (IsFull()) { cout << "Full" << endl; return false; } else { new_q[rear] = x; rear = (rear+1) % MAXSIZE; return true; } } template<class T> bool SeqQueue<T>::DeQeue() { if (IsEmpty()) { cout << "Underflow" << endl; return false; } front = (front+1) % MAXSIZE; return true; } template<class T> void SeqQueue<T>::Print() { if (!IsEmpty()) { int tempNum = front; while (tempNum != rear) { cout << new_q[tempNum] << " " ; tempNum = (tempNum+1) % MAXSIZE; } cout << endl; } } template<class T> int SeqQueue<T>::Getfront() { return front; } template<class T> int SeqQueue<T>::Getrear() { return rear; } #endif #include "SeqQueue.h" int main() { SeqQueue<int> Se; Se.EnQueue(2); Se.EnQueue(4); Se.EnQueue(6); Se.Print(); cout << "------------------------" << endl; Se.EnQueue(5); Se.EnQueue(3); Se.Print(); cout << "------------------------" << endl; cout << "队列长度:" << Se.Length() << endl; int front_date; Se.Front(front_date); cout << "队列首元素:" << front_date << endl; cout << "front值:" << Se.Getfront() << endl; cout << "rear值:" << Se.Getrear() << endl; Se.DeQeue(); Se.DeQeue(); Se.Print(); cout << "------------------------" << endl; Se.EnQueue(100); Se.EnQueue(101); Se.Print(); Se.Front(front_date); cout << "队列首元素:" << front_date << endl; cout << "front值:" << Se.Getfront() << endl; cout << "rear值:" << Se.Getrear() << endl; cout << "------------------------" << endl; system("pause"); return 0; }
有什么错误之处,请大家指出来,互相学习,希望我写的代码,能帮到你.......
相关文章推荐
- C++类模板 实现队列的链式存储结构算法 《数据结构》(北京科海) 部分代码摘抄,自己编写运行
- C++类模板 实现两栈共享数组空间的算法 《数据结构》(北京科海) 自己摘抄完成
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- C++类模板 实现栈的链式存储结构 《数据结构》(北京科海) (部分摘抄)
- C++类模板实现循环队列
- C++类模板 实现顺序表 从《数据结构》(C++版) 北京科海摘抄
- 【自己动手写数据结构】 -- 循环队列的表示与实现
- 算法与数据结构【四】——C语言实现循环队列
- 算法与数据结构 其四 循环队列的实现
- 数据结构:循环队列(C语言实现)
- 模板实现循环队列
- 数据结构之循环数组实现队列
- [数据结构]队列之链式队列的类模板实现
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
- [数据结构]队列实现、循环队列的不同实现
- 微软等数据结构+算法面试100题(33)--用俩个栈实现队列
- 数据结构之——基于数组实现的循环队列
- 进阶篇_STL详解(函数模板特化,类模板特化,用模板实现自己的通用算法)
- 数据结构(三)——队列及实现、循环队列实现
- 数据结构之队列――顺序存储结构(php代码实现――方法三)