Eip_Nit_AC
2019-06-27 00:32
采纳率: 75%
浏览 356

循环队列 银行排队疑问?

#include<stdio.h>
#include<stdlib.h>
#define MaxWindow 30
#define MaxProc 1
#define ERROR -1

struct People
{
    int T;
    int P;
};
typedef struct People ElementType;

struct QNode 
{
    ElementType *Data;
    int Front, Rear;
    int MaxSize;
};
typedef struct QNode *Queue;

Queue CreatQueue(int MaxSize)
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = MaxSize;
    return Q;
}

bool IsFull(Queue Q)
{
    return((Q->Rear + 1) % Q->MaxSize == Q->Front);
}

bool AddQ(Queue Q, ElementType X)
{
    if (IsFull(Q))
    {
        printf("队列满\n");
        return false;
    }
    else
    {
        Q->Rear = (Q->Rear + 1) % Q->MaxSize;
        Q->Data[Q->Rear] = X;
        return true;
    }
}

bool IsEmpty(Queue Q)
{
    return(Q->Front == Q->Rear);
}

ElementType DeleteQ(Queue Q)
{

    ElementType data;
    if (IsEmpty(Q))
    {   printf("队列空");
    return ERROR;
    }
    else
    {
        Q->Front = (Q->Front + 1) % Q->MaxSize;
        return Q->Data[Q->Front];;
    }
}

void DestroyQueue(Queue Q)
{
    free(Q);
}

int FindNextWindow(int W[], int k, int *WaitTime)
{
    int WinAvail;
    int MinW = MaxProc + 1;
    int i;
    for (i = 0; i < k; i++)
        if (W[i] < MinW)
        {
        MinW = W[i];
        WinAvail = i;
        }
    *WaitTime = MinW;
    for (i = 0; i < k; i++)
        W[i] -= MinW;
    return WinAvail;
}

double QueueingAtBank(Queue Q, int N)
{
    struct People Next;
    int k;
    int TotalTime;
    int CurrentTime;
    int Window[MaxWindow];
    int WaitTime;
    int WinAvail;
    int i, j;
    scanf("%d", &k);

    for (i = 0; i < k; i++)
        Window[i] = 0;
    TotalTime = 0;

    while (!IsEmpty)
    {
        WinAvail = FindNextWindow(Window, k, &WaitTime);
        CurrentTime += WaitTime;
        Next = DeleteQ(Q);
        if (CurrentTime >= Next.T)
            TotalTime += (CurrentTime - Next.T);
        else
        {
            WaitTime = Next.T - CurrentTime;
            for (j = 0; j < k; j++)
            {
                Window[j] -= WaitTime;
                if (Window[j] < 0)
                    Window[j] = 0;
            }
            CurrentTime = Next.T;
        }
        Window[WinAvail] = Next.P;
    }
    return((double)TotalTime / (double)N);
}

int main()
{
    int N;
    Queue Q;
    int i;
    ElementType X;
    scanf("%d", &N);
    Q = CreatQueue(N);
    for (i = 0; i < N; i++)
    {
        scanf("%d %d", &X.T, &X.P);
        AddQ(Q, X);
    }
    printf("Average waiting time = %.lf minute(s).\n", QueueingAtBank(Q, N));

    DestroyQueue(Q);
    return 0;
}

图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • qq_34495994 2019-06-27 11:41
    已采纳

    Elementtype比其他基础类型更有利于阅读程序的需要,可以定义成多种类型,本例中返回值类型与你定义的Elementtype类型不一致,故报错。

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题