【问题标题】:Passing a heap as an argument将堆作为参数传递
【发布时间】:2020-09-18 18:24:23
【问题描述】:

我尝试创建几个函数,通过下面的代码将堆作为参数传递。然而,结果并没有像我预期的那样。

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}

void pushValue (int *heap, int count, int value)
{
    count++;
    heap[count] = value;
    upHeap_min2(heap, count);   
}

void view(int *heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i < count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int count = -1;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

获取父索引的函数:

int getParentIdx (int index)
{
    return (index-1)/2;
}

上面的代码应该已经打印出来了

10 20 40 90 30

但它什么也没打印。我也想过将它作为双指针传递,但我没有工作。这是否意味着我不能将堆作为参数传递(这意味着我必须将堆声明为全局变量)还是有其他方法可以做到这一点?

【问题讨论】:

  • 我很好奇你为什么不将count 作为指针传递给pushValue(heapDemo, count, 30);。我错过了什么吗?你不需要通过指针传递count,比如pushValue(heapDemo, &amp;count, 30);

标签: c arrays heap


【解决方案1】:

您的pushValue 函数采用count 参数按值(这意味着该函数接收数据的副本),因此它永远不会在main 函数。相反,您应该将 count 作为指针传递,并且(因此)需要在函数内取消引用它:

void pushValue(int* heap, int* count, int value)
{
    ++(*count);
    heap[*count] = value;
    upHeap_min2(heap, *count);
}

那么,在main,你应该使用count地址来调用它:

    pushValue(heapDemo, &count, 30); // And similarly for the other calls

另外,view 函数中的循环在距离结尾处停止。将循环限制更改为i &lt;= count。 (这个函数使用count但没有修改它,所以按值传递是可以的。):

void view(int* heap, int count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= count; i++) {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

请随时要求进一步澄清和/或解释。

【讨论】:

  • 请问...你为什么用++(*count)而不是(*count)++
  • @Brian989 在这种情况下,结果没有区别。我只是在乱写代码,做出了改变(我的风格比其他任何东西都多),却忽略了把它改回来。不过,我认为预增量更好的一个原因是,如果你“忘记”括号:虽然++*count 可以工作,但*count++ 不会!
  • 感谢@Adrian 的帮助。我很感激。
【解决方案2】:

这是一个有效的解决方案,让我解释一下

#include<stdio.h>

void upHeap_min2 (int *heap, int index)
{
    if (index == 0)
        return;

    int parentIdx = getParentIdx(index);
    if (heap[index] < heap[parentIdx])
    {
        int temp = heap[index];
        heap[index] = heap[parentIdx];
        heap[parentIdx] = temp; 

        upHeap_min2(heap, parentIdx);
    }
}
int getParentIdx (int index)
{
    return (index-1)/2;
}

void pushValue (int *heap, int *count, int value)
{
    *count = *count + 1;
    heap[*count] = value;
    upHeap_min2(heap, *count);   
}

void view(int *heap, int *count)
{
    printf("Values inside heap: ");
    for (int i = 0; i <= *count; i++)
    {
        printf("%d ", heap[i]);
    }
    printf("\n");
}

int main()
{
    int heapDemo[101];
    int conter = -1; //new var
    int *count = &conter;
    pushValue(heapDemo, count, 30);
    pushValue(heapDemo, count, 20);
    pushValue(heapDemo, count, 40);
    pushValue(heapDemo, count, 90);
    pushValue(heapDemo, count, 10);

    view(heapDemo, count);

    return 0;
}

主要错误是您将计数器作为简单变量传递,您应该将其作为指针传递,这导致 counter 永远不会递增,所以我创建了一个指向该变量的指针,现在 counter 递增它会打印您所期望的。此外,我还修改了用于打印值的主体

【讨论】:

  • 这并不能解释为什么传递 count 而不是 &amp;count 不起作用。这对您来说可能很明显,但询问此问题的人没有这种知识,因此应该提供答案。
  • 好的。事实是,如果您传递 count,您将变量作为值传递,因此它将变量的副本传递给方法,而原始变量 count 将永远不会增加。如果你传递一个引用&amp;count,现在你正在增加count 变量。在这里您可以找到更多信息:stackoverflow.com/questions/373419/…
  • 谢谢@Josep!我真的以为我不正确地传递了数组......它就像你说的那样工作。
猜你喜欢
  • 2020-06-22
  • 2014-11-14
  • 2019-03-26
  • 2015-05-24
  • 2012-11-10
  • 2020-10-30
  • 2014-02-25
  • 1970-01-01
相关资源
最近更新 更多