数据结构之SWUSTOJ1027: 舞伴问题

题目:

思路:采用循环队列的思想,队首元素出队列,然后再进入队尾。
 

代码:

#include<iostream>
#include<string.h>
using namespace std;
typedef struct SList
{
	char data;
	struct SList* next;
}SL;
typedef struct Queue
{
	SL* front;
	SL* rear;
}QU;
QU* QueueCreate(char* arr)
{
	QU* q = (QU*)malloc(sizeof(QU));
	SL* cur = (SL*)malloc(sizeof(SL));
	q->front = q->rear = cur;
	int n = strlen(arr);
	for (int i = 0; i < n; i++)
	{
		SL* newnode = (SL*)malloc(sizeof(SL));
		newnode->data = arr[i];

		cur->next = newnode;
		cur = newnode;
		newnode->next = q->front;//设计循环队列
	}
	q->rear = cur;
	return q;
}
void EnQueue(QU* ps,char x)//入队列
{
	ps->rear->next->data = x;
	ps->rear = ps->rear->next;
}
void PopQueue(QU* ps)//出队列
{
	ps->front = ps->front->next;
}
char QueueFront(QU* ps)//找队头的元素
{
	return ps->front->next->data;
}
int main()
{
	int m = 0;
	cin >> m;
	char arr1[50] = { 0 };
	for (int i = 0; i < m; i++)
	{
		cin >> arr1[i];//读数据时需要一个一个读,注意每个字符中间有一个空格
	}
	QU* man = QueueCreate(arr1);
	
	int n = 0;
	cin >> n;
	char arr2[50] = { 0 };
	for (int i = 0; i < n; i++)
	{
		cin >> arr2[i];//读数据时需要一个一个读,注意每个字符中间有一个空格
	}
	QU* woman = QueueCreate(arr2);

	int k = 0;
	cin >> k;
	for (int i = 1; i < k; i++)//注意这题的i要从等于1开始
	{
		char c1 = QueueFront(man);//先用元素记录一下队首元素的值
		PopQueue(man);
		EnQueue(man, c1);//出队列过后入队尾
		char c2 = QueueFront(woman);
		PopQueue(woman);
		EnQueue(woman,c2);
	}
	printf("%c %c", QueueFront(man), QueueFront(woman));

	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值