题目:
思路:采用循环队列的思想,队首元素出队列,然后再进入队尾。
代码:
#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;
}