假设Sigmund Landers在商业街设置了一个提供建议的摊位,顾客可以购买1分钟,2分钟,或3分钟的建议,为确保交通每个摊位前排队等待的顾客最多10人,用两个队列模拟两个摊位

/假设Sigmund Landers在商业街设置了一个提供建议的摊位,顾客可以购买1分钟,2分钟,或3分钟的建议,为确保交通每个摊位前排队等待的顾客最多10人,用两个队列模拟两个摊位/

#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10

typedef struct
{
    int items[MAX_SIZE];
    int front, rear;
} Queue;
void initQueue(Queue *q)
{
    q->front = -1;
    q->rear = -1;
}
int isFull(Queue *q)
{
    return (q->rear == MAX_SIZE - 1);
}
int isEmpty(Queue *q)
{
    return (q->front == -1);
}
void enqueue(Queue *q, int item)
{
    if (isFull(q))
    {
        printf("Queue is full\n");
    }
    else
    {
        if (isEmpty(q))
        {
            q->front = 0;
        }
        q->rear++;
        q->items[q->rear] = item;
        printf("Customer with %d minutes of advice added to queue\n", item);
    }
}
int dequeue(Queue *q)
{
    int item;
    if (isEmpty(q))
    {
        printf("Queue is empty\n");
        return -1;
    }
    else
    {
        item = q->items[q->front];
        if (q->front == q->rear)
        {
            q->front = -1;
            q->rear = -1;
        }
        else
        {
            q->front++;
        }
        return item;
    }
}
void serveCustomer(Queue *q, int stallNumber)
{
    int adviceTime = dequeue(q);
    if (adviceTime != -1)
    {
        printf("Serving customer at stall %d for %d minutes of advice\n", stallNumber, adviceTime);
    }
}
int main(void)
{
    Queue stall1, stall2;
    initQueue(&stall1);
    initQueue(&stall2);
    // 模拟顾客选择建议时间并排队
    enqueue(&stall1, 1); // 顾客选择1分钟建议
    enqueue(&stall1, 2); // 顾客选择2分钟建议
    enqueue(&stall2, 3); // 顾客选择3分钟建议
    enqueue(&stall2, 1); // 顾客选择1分钟建议
    enqueue(&stall1, 2); // 顾客选择2分钟建议
    enqueue(&stall1, 3); // 顾客选择3分钟建议
    enqueue(&stall2, 2); // 顾客选择2分钟建议
    enqueue(&stall2, 1); // 顾客选择1分钟建议

    // 服务顾客
    serveCustomer(&stall1, 1);
    serveCustomer(&stall1, 1);
    serveCustomer(&stall2, 2);
    serveCustomer(&stall2, 2);

    return 0;
}

posted on 2024-08-14 23:26  wessf  阅读(2)  评论(0编辑  收藏  举报