使用pop函数从优先级队列返回两个值

时间:2014-01-03 11:59:14

标签: c struct priority-queue

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

的Structs:

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 个答案:

答案 0 :(得分:1)

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

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,所以我建议:

答案 1 :(得分:1)

您可以创建struct point

类型的队列数据

的Structs:

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

这样的事情应该可以解决问题。我没有测试它,所以可能会有错误。 祝你好运!

答案 2 :(得分:0)

在C ++中你会使用一个向量或类似的东西来存储一个数组,不幸的是你不能依赖它。

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

q_elem_t *my_array = q_elem_t array[100]; //pseudo code

有关创建结构数组的详细信息,请参阅此处:How do you make an array of structs in C?

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

对我来说,看起来混乱是缺乏数据结构。数组是一个很好的起点,但是如果你想了解更多信息,请查看链接列表等等。