【舞伴问题 -- 链队列实现】

数据结构作业记录
如有BUG ,欢迎指出!!

#include<bits/stdc++.h>
using namespace std;
#define status int 
#define OK 1 
#define ERROR 0 

typedef struct  QNode{
    int sex;
    char name[100];
    struct QNode *next;
}QNode;

typedef struct LinkQueue{
    QNode *rear=new QNode;
    QNode *front=new QNode;
    status init(){  // 初始化 
        front->next=rear;
        rear->next=front;
        return OK;
    }
    bool IsEmpty(){  // 队列是否为空 
        return front->next==rear;
    }
    bool Pop(QNode& e){// 弹出队顶元素 
        QNode *p;
        if(IsEmpty()) return ERROR ;
        e.sex=front->next->sex; strcpy(e.name,front->next->name);
        p=front->next;
        front->next=p->next;
        delete p;
        return OK;
    }
    bool Push(QNode temp){  // 输入队列 
        QNode *p;  QNode *t=new QNode;
        t->sex=temp.sex; strcpy(t->name,temp.name);
        p=rear->next;
        p->next=t;
        t->next=rear;
        rear->next=t;
        return OK;
    }
    bool Top(QNode &e){ // 得到队列顶元素 
         if(IsEmpty()) return ERROR;
         e.sex=front->next->sex; strcpy(e.name,front->next->name);
         return OK;
    } 
}LinkQueue;

LinkQueue Queue,MQueue,FQueue; // 分别为 所有dancer ,女dancer,男dancer 
bool DancerAssign(){
    Queue.init();MQueue.init();FQueue.init(); // 初始化 
    printf("Please input the nummber of dancer:");
    int n;scanf("%d",&n);
    char sex[10];QNode p,e;
    int id=1;
    for(int i=1;i<=n;i++){
        printf("Please input the %d dancer information :\n",id++);
        printf("name :");scanf("%s",p.name);
        printf("sex :"); scanf("%s",sex);  // 要输入中文 
        if(strcmp(sex,"男")==0) 
            p.sex=1;
        else
            p.sex=0; 
        Queue.Push(p);
    }
    while(!Queue.IsEmpty()){
        Queue.Pop(e);
        if(e.sex==1) 
            FQueue.Push(e);
        else 
            MQueue.Push(e);
    }
    id=1;
    while(!MQueue.IsEmpty()&&!FQueue.IsEmpty()){
        printf("The %d dancing partners are :",id++);
        MQueue.Pop(e) ; printf("%s and ",e.name);
        FQueue.Pop(e) ; printf("%s .\n",e.name) ;
    }
    if(!MQueue.IsEmpty()) {
        MQueue.Top(e);
        printf("The first woman to get a partner is : %s .\n",e.name);
    }else if(!FQueue.IsEmpty()){
        FQueue.Top(e);
        printf("The first man to get a partner is : %s .\n",e.name);
    }
} 
int main(){
    puts("----------Welcome to use----------");
    while(1){
        char op[10];
        puts("\n A :the party begin !!");
        puts(" Q :End .");
        printf("Please input command: ");
        scanf("%s",op);
        if(op[0]=='A')
            DancerAssign();
        else if(op[0]=='Q') 
            break;
        else
            puts("Your command is wrong ,please input again !!\n");
    }
    puts("Welcome to use again .\nBye ~ ");
    return 0 ; 
} 
    原文作者:舞伴问题
    原文地址: https://blog.csdn.net/qq_37383726/article/details/78488933
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞