【发布时间】:2017-11-17 14:57:34
【问题描述】:
我目前正在尝试用C实现一个队列数据结构,上下文如下:
医生手术需要一个计算机程序来为患者提供自助服务。患者使用此控制台在到达手术室时进行登记。他们还可以使用控制台查询他们在等候名单中的位置,或了解目前有多少医生在手术中。医生也可以使用该程序在完成检查后检查(出院)患者。医生也使用该程序来检查他们的房间。该程序必须维护所有已登记患者的等候名单(队列),并且一旦其中一名医生空闲,该程序必须呼叫下一位患者通过显示消息队列。
我需要实现以下队列函数:
void enqueue (int n) // append n to the queue
int dequeue () // remove the first item in the queue, and return its value
int first() // get the first item without removing it
int last () // get the last item without removing it
void clear () // clear (initialize) the queue
bool IsEmpty () // returns true if the queue is empty
bool IsFull () // returns true if the queue is full
void print () // print the entire queue
int position (int n) // returns the position of n in the queue, or -1 if n
is not in the queue
void remove (int n) // remove n from the queue
位置函数是我有点挣扎的东西:
int position(int n) {
system("clear");
int pos = 1;
for (int i = front; i <= rear; i++) { // a for loop to run for each
// element in the queue
if (queue[i % MAX] == n) // checks to see whether the integer inputted
// by the user is currently in the queue array
printf("Number %d is in the queue\n", num); //
return;
}
}
我想让程序打印出 n 在队列中的位置。我也不确定如何实现从队列中删除特定元素的功能。另外我还没有尝试删除功能,所以当我尽我所能实现它时会再次发布。
感谢 coderredoc,我得到了打印队列中位置的代码!但是因为我希望它显示数字 1,而不是 0,如果队列中的第一个,我使用了以下内容:
void position(int n) {
system("clear");
int pos = 1;
for (int i = front; i <= rear; i++) {
if (queue[i % MAX] == n)
printf("You are in the queue!\n");
printf("Position in queue is %d \n",(i % MAX) +1);
}
}
但它会循环并多次打印出队列中的增量位置。我知道这与我的 for 循环有关,但不知道如何防止它。
编辑:上述功能现已修复。谢谢。
现在尝试实现一个函数,从队列中删除用户定义的元素。
【问题讨论】:
-
你有什么问题?什么不工作?您能否以MCVE 的形式将问题归结为仅与相关功能有关?
-
请提供Minimal, Complete, and Verifiable example 删除任何与您的实际问题无关的内容。
-
OP 已编辑以查明我最需要帮助的地方
标签: c arrays date queue structure