2017-05-06 56 views
-2

我想釋放一個Adjency Linked List內存 這裏是我的數據結構和兩個函數,它們可以爲圖分配內存。 如何釋放分配的內存? 感謝您的幫助如何免費爲相關鏈接列表分配內存?

struct ListPoint { 
    int dest; 
    int weight; 
    struct ListPoint* next; 
}; 
struct List { 
    struct ListPoint* head; 
}; 
struct Graf { 
    int V; 
    struct List* array; 
}; 
struct ListPoint* newAdjencyListPoint(int dest, int weight) 
{ 
    struct ListPoint* newPoint = 
     (struct ListPoint*)malloc(sizeof(struct ListPoint)); 
    newPoint->dest = dest; 
    newPoint->weight = weight; 
    newPoint->next = NULL; 
    return newPoint; 
} 
struct Graf* createGraph(int V) 
{ 
    struct Graf* graf = (struct Graf*)malloc(sizeof(struct Graf)); 
    graf->V = V; 
    graf->array= (struct List*)malloc(V * sizeof(struct List)); 
    int i; 
    for (i = 0; i < V; ++i) 
     graf->array[i].head = NULL; 
    return graf; 
} 

回答

0

下面的代碼將problaby被你在找什麼:

freeLinkedList(struct List list){ 
    struct ListPoint *aux,*it = list.head; 
    while(it != NULL){ //free a node and go to the next one 
     aux = it->next; 
     free(it); 
     it = aux; 
    } 
} 
freeAdjList(struct Graf* adj_list){ 
    for(int i=0;i<adj_list->V;i++) //free each linked list 
     freeLinkedList(adj_list->array[i]); 
    free(adj_list->array); //free the linked list array 
    free(adj_list); //free the adj matrix itself 
} 
+0

不幸的是我得到了來自微軟的Visual Studio 2013 錯誤1個錯誤C2440以下錯誤:「功能」 :無法從'列表'轉換爲'列表*' 問題出在「for」循環:freeLinkedList(adj_list-> array [i]); adj_list有一個紅色下劃線 –

+0

錯誤:類型「struct List」的參數與類型爲「struct List *」的參數不兼容 –

+0

現在它應該工作 –