提问者:小点点

从优先级队列返回两个带有pop函数的值


我有优先队列,它只返回int y的pop函数,但我需要返回int x和int y。所以我发现,我可以使用struct(struct point)从函数中返回两个值,但我不知道它是如何实现的(重写int以结构并在main中使用它)。

结构:

typedef struct { int x; int y; int pri; } q_elem_t;
typedef struct { q_elem_t *buf; int n, alloc; } pri_queue_t, *pri_queue;
struct point{int PointX; int PointY;}; 

弹出功能:

int priq_pop(pri_queue q, int *pri)
{
  int out;
  if (q->n == 1) return 0;

  q_elem_t *b = q->buf;

  out = b[1].y;
  if (pri) *pri = b[1].pri;

  /* pull last item to top, then down heap. */
  --q->n;

  int n = 1, m;
  while ((m = n * 2) < q->n) {
    if (m + 1 < q->n && b[m].pri > b[m + 1].pri) m++;

    if (b[q->n].pri <= b[m].pri) break;
    b[n] = b[m];
    n = m;
  }

  b[n] = b[q->n];
  if (q->n < q->alloc / 2 && q->n >= 16)
    q->buf = realloc(q->buf, (q->alloc /= 2) * sizeof(b[0]));

  return out;
}

在main()中使用:

  /* pop them and print one by one */
  int c; 
  while ((c = priq_pop(q, &p)))
  printf("%d: %d\n", p, c);

我从C开始,所以我将非常感谢任何帮助。


共3个答案

匿名用户

您可以这样声明您的结构:

typedef struct queue_element_struct { // It's good practice to name your structs
   int x,y;
   int pri;
} queue_element_t;

typedef struct priority_queue_struct { 
   queue_element_t *buf; 
   int n, alloc; 
} pri_queue_t, *pri_queue; // Don't know what `*pri_queue` is for

然后更改您的函数以返回指向queue_element_t结构的指针

queue_element_t * priq_pop(pri_queue q, int *pri)

改变

int out;
if (q->n == 1) return 0;
q_elem_t *b = q->buf;

out = b[1].y;

// Create new pointer to queue_element_t structure
// that will be returned by this function
queue_element_t *out;
out = (queue_element_t *) malloc(sizeof(queue_element_t));
if (! out) {
  // Could not allocate
}
if (q->n == 1) return 0;

// Set data from queue
out->x = q->buf[1].x;
out->y = q->buf[1].y;

我不知道你的函数到底做了什么,但这就是你在C中返回结构的方式。

你说你刚刚从C开始,所以我建议:

  • Steve McConnell的《代码完成》一书。注释你的代码非常有用(无论多小)
  • 正确命名变量:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Variable_Names
  • 了解指针。你能读到的关于它们的一切,都读吧。

匿名用户

您可以使您的队列数据类型为struct point

结构:

typedef struct point{int PointX; int PointY;} q_data; 
typedef struct { q_data d; int pri; } q_elem_t;
typedef struct { q_elem_t *buf; int n, alloc; } pri_queue_t, *pri_queue;

弹出功能:

q_data priq_pop(pri_queue q, int *pri)
{
  q_data out = {0,0};
  if (q->n == 1) return out;

  q_elem_t *b = q->buf;

  out = b[1].d;
  if (pri) *pri = b[1].pri;

  /* pull last item to top, then down heap. */
  --q->n;

  int n = 1, m;
  while ((m = n * 2) < q->n) {
    if (m + 1 < q->n && b[m].pri > b[m + 1].pri) m++;

    if (b[q->n].pri <= b[m].pri) break;
    b[n] = b[m];
    n = m;
  }

  b[n] = b[q->n];
  if (q->n < q->alloc / 2 && q->n >= 16)
    q->buf = realloc(q->buf, (q->alloc /= 2) * sizeof(b[0]));

  return out;
}

在main()中使用:

  /* pop them and print one by one */
  q_data c; 
  while ((c = priq_pop(q, &p)))
  printf("%d: %d, %d\n", p, c.PointX, x.PointY);

像这样的东西应该可以做到这一点。不过我没有测试它,所以可能会有错误。祝你好运!

匿名用户

在C中,您将使用向量或类似的东西来存储不幸的数组,您不能依靠它。

为什么不使用数组,你可以让你的队列是一个q_elem_t数组?

q_elem_t*my_array=q_elem_t数组[100];//伪码

有关制作结构数组的更多信息,请参阅此处:如何在C中制作结构数组?

数组的唯一特点是您需要malloc任意大小(即数组[100]),或者您需要动态控制数组的内存。如果您刚开始,最好声明一个大小为100的数组。

对我来说,困惑似乎在于缺乏数据结构。数组是一个很好的起点,但是如果你想了解更多,请查看链表之类的东西。