구현중인 우선 순위 큐에 대한 구조체 포인터의 배열을 해제하는 데 문제가 있습니다. 클라이언트 C 프로그램에서 고정 크기로 노드 포인터의 두 동적 배열을 만듭니다. 배열 heapMap에는 특정 ID 정수 값 및 배열 으로 구성된 각 노드에 매핑되는 노드 포인터가 포함됩니다. 힙은 현재 값과 관련하여 노드가 포함 된 힙 배열입니다.우선 순위 큐의 구현에서 struct 포인터의 배열을 할당 해제합니다.
그러나 모든 것이 작동하는 것처럼 보일 수는 있지만 pq_free 함수는 오류를 일으키거나 배열의 할당을 제대로 할당하지 않습니다. 어떤 도움이
구조
typedef struct node_struct{
int ID;
double val;
}NODE;
struct pq_struct {
char heapType;
int max;
int inUse;
NODE ** heap; //BOTH have a specific capacity
NODE **heapMap; //array of pointers to each
};
을 감상 할 수있다 이것은 내가 구조에 대한 메모리를 할당하는 데 사용하는 기능입니다.
PQ * pq_create(int capacity, int min_heap){
PQ * newQueue = (PQ*) malloc(sizeof(PQ)); //Allocate memory for a new heap
newQueue->max = capacity;
newQueue->inUse = 0;
int inUse = 1;//1 in use by default, the 0th point in the array is left alone intentionally
//If min_heap == 0, it it is a max heap, any other value is a min heap.
if(min_heap != 0){
newQueue->heapType = 'm';
}else{
newQueue->heapType = 'M';
}
//Allocate memory for heapMap and heap..
newQueue->heap = (NODE**) malloc(sizeof(NODE*)*capacity); //array of nodes, the heap
newQueue->heapMap = (NODE**) malloc(sizeof(NODE*) * capacity);//array of node pointers, the HEAPMAP
int i = 0;
for (i = 0; i < capacity + 1;i++) {
newQueue->heapMap[i] = NULL;
}
//return PQ pointer
return newQueue;
}
이것은 제대로 작동하지 않는 pq_free 기능입니다. 도움에 미리 감사드립니다.
void pq_free(PQ * pq){
//free all nodes
NODE * temp;
NODE ** temp2;
int i;
for (i = 0; i < pq->inUse; i++) {
if (pq->heapMap[i] != NULL) {
temp = pq->heapMap[i];
free(temp);
}
}
//pq->heapMap = NULL;
free(pq->heap);
free(pq->heapMap);
free(pq);
}