左神基础班-猫狗队列

#include<queue>
#include<iostream>
using namespace std;

class Pet{
private:
	int type;
public:
	Pet(){

	}
	Pet(int type){
		this->type = type;
	}
	int getType(){
		return type;
	}
	void setType(int i){
		type =i;
	}
};
class Cat:public Pet
{
};
class Dog:public Pet{
};
class Elem{
private:
	Pet p;
	int num;
public:
	Elem(){}
	Elem(Pet p, int num){
		this->p = p;
		this->num = num;
	}
	Pet getPet(){
		return p;
	}
	void setPet(Pet p){
		this->p = p;
	}
	void setNum(int num){
		this->num = num;
	}
	int getNum(){
		return num;
	}
};
class CatDogQueue{
private:
	queue<Elem> cat;
	queue<Elem> dog;
public:
	void push(Elem elem){
		if(elem.getPet().getType() == 1){
			cat.push(elem);
		}else{
			dog.push(elem);
		}
	}
	Pet pollAll(){
		Pet pet;
		if(!cat.empty() && !dog.empty()){
			if(cat.front().getNum() < dog.front().getNum()){
				pet = cat.front().getPet();
				cat.pop();
			}else{
				pet = dog.front().getPet();
				dog.pop();
			}
		}else if(!cat.empty()){
			pet = cat.front().getPet();
			cat.pop();
		}else if(!dog.empty()){
			pet = dog.front().getPet();
			dog.pop();
		}
		return pet;
	}
	Pet pollCat(){
		if(!cat.empty()){
			return cat.front().getPet();
		}

	}
	bool isEmpty(){
		if(cat.empty() && dog.empty()){
			return true;
		}else{
			return false;
		}
	}
	bool catIsEmpty(){
		if(cat.empty())
			return true;
		return false;
	}
	bool dogIsEmpty(){
		if(dog.empty())
			return true;
		return false;
	}
};
int main(){
	CatDogQueue cdq;
	
	Cat c;
	c.setType(1);
	Elem e1;
	e1.setPet(c);
	e1.setNum(1);

	Dog d;
	d.setType(2);
	Elem e2;
	e2.setPet(d);
	e2.setNum(2);

	cdq.push(e1);
	cdq.push(e2);
	Pet p = cdq.pollAll();
	cout << "pollall is :" << p.getType()<<endl;
	p = cdq.pollAll();
	cout << "pollall is :" << p.getType()<<endl;
	if(cdq.isEmpty())
		cout << "now,queue is empty "<<endl;
	if(cdq.catIsEmpty())
		cout << "now,cat queue is empty "<<endl;
	if(cdq.dogIsEmpty())
		cout << "now,dog queue is empty "<<endl;
	
	Cat c2;
	c2.setType(1);
	Elem e3;
	e3.setPet(c2);
	e3.setNum(3);
	cdq.push(e3);
	cout << "cat enqueue " << endl;
	if(!cdq.catIsEmpty()){
		cout << "当猫队列有猫时,直接返回猫:" <<cdq.pollCat().getType() << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值