数据结构:用队列模拟理发店的排队情况(C#)

题目内容:使用的排队现象,通过仿真手法评估其营业状况。
*基本要求:设某理发馆有N把理发椅,可同时为N位顾客进行理发。
*当顾客进门时,若有空椅,则可以立即坐下理发,否则需要依次排队等候。
*一旦有顾客理完发离去时,排在队头的顾客便可开始理发。
*若理发馆每天连续营业T小时,求一天内顾客在理发馆内的平均逗留时间
*顾客排队等候的队列平均长度

N和T在运行的时候输入

用C#写的,有注释,很混乱,请高人指教~~

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace 队列应用
  6. {
  7.     class Seat
  8.     {
  9.       
  10.         public  bool IsFree;
  11.         public Customer cus=null;
  12.         public Seat(bool b) 
  13.         {
  14.             IsFree = b;      
  15.         }
  16.     }
  17.     class Customer
  18.     {
  19.         public int cometime;
  20.         public int timetogo;
  21.         public int cost =30- new Random().Next(10);//理发需要20~30分钟
  22.         
  23.         public Customer() { }
  24.     }
  25.     class Program
  26.     {
  27.         static void Main(string[] args)
  28.         {
  29.             System.Console.WriteLine("每天营业多少小时?");
  30.             int workinghours = int.Parse(Console.ReadLine());
  31.             System.Console.WriteLine("有多少个椅子?");
  32.             int seats = int.Parse(Console.ReadLine());
  33.             Process(seats,workinghours);
  34.             Console.ReadLine();
  35.         }
  36.         static void Pr
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
队列是一种数据结构,它遵循先进先出(FIFO)的原则。这意味着新添加到队列中的元素总是被放在队列的末尾,而从队列中移除的元素总是最早进入队列的元素。 在C语言中,可以使用数组和指针来实现队列。以下是一个简单的队列实现示例: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10 typedef struct { int data[MAX_SIZE]; int front; int rear; } Queue; void initQueue(Queue *q) { q->front = -1; q->rear = -1; } int isQueueEmpty(Queue *q) { return (q->front == -1 && q->rear == -1); } int isQueueFull(Queue *q) { return (q->rear == MAX_SIZE - 1); } void enqueue(Queue *q, int value) { if (isQueueFull(q)) { printf("Queue is full\n"); return; } if (q->front == -1) { q->front = 0; } q->rear++; q->data[q->rear] = value; } int dequeue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return -1; } int value = q->data[q->front]; q->front++; return value; } void printQueue(Queue *q) { if (isQueueEmpty(q)) { printf("Queue is empty\n"); return; } printf("Queue elements: "); for (int i = q->front; i < q->rear + 1; i++) { printf("%d ", q->data[i]); } printf("\n"); } ``` 这个队列实现包括以下功能: * `initQueue`:初始化队列,设置队首和队尾为-1表示队列为空。 * `isQueueEmpty`:检查队列是否为空。如果队首和队尾都为-1,则队列为空。 * `isQueueFull`:检查队列是否已满。如果队尾的下一个位置等于数组的最大索引减1,则队列已满。 * `enqueue`:将元素添加到队列的末尾。如果队列已满,则打印错误消息并返回。否则,将新元素添加到队尾,并更新队尾的位置。 * `dequeue`:从队列的开头移除一个元素并返回它。如果队列为空,则打印错误消息并返回-1。否则,返回并删除队首的元素,并更新队首的位置。 * `printQueue`:打印队列中的所有元素。如果队列为空,则打印错误消息并返回。否则,遍历队列并打印每个元素。 这个简单的队列实现可以用于各种应用,例如模拟程序中的任务调度、实现先进先出算法等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值