【问题标题】:How to implement this create function correctly?如何正确实现这个创建功能?
【发布时间】:2020-04-08 21:03:07
【问题描述】:

我正在尝试创建一个函数来创建优先级队列。我的结构是这样的:

struct node {
  char *item;
  struct node *next;
};

struct queue {
  struct node *start;
  struct node *end;
};

struct priority_queue {
  struct queue **aoq;
  int x;
};

我要实现的功能是:

struct priority_queue *priority_queue_create(int x);

这里,从结构p​​riority_queue中可以看出,struct queue **aoq本质上是一个队列数组,这是我想要的。 int x,在函数头中,是数组中的队列数。

我的看法是这样的:

struct priority_queue *priority_queue_create(int x) {
  struct priority_queue *pq = malloc(sizeof(struct priority_queue));
  pq->x = x;
  pq->aoq = malloc(x* sizeof(struct queue));
  return pq;
}

我在上面的代码中放置 3 行注释的地方是我怀疑我的错误所在的地方。我希望能够执行以下操作:如果 x = 3,则应该有一个包含 3 个队列的数组,并且我想我可以通过执行类似

的操作来访问它
pq->aoq[0] /// to access the first queue in the array, or
pq->aoq[2] /// to access the third queue in the array

谁能帮助我修复我的实现?提前致谢。


编辑: 我尝试过的其他实现:

struct priority_queue *priority_queue_create(int x) {
  struct priority_queue *pq = malloc(sizeof(struct priority_queue *));
  pq->x = x;
  pq->aoq = malloc(x * sizeof(struct queue *));
  for (int i = 0; i < x; ++i) {
    pq->aoq[i] = malloc(sizeof(struct queue));
  }
  return pq;
}

【问题讨论】:

  • 对不起,他们是错别字。我在上面编辑过。
  • malloc(x * sizeof(struct queue)) 应该是malloc(x * sizeof(struct queue *))
  • 那么你需要为每个struct queue节点分配内存并分配给pq-&gt;aoq[i]
  • 我刚试过。它不适合我。
  • “它不工作”并没有告诉我们问题是什么。更新问题中的代码并清楚地描述“不工作”的含义。

标签: c arrays struct queue


【解决方案1】:

根据您发布的代码,我认为这是您想要的功能。您错过了struct priority_queue 内的队列的内存分配,即:

pq-> aoq = malloc( x * sizeof(struct queue *));

添加这个并保留您已有的(使用示例主函数来测试函数priority_queue_create()),我们有以下代码:

#include <stdio.h>
#include <stdlib.h>

struct node {
    char *item;
    struct node *next;
};

struct queue {
    struct node *start;
    struct node *end;
};

struct priority_queue {
    struct queue **aoq;
    int x;
};


struct priority_queue *priority_queue_create(int x) {
    struct priority_queue *pq = malloc(sizeof(struct priority_queue *));
    pq->x = x;

    pq-> aoq = malloc( x * sizeof(struct queue *));

    for (int i = 0; i < x; ++i) {
       pq->aoq[i] = malloc(sizeof(struct queue)); ///
    }

    return pq;
}

int main(){

    struct priority_queue * pq = priority_queue_create(3);
    char * str1 = "abcd";
    char * str2 = "abcde";

    pq->aoq[0]->start = malloc(sizeof(struct node));
    pq->aoq[2]->start = malloc(sizeof(struct node)); 
    pq->aoq[0]->start->item = str1;
    pq->aoq[2]->start->item = str2;

    printf("%s - %s\n", pq->aoq[0]->start->item,  pq->aoq[2]->start->item);

    free(pq->aoq[0]->start);
    free(pq->aoq[2]->start);
    free(pq->aoq[0]);
    free(pq->aoq[1]);
    free(pq->aoq[2]);
    free(pq->aoq);
    free(pq);

}

【讨论】:

  • 我尝试了您的建议,但由于某种原因我收到了堆缓冲区溢出错误。我在上面的问题中更改了我的代码,以便您可以输入我输入的内容。
  • 级别的大小是多少,为什么不使用 x 来定义优先级队列的元素数?顺便说一句:函数中未初始化级别,因此您可能会在堆中分配大量内存。
  • 抱歉,刚才写错了。我用 x 替换了级别,但出现了同样的问题。
  • 您使用的是多大的 x 以及代码在哪里中断?
  • x 应该是任意数字,并且代码在代码中有 3 行注释的地方中断。
猜你喜欢
  • 2022-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2021-11-23
  • 1970-01-01
  • 2011-03-02
  • 2016-12-07
  • 2022-12-09
相关资源
最近更新 更多