C 队列数据结构的实现
我目前正在尝试用C实现队列数据结构。上下文如下: 医生需要一个计算机程序为病人提供自助服务。患者使用此控制台检查手术的到达情况。他们还可以使用控制台查询他们在等待名单中的位置,或者找出目前有多少医生在手术中。医生还可以使用该程序在患者完成脱胺后检查(出院)患者。医生也使用该程序来登记入住和退房。该程序必须维护所有登记入住患者的等待名单(队列),一旦其中一名医生有空,该程序必须通过显示消息来呼叫队列中的下一位患者 我需要实现以下队列功能:C 队列数据结构的实现,c,arrays,date,queue,structure,C,Arrays,Date,Queue,Structure,我目前正在尝试用C实现队列数据结构。上下文如下: 医生需要一个计算机程序为病人提供自助服务。患者使用此控制台检查手术的到达情况。他们还可以使用控制台查询他们在等待名单中的位置,或者找出目前有多少医生在手术中。医生还可以使用该程序在患者完成脱胺后检查(出院)患者。医生也使用该程序来登记入住和退房。该程序必须维护所有登记入住患者的等待名单(队列),一旦其中一名医生有空,该程序必须通过显示消息来呼叫队列中的下一位患者 我需要实现以下队列功能: void enqueue (int n) // appen
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;
}
}
int位置(int n){
系统(“清除”);
int pos=1;
对于(int i=front;i代码中有一些错误:-
1.
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 0; <----------PROBLEM
}
}
逻辑错误
- 此外,如果输入的数字超过5个,它会无声地删除元素,而不会给出任何消息。请更改此设置
- 此外,队列实现的逻辑错误。删除所有5个元素后,仍会显示消息
队列
已满且队列
为空
你应用的算法仍然是错误的。请检查任何一本书
为了帮助您,队列已满条件为
(后部+1)%MAX==前部
队列为空的条件为
front==-1&&rear=-1
(这适用于OP的实现,其中front=-1
和rear=-1
最初)
insert()
的伪代码为:
delete()
的伪代码为:
你有什么问题?什么不起作用?你能把问题归结为只关于相关功能的问题吗?以a的形式?请提供一个删除与你的实际问题无关的任何内容。编辑OP以确定我最需要帮助的地方谢谢。我已设法让它打印出第n个元素是否ent在队列中,但我不确定如何让它打印第n个元素在队列中的哪个位置,如果这有意义的话。谢谢,我让它工作了,但遇到了另一个问题。我将编辑我的帖子以显示它是什么。将“前”和“后”设置为0会导致程序在我选择“显示”时将队列中的第一个元素打印为零ay队列选项。@GeorgeStrawbridge.:我已经帮助您了解了基本思想和算法。我向您提到了问题。我在这里也会给您一个提示……循环类似于for(int I=0;i@GeorgeStrawbridge.:看看这个网站……你就会知道如何提问等。
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 0; <----------PROBLEM
}
}
void position(int n) {
system("clear");
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)-front);
return;
}
}
}
1. if queue is full - show message.
2. if queue is empty set front and rear to 0. [Denoting there is some element in queue]
3. else rear = rear % MAX
4. put the element in the array at position rear.
1. if queue is empty - show message.
2. else if front == rear then rear = front = -1
3. else front = (front + 1)%MAX.