数据结构上机题——队列及其应用

由用户输入n个10以内的数,每当输入i(0≤i≤9),就把它插入到第i号队列中。最后把10个队列中非空队列,按队列号从小到大的顺序串接成一条链,并输出该链的所有元素。

// 由用户输入n个10以内的数,每当输入i(0≤i≤9),
// 就把它插入到第i号队列中。最后把10个队列中非空队列,按队列号从小到大的顺序串接成一条链,并输出该链的所有元素。

#include<iostream>
using namespace std;
template<class T>
class LinkNode{
public:
    T data;
    LinkNode<T> *pNext;
    LinkNode(T data){
    this->data = data;
    }
};
template<class T>
class LinkQueue{
private:
    LinkNode<T> *pfront;
    LinkNode<T> *prear;
    int size;
public:
    LinkQueue(int size = 0){
        this->pfront = prear = NULL;
        this->size = 0;
    }
    bool Isempty(){
        if(size == 0)
            return true;
        else
            return false;
    }
    void Show(){
        LinkNode<T> *p = pfront;
        if(p == NULL){
            cout  << "队空";
            return ;
        }
        else
        for(p;p!=prear->pNext;p = p->pNext)
            cout << p->data << "   ";
    }
    void Enqueue(T data);
    void Enqueue2(LinkNode<T> *p);
    LinkNode<T>* Outqueue();
    void Zhuanyi(LinkQueue<T> start);
};
 
template<class T>
void LinkQueue<T> :: Enqueue(T data){
    LinkNode<T> *p = new LinkNode<T>(data);
    if(prear == NULL)
        this->prear = pfront = p;
    else{
        prear->pNext = p;
        prear = p;
    }
    size += 1;
}
template<class T>
void LinkQueue<T> :: Enqueue2(LinkNode<T> *p){
    if(prear == NULL)
        this->prear = pfront = p;
    else{
        prear->pNext = p;
        prear = p;
    }
    size += 1;
}
template<class T>
LinkNode<T>* LinkQueue<T> :: Outqueue(){
    if(pfront != NULL){
        LinkNode<T> *p = pfront;
        pfront = p->pNext;
        size -= 1;
        return p;
    }
    else{
        cout << "队列为空,无法出队!" << endl;
 
        return NULL;
    }
}
template<class T>
void LinkQueue<T> :: Zhuanyi(LinkQueue<T> start){
    LinkNode<T> *p = NULL;
    while(!start.Isempty()){
        p = start.Outqueue();
        Enqueue2(p);
    }
}
int main()
{
    LinkQueue<int> que[11] ;//= new[LinkQueue<char>](10); 这个到底咋写来着
    cout << "请输入0到9之间的任意数字,中间用空格隔开(输入其他任意值结束):" << endl;
    int x;
    cin >> x;
    while(x>=0 && x<=9)
    {
        que[x].Enqueue(x);
        cin >> x;
    }
    for(int i = 0;i<=9;i++){
        cout << "第" << i << "个队列:";
        que[i].Show();
        cout << endl ;
    }
    for(int i=0;i<=9;i++){
        if(!que[i].Isempty())
        que[10].Zhuanyi(que[i]);
    }
    cout << "连接后:" << endl;
    que[10].Show();
return 0;
}

设计一个环形队列,用front和rear分别作为队头和队尾指针,另外用一个变量tag表示队列是空(0)还是不空(1),这样就可以用front==rear作为队满的条件。要求设计队列的相关基本运算算法。

// 设计一个环形队列,用front和rear分别作为队头和队尾指针,
// 另外用一个变量tag表示队列是空(0)还是不空(1),这样就可以用front==rear作为队满的条件。要求设计队列的相关基本运算算法。

#include<iostream>
template<class T>
class queue{
	private:
		int maxsize;
		int front;
		int rear;
		bool tag;
		T *data;
	public:
		queue(int size){
			maxsize = size;
			front = 0;
			rear = 0;
			tag = false;
			data = new T [size];
		}
		~queue(){
			delete data;
		}
		void clear(){            //清除
			rear = front;
			tag = false;
		}
	    bool enqueue (T tmp){    //入队
	    	if(full()){
	    		return false;
	    	}
	    	else{
		    	if(empty()){
                    tag = true;
		    	}
		    	data[rear] = tmp;
		    	rear = (rear+1) % maxsize;
		    	return true;
	    	}
	    }
	    bool dequeue (T &tmp){   //出队
	    	if(empty()){
	    		return false;
	    	}
	    	else{
		    	tmp = data[front];
		    	front = (front+1) % maxsize;
		    	if(front == rear){
		    		tag = false;
		    	}
	    		return true;
	    	}
	    }
	    bool getfront (T &tmp){
	    	if(empty()){
	    		return false;
	    	}
	    	else {
	    		tmp = data[front];
	    		return true;
	    	}
	    }
		bool empty(){
			if (rear == front && tag == false){
				return true;
			}
			else{
				return false;
			}
		}
		bool full(){
			if (rear == front && tag == true){
				return true;
			}
			else{
				return false;
			}
		}
};

编程实现银行叫号系统,若需使用队列,请使用作业(1)中自己定义的,不要使用STL的。

// 编程实现银行叫号系统,若需使用队列,请使用作业(1)中自己定义的,不要使用STL的
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <list>

using namespace std;

class User
{
public:
    User();
    User(int id,bool isWait,int ArriveTime,wchar_t type,int ServerTime)
        :id(id),isWait(isWait),ArriveTime(ArriveTime),type(type),ServerTime(ServerTime){};
    ~User(){};

    virtual void getServed();
public:
    int id;
    int isWait;
    int ArriveTime;
    wchar_t type;
private:
    int ServerTime;

};

class NormalUser: public User{
public:
    NormalUser(int id,bool isWait,int ArriveTime,wchar_t type,int ServerTime)
        :User(id,isWait,ArriveTime,type,ServerTime){};
    void getServed();
};

class VIPUser: public User{
public:
    VIPUser(int id,bool isWait,int ArriveTime,wchar_t type,int ServerTime)
        :User(id,isWait,ArriveTime,type,ServerTime){};
    void getServed();
};

class OfficialUser: public User{
public:
    OfficialUser(int id,bool isWait,int ArriveTime,wchar_t type,int ServerTime)
        :User(id,isWait,ArriveTime,type,ServerTime){};
    void getServed();
};

void User::getServed(){
    if(type=='n'){
        cout<<endl<<endl<<"---普通用户"<<id<<" 服务完毕!---"<<endl;
    }
    if(type=='v'){
        cout<<endl<<endl<<"--- VIP用户"<<id<<" 服务完毕!---"<<endl;
    }
    if(type=='o'){
        cout<<endl<<endl<<"---对公用户"<<id<<" 服务完毕!---"<<endl;
    }
}

void NormalUser::getServed(){
    cout<<endl<<endl<<"---普通用户"<<id<<" 服务完毕!---"<<endl;
}

void VIPUser::getServed(){
    cout<<endl<<endl<<"--- VIP用户"<<id<<" 服务完毕!---"<<endl;
}

void OfficialUser::getServed(){
    cout<<endl<<endl<<"---对公用户"<<id<<" 服务完毕!---"<<endl;
}


class BankWindow
{
public:
    BankWindow(){};
    BankWindow(bool isBusy,int id,wchar_t type)
        :isBusy(isBusy),id(id),type(type){};
    ~BankWindow(){};

    bool isBusy;
    int id;
    User *client;
    wchar_t type;
    int serviceStartTime;

    virtual void HandleUser();
};

class NormalBankWindow: public BankWindow{
public:
    NormalBankWindow(bool isBusy,int id,wchar_t type)
        :BankWindow(isBusy,id,type){};
    void HandleUser();
};

class VIPBankWindow: public BankWindow{
public:
    VIPBankWindow(bool isBusy,int id,wchar_t type)
        :BankWindow(isBusy,id,type){};
    void HandleUser();
};

class OfficialBankWindow: public BankWindow{
public:
    OfficialBankWindow(bool isBusy,int id,wchar_t type)
        :BankWindow(isBusy,id,type){};
    void HandleUser();
};

void BankWindow::HandleUser(){
    cout<<endl<<"BankWindow handle user."<<endl;
}

void NormalBankWindow::HandleUser(){
    cout<<endl<<"---"<<this->id<<"号窗口 接收 用户"<<client->id<<"---"<<endl<<endl;
}

void VIPBankWindow::HandleUser(){
    cout<<endl<<"---"<<this->id<<"号窗口 接收 用户"<<client->id<<"---"<<endl<<endl;
}

void OfficialBankWindow::HandleUser(){
    cout<<endl<<"---"<<this->id<<"号窗口 接收 用户"<<client->id<<"---"<<endl<<endl;
}

class Simulater
{
public:
    Simulater(){};
    ~Simulater(){};

    queue<NormalUser> NormalUserQueue;
    queue<VIPUser> VIPUserQueue;
    queue<OfficialUser> OfficialUserQueue;

    list<NormalBankWindow> nbw;
    list<VIPBankWindow> vbw;
    list<OfficialBankWindow> obw;

    void customerEnter();
    void simulateCustomerEnter();
    void simulateCallCustomer();
    void Simulate();
    void display();
};

int t=0;
bool Flag=false;
int customerEnterMatrix[9][3]={
    {3,1,1},
    {5,0,0},
    {0,0,0},
    {5,1,0},
    {0,0,0},
    {0,0,0},
    {0,0,0},
    {0,0,0},
    {0,0,0}
};

void Simulater::customerEnter(){//模拟用户入队
    static int h=1001;
    int i=0,j=0,k=0;
    i=customerEnterMatrix[t/2][0];
    j=customerEnterMatrix[t/2][1];
    k=customerEnterMatrix[t/2][2];
    for(int n=0;n<i;n++){
        NormalUserQueue.push(NormalUser(h++,true,t,'n',0));
        cout << "普通客户 进入排队,  编号:" <<NormalUserQueue.back().id<< endl;
    }
    for(int n=0;n<j;n++){
        VIPUserQueue.push(VIPUser(h++,true,t,'v',0));
        cout << "VIP客户  进入排队,  编号:"<<VIPUserQueue.back().id<<endl;
    }
    for(int n=0;n<k;n++){
        OfficialUserQueue.push(OfficialUser(h++,true,t,'o',0));
        cout << "对公客户 进入排队,  编号:" <<OfficialUserQueue.back().id<< endl;
    }
}

void Simulater::simulateCustomerEnter(){//银行处理用户序列
    //普通用户窗口
    list<NormalBankWindow>::iterator itor;
    itor=nbw.begin();
    while(itor!=nbw.end()){
        cout <<"窗口号:"<<itor->id;
        if (itor->isBusy == true){
            cout<< "  -正在服务-  "<< " 窗口类型: " << "普通 "<< "客户编号: " << itor->client->id;
            if (t - itor->serviceStartTime >= 4){
                itor->client->getServed();
                Flag=true;
                itor->isBusy = false;//将当前位置的User清空
            }
        }
        else{
            cout << "  -等待服务-  ";
        }
        cout<<endl;
        itor++;
    }
    //VIP用户窗口
    cout <<"窗口号:"<<vbw.begin()->id;
    if (vbw.begin()->isBusy == true){
        cout<< "  -正在服务-  "<< " 窗口类型: " << " VIP "<< "客户编号: " << vbw.begin()->client->id;
        if (t - vbw.begin()->serviceStartTime >= 4){
            vbw.begin()->client->getServed();
            Flag=true;
            vbw.begin()->isBusy = false;//将当前位置的User清空
        }
    }
    else{
        cout << "  -等待服务-  ";
    }
    cout<<endl;
    //对公用户窗口
    cout <<"窗口号:"<<obw.begin()->id;
    if (obw.begin()->isBusy == true){
        cout<< "  -正在服务-  "<< " 窗口类型: " << "对公 "<< "客户编号: " << obw.begin()->client->id;
        if (t - obw.begin()->serviceStartTime >= 4){
            obw.begin()->client->getServed();
            Flag=true;
            obw.begin()->isBusy = false;//将当前位置的User清空
        }
    }
    else{
        cout << "  -等待服务-  ";
    }
    cout<<endl;

}
//银行处理的时间是4秒,占两个周期
void Simulater::simulateCallCustomer(){//呼叫用户,从队列里面呼叫
    //普通用户窗口
    list<NormalBankWindow>::iterator itor;
    itor=nbw.begin();
    while(itor!=nbw.end()){
        if (itor->isBusy == false && !NormalUserQueue.empty()){
            itor->client = &NormalUserQueue.front();
            cout<<itor->client->id<<", 请进入"<<itor->id<<"号普通窗口服务" ;
            itor->HandleUser();
            itor->isBusy = true;
            itor->serviceStartTime = t;
            NormalUserQueue.pop();
        }
        itor++;
    }
    //VIP用户窗口
    if (vbw.begin()->isBusy == false){
        if (!VIPUserQueue.empty()){
            vbw.begin()->client = &VIPUserQueue.front();
            vbw.begin()->isBusy = true;
            vbw.begin()->serviceStartTime = t;
            VIPUserQueue.pop();
            cout <<vbw.begin()->client->id<< ", 请进入3号VIP窗口服务" ;
            vbw.begin()->HandleUser();
        }
        else if (!NormalUserQueue.empty()){
            vbw.begin()->client = &NormalUserQueue.front();
            vbw.begin()->isBusy = true;
            vbw.begin()->serviceStartTime = t;
            NormalUserQueue.pop();
            cout <<vbw.begin()->client->id<< ", 请进入3号VIP窗口服务" ;
            vbw.begin()->HandleUser();
        }
        else{}
    }
    //对公用户窗口
    if (obw.begin()->isBusy == false){
        if (!OfficialUserQueue.empty()){
            obw.begin()->client = &OfficialUserQueue.front();
            obw.begin()->isBusy = true;
            obw.begin()->serviceStartTime = t;
            OfficialUserQueue.pop();
            cout <<obw.begin()->client->id<< ", 请进入4号对公窗口服务" ;
            obw.begin()->HandleUser();
        }
        else if (!NormalUserQueue.empty()){
            obw.begin()->client = &NormalUserQueue.front();
            obw.begin()->isBusy = true;
            obw.begin()->serviceStartTime = t;
            NormalUserQueue.pop();
            cout <<obw.begin()->client->id<< ", 请进入4号对公窗口服务" ;
            obw.begin()->HandleUser();
        }
        else {}
    }
}

void Simulater::Simulate(){
    nbw.push_back(NormalBankWindow(false,0,' '));
    nbw.push_back(NormalBankWindow(false,1,' '));
    nbw.push_back(NormalBankWindow(false,2,' '));
    vbw.push_back(VIPBankWindow(false,3,' '));
    obw.push_back(OfficialBankWindow(false,4,' '));
    cout << "------------------------模  拟  开  始---------------------" << endl;

    while(t<=16){//模拟时间是否结束
        cout<<"当"<<t<<"s时 :"<<endl;
        customerEnter();
        simulateCallCustomer();
        simulateCustomerEnter();
        if(Flag){
            Flag=false;
            //customerEnter();
            simulateCallCustomer();
            simulateCustomerEnter();
        }
        cout<<endl<<"**********************************************************"<<endl;

        //时间增加
        t+=2;
    }

}

int main(){
    Simulater SSS;
    SSS.Simulate();
    return 0;
}
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值