队列篇(二)----环形队列的应用(C++版)

来源:互联网 发布:fifa隐藏属性数据库 编辑:程序博客网 时间:2024/05/23 02:03



根据对基本环形队列的创建,这里引出创建一个新类型:顾客类,保存客户用户名和年龄,存入到环形队列中。这里对上节基本环形队列稍做修改,然后添加一个对顾客类的声明和定义文件。

           注:本节内容调用上节环形队列创建方法代码,但是对于队列结构的类型做了相应修改,那么我们可以思考,是否可以利用c++的模板函数,定义一个适合所有类型的环形队列



创建顾客类声明文件  Customer.h

#include<string>
using namespace std;
class Customer{
public:
Customer(string name="", int age=0);        //定义默认值的默认构造函数
void printInfo()const;                                 //打印当前顾客信息
private:
string m_strName;                               //顾客姓名
int m_iAge;                                           //顾客年龄
};



创建类声明文件文件  MyQueue.h

#include"Customer.h"                 //引入顾客类

class MyQueue{
public:
MyQueue(int queueCapacity);      //创建队列
virtual ~MyQueue();                      //销毁队列
void ClearQueue();                       //清空队列
bool QueueEmpty() const;         //判空队列

int QueueLength() const;           //检测队列长度
bool QueueFull() const;             //判定队列是否为满
bool EnQueue(Customer element);          //新元素入队,传入元素修改为Customer类型
bool DeQueue(Customer &element);        //首元素出队,出队元素修改为Customer类型

void QueueTraverse();                    //遍历队列

private:

Customer *m_pQueue;                     //队列数组指针,修改为Customer类型

 C*m_pQueue;                               //队列数组指针int m_iQueueLen;                           //队列元素个数

int m_iQueueCapacity;                   //队列数组容量
int m_iHead;                                   //队列头
int m_iTail;                                     //队列尾
};

创建类定义文件  MyQueue.cpp

#include "MyQueue.h"
#include<iostream>
using namespace std;
MyQueue::MyQueue(int queueCapacity){
m_iQueueCapacity = queueCapacity;
m_pQueue = new Customer[m_iQueueCapacity];       //动态创建一个规定容量的数组,这里修改为Customer类型数组
ClearQueue();                                                      //初始时,元素个数,头队列,尾队列都为0,可调用清空队列函数
}
MyQueue::~MyQueue(){
delete[]m_pQueue;                                             //删除动态数组
m_pQueue = NULL;
}
void MyQueue::ClearQueue(){
m_iHead = 0;
m_iTail = 0;
m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty()const{
if (m_iQueueLen == 0){
return true;
}
else{
return false;
}
}
int MyQueue::QueueLength()const{
return m_iQueueLen;
}
bool MyQueue::QueueFull()const{
if (m_iQueueLen == m_iQueueCapacity){
return true;
}
else{
return false;
}
}
bool MyQueue::EnQueue(Customer element){
if (QueueFull()){
return false;
}
else{
m_pQueue[m_iTail] = element;
m_iTail++;
m_iTail = m_iTail%m_iQueueCapacity;      //核心:这里取容量的余数,为了实现循环遍历成一个环形,达到数组容量时,又置为0
m_iQueueLen++;                                         //队列元素个数加一
return true;
}
}
bool MyQueue::DeQueue(Customer &element){
if (QueueEmpty()){
return false;
}
else{
element = m_pQueue[m_iHead];
m_iHead++;
m_iHead = m_iHead%m_iQueueCapacity;      //与入队时作用相同
m_iQueueLen--;                                                //队列元素减一
return true;
}
}
void MyQueue::QueueTraverse(){
for (int i = m_iHead; i < m_iQueueLen; i++){                    //循环从队列头开始,执行队列长度次,到达队列尾
 m_pQueue[i%m_iQueueLen].printInfo();        //输出按环形队列输出,由于定义了顾客类的输出函数,这里直接调用即可
}
}

创建顾客类定义文件 Customer.cpp

#include<iostream>
#include"Customer.h"
using namespace std;
Customer::Customer(string name, int age){
m_strName = name;
m_iAge = age;
}
void Customer::printInfo()const{
cout << "姓名:" << m_strName << endl;
cout << "年龄:" << m_iAge << endl;
cout << endl;
}

创建测试文件  demo.cpp

#include<iostream>
#include<stdlib.h>
#include"MyQueue.h"
//#include"Customer.h"
int main(void){
MyQueue *p = new MyQueue(4);                      //创建一个容量为四的环形数组
Customer c1("zhangsan", 20);                           //创建第一个顾客对象c1

Customer c2("lisi", 23);                                     //创建第二个顾客对象c2
p->EnQueue(c1);                                              //分别将两个顾客对象传入队列中
p->EnQueue(c2);
p->QueueTraverse();                                          //输出查看队列信息
}

原创粉丝点击