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

数据结构作业记录
如有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 ; 
} 
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值