C-like language-queue-dance partner problem-(algorithm and data structure)

Note: This code uses C language, and it can run normally. A running interface is attached below the code area to solve the dance partner matching problem of queue examples.
Ideas:
1. Set up two queues to store men and women enqueue
2 . Suppose the records of men and women are stored in an array as input, and then scan the elements of the array in turn, and decide whether to enter the men's or women's team based on gender.
3. After the construction of these two queues is completed, the current head elements of the two teams are sequentially queued up as dance partners until a certain queue becomes empty.
At this time, if a team still has a waiting pair, the name of the waiter in the queue is output. This person will be the first person to get a partner when the next round of dance music starts.

Code area

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100  //定义队列最大空间
#define N 4   //4为参加人数,此处可以自由设定
typedef struct
{
    char name[50]; //姓名
    char sex;  //性别
}Dancer;

typedef struct
{
    Dancer *base;
    int front;  //下标值
    int rear;
}SqQueue;

enum Status{ERROR,OK};   //本代码用的是枚举类型,此处可根据具体要求替换
 
//队列初始化
Status InitQueue(SqQueue &Q)
{
    Q.base=(Dancer *)malloc(MAXSIZE*sizeof(Dancer));
    if(!Q.base) return ERROR;
    Q.front=Q.rear=0;
    return OK;
}
 
 
//入队列
Status EnQueue(SqQueue &Q,Dancer e)
{
    if((Q.rear+1)%MAXSIZE==Q.front) return ERROR;  //队满-》报错
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXSIZE;
    return OK;
}
 
 
//出队列
Status DeQueue(SqQueue &Q,Dancer *e)
{
    if(Q.front==Q.rear) return ERROR;  //队空--》报错
    *e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXSIZE;
    return OK;
}
 
 
//判断队列是否为空
int QueueEmpty(SqQueue Q)
{
    if(Q.rear==Q.front) 
		return OK;
    else
		return ERROR;
}
 
 
int main()
{
    char s;  
	int i=0;
    SqQueue Men,Women;  //定义两个队,分别为男队和女队
    Dancer D,p1,p2;  //D为舞者的基本信息,其中用p1,p2表示具体个人信息
    InitQueue(Men);
    InitQueue(Women);
    printf("input your name and sex(m/w),we will arrange partner in order:"); //请您输入姓名和性别(此处用m/w表示),然后将按照签到次序为您安排舞伴
    for(i=0;i<N;i++)
    {
        printf("\nname:");  //姓名
        scanf("%s",D.name);
        scanf("%c",&s); //这一步是保护下一个相邻的scanf,当然也可以同时输入 姓名和 性别
        printf("sex:");
        scanf("%c",&D.sex);  //性别
        if(D.sex=='m')
            EnQueue(Men,D);  //男士进男队
        if(D.sex=='w')
            EnQueue(Women,D);  //女士进女队
    }
 
 
    printf("\nthese are datas about partner:");  //下面是安排情况
        while(!QueueEmpty(Men)&&!QueueEmpty(Women))  //在均为非空的情况下
        {
            DeQueue(Men,&p1);  //第一个男士出队
            DeQueue(Women,&p2);  //第一个女士出队
            printf("\n%s(%c)&%s(%c)",p1.name,p1.sex,p2.name,p2.sex);  //输出信息
        }
        while(!QueueEmpty(Men))  //若男队有剩余
        {
            DeQueue(Men,&p1);
            printf("\n%s(%c) can match early in order",p1.name,p1.sex);  //那么在下一轮中,会依次优先安排舞伴
        }
         while(!QueueEmpty(Women))  //若女队有剩余
        {
            DeQueue(Women,&p2);
            printf("\n%s(%c) can match early in order",p2.name,p2.sex);   //那么在下一轮中,会依次优先安排舞伴
        }
		 printf("\n");
    return 0;
}

Test area

Insert picture description here

Published 57 original articles · praised 54 · visits 2352

Guess you like

Origin blog.csdn.net/September_C/article/details/104994945