数据冒险之队列实例

来源:互联网 发布:日系高端护肤品知乎 编辑:程序博客网 时间:2024/04/23 12:29

Customer.h

#ifndef CUSTOMER_H#define CUSTOMER_H#include<string>using namespace std;class Customer{public://Customer();Customer(string name="", int age=0);void printInfo() const;private:string m_Strname;int m_iage;};#endif

Customer.cpp

#include"Customer.h"#include<iostream>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;}

MyQueue.h

#ifndef MYQUEUE_H#define MYQUEUE_H#include"Customer.h"class MyQueue{public:MyQueue(int quequeCapacity);      // 创建队列 virtual ~MyQueue();              // 销毁队列 void ClearQueue();              //清空队列 bool QueueEmpty() const;        //判空 bool QueueFull() const;         //判满 int QueueLength() const;        //获得队列长度 bool EnQueue(Customer element);     //入队 bool DeQueue(Customer &element);        //出队 void QueueTraverse();           //遍历队列 private:int m_iQueueCapacity;          //队列容量 int m_iHead;                    //队列头    //数组下标int m_iTail;                    //队列尾 int m_iQueuelen;                //队列长度 Customer *m_pQueue;                  //队列指针 };#endif

MyQueue.cpp

#include "MyQueue.h"#include <iostream>using namespace std;MyQueue::MyQueue(int queueCapacity)  //初始化队列{m_iQueueCapacity = queueCapacity;           //此处传入队列总容量m_pQueue = new Customer[m_iQueueCapacity];       //在堆上申请内存  ClearQueue();                             //清空队列}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;              //为空返回trueelsereturn false;               //否则false//return m_iQueuelen == 0 ? true : false;}bool MyQueue::QueueFull() const{if (m_iQueuelen == m_iQueueCapacity)return true;elsereturn false;}int MyQueue::QueueLength() const{return m_iQueuelen;}bool MyQueue::EnQueue(Customer element)     //入队操作{if (QueueFull())            //入队先判满return false;else{m_pQueue[m_iTail] = element;//尾部入队m_iTail++;                   //尾变长m_iTail %= m_iQueueCapacity; //取余实现循环队列 m_iQueuelen++;               //长度变长return true;}}bool MyQueue::DeQueue(Customer &element)     //出队操作{if (QueueEmpty())           //出队则判空return false;else{element = m_pQueue[m_iHead];//头部出队m_iHead++;                  //头改变 m_iHead %= m_iQueueCapacity;//取余实现循环m_iQueuelen--;              //长度减一return true;}}void MyQueue::QueueTraverse()//遍历要注意起始于结束{ //从Head开始,共遍历m_iQueuelen个,不能以Tail结尾//因为Tail是会变化的,这里取余操作也是必不可少的for (int i = m_iHead; i<m_iQueuelen + m_iHead; i++){m_pQueue[i%m_iQueueCapacity].printInfo();cout << "前面还有 " << (i - m_iHead) << " 人" << endl;}}

main.cpp

#include "MyQueue.h"#include<iostream>using namespace std;int main(){MyQueue * p = new MyQueue(4);Customer c1("张三",20);Customer c2("李四", 22);Customer c3("王五", 23);Customer c4("刘六", 24);//入队操作cout << "/******入队******/" << endl;p->EnQueue(c1);p->EnQueue(c2);p->EnQueue(c3);p->EnQueue(c4);p->QueueTraverse();//出队操作,并显示cout << "/******出队******/" << endl;p->DeQueue(c1);c1.printInfo();//p->QueueTraverse();delete[]p;p = NULL;return 0;}