银行业务模拟——链式队列(C++版)

大家好我是大一新生,如果代码有误或者需要改良的可以在下方评论中回复,谢谢观看!

#include<iostream>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define Status int

typedef int QElemType;

typedef struct Qnode{
    QElemType Data;
    struct Qnode *next;
}QNode;

typedef struct{
    QNode *front;
    QNode *rear;
}LinkQueue;

Status InitQueue(LinkQueue &Q)
{
    Q.front=Q.rear=new QNode;
    if(!Q.front)
    {
        cout<<"初始化失败!"<<endl;
        return ERROR;
    }
    Q.rear->next=NULL;
    return OK;
}

Status EnQueue(LinkQueue &Q,QElemType e)
{
    QNode *p;
    p=new QNode;
    p->Data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return OK;

Status DeQueue(LinkQueue &Q,QElemType &e)
{
    if(Q.front==Q.rear)
    {
        cout<<"队列为空!"<<endl;
        return ERROR;
    }
    QNode *p;
    p=Q.front->next;
    e=p->Data;
    Q.front->next=p->next;
    if(p==Q.rear)
    Q.rear=Q.front;
    delete p;
    return OK;
}

Status PaiQueue(LinkQueue &Q1,LinkQueue &Q2)
{
    LinkQueue Q3;
    InitQueue(Q3);
    int n,c=0;
    QElemType e;
    cout<<"input n:";
    cin>>n;
    while(n)
    {
        cin>>e;
        if(e%2==0)
        EnQueue(Q2,e);
        else
        EnQueue(Q1,e);
        n--;
    }
    while(Q1.front!=Q1.rear||Q2.front!=Q2.rear)
    {
        if(Q1.front!=Q1.rear&&c!=2)
        {
            DeQueue(Q1,e);
            EnQueue(Q3,e);
            c++;
        }
        else if(Q2.front!=Q2.rear)
        {
            DeQueue(Q2,e);
            EnQueue(Q3,e);
            c=0;
        }
        else
        c=0;
    }
    cout<<"队列为:";
    while(Q3.front!=Q3.rear)
    {
        DeQueue(Q3,e);
        cout<<e<<" ";
    }
    return OK;
}

main()
{
    LinkQueue Q1,Q2;
    InitQueue(Q1);
    InitQueue(Q2);
    PaiQueue(Q1,Q2);
}

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值