舞伴问题

结构体等

typedef int status;
typedef string Qelemtype; 
#define OK 1
#define ERROR 0
#define OVEFLOW -2

typedef struct QNode
{
	Qelemtype data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct 
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

初始化

status InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new QNode;
	Q.front->next =	NULL;
	return OK;
}

入队

status EnQueue(LinkQueue &Q,Qelemtype e)
{
	QueuePtr p;
	p = new QNode;
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return OK;
}

出队

status DeQueue(LinkQueue &Q, Qelemtype &e)
{
	if(Q.front == Q.rear) return ERROR;
	QueuePtr p;
	p = Q.front->next;
	Q.front->next = p->next;
	if(p == Q.rear) Q.rear = Q.front;
	delete p;
	return OK;
}

队长

status duichang(LinkQueue &Q)
{
	if(Q.front == Q.rear) return ERROR;
	int i = 1;
	while(Q.front->next != Q.rear)
	{
		i++;
		Q.front->next = Q.front->next->next;
	}
	return i;
}

输出队列

status scd(LinkQueue &Q)
{
	if(Q.front == Q.rear) return ERROR;
	do
	{
		
		cout<<Q.front->next->data ;
		if(Q.front->next == Q.rear) break; 			//要点 
		Q.front->next=Q.front->next->next;
	}while(Q.front !=Q.rear);
	return OK;
}

主函数

int main()
{
	int a;
	string e;
	LinkQueue N,L;
	InitQueue(N);
	InitQueue (L);
	cout<<"请输入男生名字。输入:#结束"<<endl;
	cin>>e;
	while(e!="#")
	{
		EnQueue(N,e);
		cout<<"请输入男生名字。输入:#结束"<<endl;
		cin>>e;
	 } 
	 cout<<"请输入女生名字。输入:#结束"<<endl;
	cin>>e; 
	 while(e!="#")
	{
		EnQueue(L,e);
		cout<<"请输入女生名字。输入:#结束"<<endl;
		cin>>e;
	 } 
	 while(1)
	 {
	 	if(N.front == N.rear||L.front == L.rear)
	 	break;
	 	DeQueue(N,e);
	 	DeQueue(L,e);
	 }
	 if(N.front == N.rear&&L.front == L.rear)
	 {
	 	cout<<"完美匹配"<<endl; 
	 }
	 else if(L.front == L.rear)
	 {
	 	a = duichang(N);
	 	cout<<a<<"位先生在等待女伴"<<endl<<"分别是"; 
	 	scd(N);
	 }
	 else
	 {
			a = duichang(L);
	 	cout<<a<<"位女士在等待男伴"<<endl<<"分别是"; 
	 	scd(L);
	 }
	 return OK;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值