天天看点

【c++实现】模拟银行叫号系统

#include<iostream>

using namespace std;

  struct QNode{

char id;//当前的号码

QNode *next;//指向下一个节点

};

class LoopQueue{

private:

QNode *head;//指向的指针

QNode *rear;//指向尾部的指针

int length;//长度    用于取号的时候

public:

void init();

void EnQueue(char n);//入队

bool DeQueue(QNode &e);//出队

void add(QNode *p);

void removenode();

//void tranverse();//遍历

// bool SetEmpty();  //置空

int getLength();

QNode * gethead(){

return head;

}

};

//没有头结点

//初始化

void LoopQueue::init(){

head=NULL; rear=NULL;

length=0;

}

//入队

void LoopQueue::add(QNode *e){

     QNode *p=e;//让指针p指向e;等待接入节点

    if(length==0){//空队列

        head=p;

        rear=p;length++;

    }else{

         rear->next=p;

        rear=rear->next;//rear 移动到新接入的节点上

        length++;

    }

}

void LoopQueue::EnQueue(char n){//改一改自己的风格,传个节点吧!!!娜娜棒棒的

QNode *p=new QNode;

p->id=n;//传入的值作为  节点p的id值   封装好数据

add(p);

}

//出队

bool LoopQueue::DeQueue(QNode &e){//e是要取值的节点

if(length==0){//空

return false;

}

e.id=head->id;//叫号时候的号码  用e把他传出来

QNode *p=head;//再用一个指针  指向头  准备释放掉这个节点

head=head->next;//移动head这个指针

return true;

}

void LoopQueue::removenode(){

    if(length==0){

            cout<<"没有了!!!"<<endl;

    }

        QNode *p = head;

        head=p->next;//移动head

        delete p;

        length--;

}

 int  LoopQueue::getLength(){

  return length;

 }

#include "BankHead.h"

int main(){

int a,i=1;

QNode e;

LoopQueue queue;

queue.init();//初始化

cout<<"请选择:\n1、欢迎光临!请排队取号,等候叫号!"<<endl

        <<"2、服务号空闲,开始叫号" <<endl

        <<"3、服务结束!退出系统!"<<endl;

while(cin>>a){

        switch(a){

            case 1:

                queue.EnQueue(i++);

cout<<"欢迎光临!"<<"综合业务"<<endl

<<"您的号码是"<<i-1<<endl

//<<"您的号码是"<<queue.getLength()<<endl

<<"您前面有"<<queue.getLength()-1<<"人等候"<<endl;break;

            case 2:

                if(queue.DeQueue(e)){

                    cout<<"请"<<e.id<<"号客户到窗口办理!"<<endl;

                }else{

                    cout<<"效率不错!所有客户问题解决完毕!\n你可以稍稍休息一下\n";

                }break;

            case 3:cout<<"小娜娜,期待您下次光临哈!"<<endl;break;

        }

}

return 0;

}

继续阅读