c - 队列数据结构实现

标签 c arrays date queue structure

我目前正在尝试用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 循环有关,但不知道如何防止它。

编辑:以上功能现已修复。谢谢。

现在尝试实现一个函数,以从队列中删除用户定义的元素。

最佳答案

代码中存在一些错误:-

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, &id");应该是scanf("%d", &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 && 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.

关于c - 队列数据结构实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353525/

相关文章:

java - 创建不同对象的实例列表

c++ - 字符串文字的模板参数推导

r - 将字符转换为日期

MySqlConversionException问题

objective-c - 如何打包 C 库以便在 Cocoa 项目中使用?

c - 在 C 中传递结构的正确方法

c - 进程的内存地址空间

c - 内存泄漏排序

java - 如何将 byte[] 数组的内容从十六进制转换为十进制?

sql - Date_trunc 按月? PostgreSQL