前面发表了一个Int型的环形队列,现在添加一个customer类,使环形队列的成员为customer的对象
以下为customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include<string>
using namespace std;
class Customer
{
public:
Customer(string name,int num);
Customer(){ Cus_name = " "; Cus_num = 0; };
~Customer(){};
void printInfo()const;//打印客户信息
private:
string Cus_name;
int Cus_num;//顾客拿到的号码
};
#endif
以下为Myqueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include"customer.h"
class Circle_Queue
{
public:
Circle_Queue(int queue_capacity);
virtual ~Circle_Queue();
bool isQueueEmpty()const;
bool isQueueFull()const;
void clear_queue();
int queue_length()const;
bool InQueue(Customer element);//参数类型改为Customer
bool DeQueue();
void queue_traverse();
private:
int m_queue_capacity;
int m_queue_length;
Customer* CircleQ;//将Int改为Customer
int q_head;
int q_tail;
};
#endif
以下为customer.cpp
#include<iostream>
#include"customer.h"
using namespace std;
Customer::Customer(string name,int num)
{
Cus_name =name;
Cus_num =num;
}
void Customer::printInfo()const
{
cout << "姓名:" << Cus_name << endl;
cout << "号码:" << Cus_num<< endl;
}
以下为Myqueue.cpp
#include"Myqueue.h"
#include<iostream>
using namespace std;
Circle_Queue::Circle_Queue(int queue_capacity)
{
m_queue_capacity = queue_capacity;
clear_queue();
CircleQ=new Customer[queue_capacity];
}
Circle_Queue::~Circle_Queue()
{
delete[]CircleQ;
CircleQ = NULL;
}
void Circle_Queue::clear_queue()
{
q_head = q_tail = 0;
m_queue_length = 0;
}
bool Circle_Queue::isQueueEmpty()const
{
return m_queue_length ==0? true : false;
}
int Circle_Queue::queue_length()const
{
return m_queue_length;
}
bool Circle_Queue::isQueueFull()const
{
return (m_queue_capacity == m_queue_length) ? true:false;
}
bool Circle_Queue::DeQueue()
{
if (isQueueEmpty())
return false;
else
{
q_head = (q_head++)%m_queue_capacity;
m_queue_length--;
return true;
}
}
bool Circle_Queue::InQueue(Customer element)
{
if (isQueueFull())
{
cout << "the queue is full,failed to insert a num" << endl;
return false;
}
else
{
CircleQ[q_tail]= element;
q_tail = (q_tail++) % m_queue_capacity;
m_queue_length++;
return true;
}
}
void Circle_Queue::queue_traverse()
{
if (isQueueEmpty()!=true)
{
for (int i = q_head; i < q_head + m_queue_length; i++)
{
CircleQ[i%m_queue_capacity].printInfo();
cout << "前面还有" << i-q_head << "人" << endl << endl;
}
cout << endl;
}
}
以下为main.cpp
#include<iostream>
#include<stdlib.h>
#include"Myqueue.h"
#include"customer.h"
using namespace std;
int main()
{
Customer c1("zhangsan", 1);
Customer c2("lisi", 2);
Customer c3("wangwu", 3);
Customer c4("liuqi", 4);
Circle_Queue *p = new Circle_Queue(4);
p->InQueue(c1);
p->InQueue(c2);
p->InQueue(c3);
p->InQueue(c4);
p->queue_traverse();
system("pause");
return 0;
}