舞伴问题描述如下:
就是说有个PARTY舞会需要进行跳舞,当然是男的跟女的搭档了。就是男的一个队列,女的一个队列,然后互相出队列,进行匹配跳舞,如果男的队列跟女的队列人一样多,则输出 匹配完美结束,若最后男的剩下或者女的剩下,此时输出剩下的队列中第一个人的名字,比如I,则输出 I还在干等着呢!大体意思就是这!
代码如下:(还是没有区分.h文件,大家懂的,分不分在这里也没有什么意义可言)
}
}
template<class T>
T LinkQueue<T>::DelQueue()
{
//首先得判断是否为空队列
assert(!IsEmpty());
LinkQueueNode<T>* old=front;
T data=old->data;//保留原对头数据
front=front->link;//移动对头指针
if(back==old)
back=NULL;
delete old;
return data;
}
template<class T>
T& LinkQueue<T>::GetFront()
{
assert(!IsEmpty());//断言,这东西挺好使滴
return front->data;
}
template<class T>
void LinkQueue<T>::MakeEmpty()
{
while(!IsEmpty())
{
this->DelQueue();
}
}
template<class T>
bool LinkQueue<T>::IsEmpty()
{
return front==NULL;
}
#include<string>
using namespace std;
struct dancer//一会就让队列中存储这种结构体类型
{
string name;
char sex;
};
int main()
{
cout<<"请输入舞伴总数量:"<<endl;
int num;
cin>>num;
LinkQueue<dancer> mdancer;
LinkQueue<dancer> fdancer;
for(int i=0;i<num;i++)
{
cout<<"请输入舞者性别(f(女) or m(男))及姓名:"<<endl;
char sex;
cin>>sex;
string name;
cin>>name;
dancer newdancer;
newdancer.name=name;
newdancer.sex=sex;
if(sex=='f')//female
{
fdancer.EnQueue(newdancer);
}
if(sex=='m')//male
{
mdancer.EnQueue(newdancer);
}
}
while( (!mdancer.IsEmpty()) && (!fdancer.IsEmpty() ) )//男女两个队列都有人
{
cout<<mdancer.DelQueue().name<<"/t<---配对--->/t"<<fdancer.DelQueue().name<<endl;
}
if(!mdancer.IsEmpty())//剩下的帅锅
{
cout<<mdancer.GetFront().name<<"先生还在干等着呢!"<<endl;
}
else if(!fdancer.IsEmpty())//都是剩下的霉女
{
cout<<fdancer.GetFront().name<<"女士还在干等着呢!"<<endl;
}
else
cout<<"配对完美结束!"<<endl;
return 0;
}