数据冒险之队列实例

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;              //为空返回true
	else
		return false;               //否则false
	//return m_iQueuelen == 0 ? true : false;
}

bool MyQueue::QueueFull() const
{
	if (m_iQueuelen == m_iQueueCapacity)
		return true;
	else
		return 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值