呦.我有这个非常简单的交换功能似乎不起作用.可能是一个指针问题所以任何建议都会很好.
void swap(pQueue *h, int index1, int index2) {
student *temp = &h->heaparray[index1];
h->heaparray[index1] = h->heaparray[index2];
h->heaparray[index2] = *temp;
}
Run Code Online (Sandbox Code Playgroud)
pQueue是一个堆指针,index1并index2保证是有效的索引.
student *temp确实得到了值,heaparray[index1]但是当heaparray[index2]赋值时,它heaparray[index2]保持不变.任何建议表示赞赏
您需要复制h->heaparray[index1](而不是其地址)的实际值,temp然后将该值复制到其中h->heaparray[index2],如下所示:
void swap(pQueue *h, int index1, int index2) {
student temp = h->heaparray[index1];
h->heaparray[index1] = h->heaparray[index2];
h->heaparray[index2] = temp;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
300 次 |
| 最近记录: |