【问题标题】:Queue data structure implementation队列数据结构实现
【发布时间】: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


【解决方案1】:

代码中有一些错误:-

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
    }
  }

一旦if 被执行。元素不匹配。它会返回。

2.

您也使用num,但在找到元素时不会更改。或者至少你应该打印找到的元素。

3.

也在case 6:

scanf("%d, &amp;id"); 应该是scanf("%d", &amp;id);

4.

同样在position() 函数中,您没有print 正确的消息。

printf("Number %d is in the queue\n", num); 将是

printf("Number %d is in the queue\n", n);

5.

您没有使用position() 的返回值。如果你不需要返回任何东西,它应该有一个返回类型void

代码将如何?

position() 的函数是这样的

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;
        }  
    }
}

逻辑错误

  • 此外,如果输入的数字超过 5 个,它会静默删除元素而不给出任何消息。改变它。

  • 另外,队列实现的逻辑错误的。删除所有 5 个元素后,它仍然显示消息 queue 已满和 queue 为空。

您应用的算法仍然是错误的。检查任何书。


为了帮助您,队列已满条件是

(rear+1)%MAX == front.

队列条件是

front == -1 &amp;&amp; rear == -1。 (这些适用于 OP 的实现,最初是 front= -1rear = -1)。

insert() 的伪代码为:

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.

delete() 的伪代码是:

1. if queue is empty - show message.
2. else if front == rear then rear = front = -1
3. else front = (front + 1)%MAX.

【讨论】:

  • 谢谢。我已经设法让它打印第 n 个元素是否在队列中,但我不确定如何让它打印第 n 个元素在队列中的哪个位置,如果这有意义的话。
  • 谢谢,我得到了它的工作,但遇到了另一个问题。我将编辑我的帖子以显示它是什么。
  • 在我选择显示队列选项时,将前后设置为 0 会导致程序将队列中的第一个元素打印为零。
  • @GeorgeStrawbridge.:我已经帮助你完成了基本的想法和算法。我提到了你的问题。我也会在这里给你一个提示......循环将类似于for(int i = 0; i&lt;(rear+MAX-front)%MAX+ 1; i++) //print queue[(front+i)%MAX] 将打印队列。 ...我猜您可以从这里确定的下一部分...现在尝试一下。它会帮助你。
  • @GeorgeStrawbridge.: 看看这个网站...你就会知道如何提问等。stackoverflow.com/tour
猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多