'Freeing the Memory of a list that contain lists
I have an assignment where i have to create a radix sort method using linked lists. First i create a sequence/list of random numbers then perform radix sort on the list using a list of 10 buckets. I have managed to create the sequence and buckets for the radix sort method but my problem is freeing/clearing the memory of all the buckets and sequence when im done. Heres my attempt to clear all the memory for the buckets. Any idea on how to stop the errors warnings i get at the bottom and free a list of lists properly?
Example. Let’s sort the sequence S0 = {32, 100, 11, 554, 626, 122, 87, 963, 265, 108, 9}.
We start by distributing elements of S0 by the value of 0-place digit (the one's place):
bucket 0: 100
bucket 1: 11
bucket 2: 32, 122
bucket 3: 963
bucket 4: 554
bucket 5: 265
bucket 6: 626
bucket 7: 87
bucket 8: 108
bucket 9: 9
ListNode *buckets = (ListNode*)malloc(sizeof(ListNode)*10);
void clearBuckets(ListNode *buckets)
{
int i = 0;
for (i =0; i< 10; i++)
{
if(&buckets[i] !=NULL){
if(buckets[i].next!=NULL);
deleteList(&buckets[i]);
}
}
}
}
void radixSort(ListNode *head, ListNode *buckets, int numValues,int place){
ListNode* buck[10]; /*points to location in radArr*/
//head = head->next;
int index;
ListNode*current =head->next;
for (int i = 0; i < 10; i++)
{
buck[i] = &buckets[i];
}
for(int j = 0; j<numValues;j++)
{
int num = getPlace(place,current->data);
buck[num] = insertNode(buck[num], current->data);
// printf("place:%d\n",num);
if(j==numValues -1){
break;
}
else{
current = current ->next;
}
}
}
void freeList(ListNode **head){
ListNode* current = *head;
ListNode* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
*head = NULL;
}
**I get these warnings when i try to run**
***warning: passing argument 1 of ‘freeList’ from incompatible pointer type [-
Wincompatible-pointer-types]
freeList(&buckets[i]);
^~~~~~~~~~~***
I also get an error when i try to free my sequence which im assuming is because my
**freeList** method takes in a ** whereas my sequence is * (just a guess as to why im
not able to free a regular single list, im assuming my **freeList** method is out of
wack as well...)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
