队列模拟

#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED

#include <stdio.h>
#include <stdbool.h>

#define MAXSIZE 10

typedef struct
{
    long arrive;
    int processtime;
}Item;

typedef struct node
{
    Item item;
    struct node *next;
}Node;

typedef struct
{
    Node *front;
    Node *rear;
    int items;   //记录队列长度
}Queue;

void InitializeQueue(Queue *pq);
bool QueueisEmpty(const Queue *pq);
bool QueueisFull(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Queue *pq,Item item);
bool DeQueue(Item *pitem,Queue *pq);
void EmptyTheQueue(Queue *pq);

void InitializeQueue(Queue *pq)
{
    pq->front=NULL;
    pq->rear=NULL;
    pq->items=0;
}

bool QueueisEmpty(const Queue *pq)
{
    return pq->items==0;
}

bool QueueisFull(const Queue *pq)
{
    return pq->items==MAXSIZE;
}

int QueueItemCount(const Queue *pq)
{
    return pq->items;
}

bool EnQueue(Queue *pq,Item item)
{
    if(QueueisFull(pq)) return false;
    Node *pnew;
    pnew=(Node *)malloc(sizeof(Node));
    if(pnew==NULL)
    {
        fprintf(stderr,"Unable to allocate memory!\n");
        exit(0);
    }

    if(QueueisEmpty(pq)) pq->front=pnew;
    else pq->rear->next=pnew;
    pnew->next=NULL;
    pq->rear=pnew;
    pq->items++;
    pnew->item=item;

    return true;
}

void CopyToNode(Item item,Node *p)
{
    p->item=item;
}

bool DeQueue(Item *pitem,Queue *pq)
{
    if(QueueisEmpty(pq)) return false;

    *pitem=pq->front->item;
    Node *psave;

    psave=pq->front;
    pq->front=pq->front->next;
    free(psave);
    pq->items--;
    if(QueueisEmpty(pq))
        pq->rear=NULL;

    return true;
}

void EmptyTheQueue(Queue *pq)
{
    Item ha;
    while(!QueueisEmpty(pq))
    {
        DeQueue(&ha,pq);
    }
    /*Node *psave;
    while(pq->front)
    {
        psave=pq->front->next;
        free(pq->front);
        pq->front=psave;
    }*/
}
#endif // QUEUE_H_INCLUDED
/*假设一个摊位前最多排10人,并且顾客随机在摊位上停留(1分钟,2分钟,3分钟) 那么Sigmund平均每小时要接待多少顾客?
  每位顾客要花多长时间?排队等待的顾客平均有多少人?*/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include <time.h>
#define MIN_PER_HR 60.0

bool newcustomer(double x);   //以随机数和rand_max来随机判断是否有顾客来
Item customertime(long when);  //when是顾客加入的时间,在函数中随机生成1~3确立顾客停留时间
void InitializeQueue(Queue *pq);
bool QueueisEmpty(const Queue *pq);
bool QueueisFull(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Queue *pq,Item item);
bool DeQueue(Item *pitem,Queue *pq);
void EmptyTheQueue(Queue *pq);

int main()
{
    Queue line;
    Item temp;        //中间参数,可以用它来保留删除的队列,被删的队列的停留时间就是等待时间或拿他来做添加队列的形参
    int hours;
    int perhours;    //自己设置一个每小时客人数
    long cycle,cyclelimit;    //一分钟检查一次是否有客人,limit是计算的总时间
    long turnaways=0;    //被拒的客人(队列满了)
    long customers=0;    //加入队列的客人
    long served=0;       //服务的客人数
    long sum_line=0;     //队列的总长
    long wait_time=0;    //开始队列为空等待时间为0
    double min_per_cust; //顾客到来的平均时间
    long line_wait=0;    //队列累计的等待时间

    InitializeQueue(&line);
    srand((unsigned int)time(0));  //随时间重置随机种子
    puts("请输入测试时长:");
    scanf("%d",&hours);
    puts("请输入每小时的平均顾客数:");
    scanf("%d",&perhours);
    cyclelimit=MIN_PER_HR*hours;
    min_per_cust=MIN_PER_HR/perhours;

    for(cycle=0;cycle<cyclelimit;cycle++)   //每分钟看一次有无客人到来,直到测试时间结束
    {
        if(newcustomer(min_per_cust))
        {
            if(QueueisFull(&line)) turnaways++;
            else
            {
                temp=customertime(cycle);
                EnQueue(&line,temp);
                customers++;
            }
        }
        if(wait_time<=0 && !QueueisEmpty(&line))
        {
            DeQueue(&temp,&line);
            wait_time=temp.processtime;
            line_wait+=cycle-temp.arrive;
            served++;
        }
        if(wait_time>0) wait_time--;  //等待时间减一分钟
        sum_line+=QueueItemCount(&line);
    }

    if(customers>0)
    {
        printf("接收的顾客数:%ld\n",customers);
        printf("服务的顾客数:%ld\n",served);
        printf("拒收的顾客数:%ld\n",turnaways);
        printf("队伍的平均长度为:%lf\n",(double)sum_line/cyclelimit);
        printf("平均等待时长:%lf\n",(double)line_wait/served);
    }
    else
    {
        puts("糟糕至极没有顾客");
    }
    EmptyTheQueue(&line);
    puts("程序完成!");
    return 0;
}

//x是顾客到来的平均时间,如果一分钟内有顾客来就返回true
bool newcustomer(double x)
{
    if(rand()*x/RAND_MAX<1) return true;
    else return false;
}

Item customertime(long when)
{
    Item cust;

    cust.processtime=rand()%3+1;
    cust.arrive=when;

    return cust;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值