• Advertisement
Sign in to follow this  

freeing linked list data

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm learning about making linked lists, and was wondering what the proper way to release a node is. i know how to exclude nodes from the loop, but how should i free the data it previously took up? thanks in advance. //pnode is the previous node, cnode is the current node int transverse(int del) { if (header->next==footer) return 0; NODE *cnode=header->next; NODE *pnode=header; do { if (cnode->data==del) pnode->next=cnode->next; pnode=cnode; cnode=cnode->next; }while (cnode!=footer); return 1; }

Share this post


Link to post
Share on other sites
Advertisement
How about this?



Linkedlist * point
Linkedlist * Cpoint

//Set point to the beginning of your list
do {
Cpoint = point->next;
Free Point;
} while (Cpoint);



This'll traverse your linked list until point->next = null, at which point the while will stop.

From,
Nice Coder

Share this post


Link to post
Share on other sites
Myself to delete a node would do the following



void delete_node(int del, node *head)
{
node *current = head;
node *previous = head;

if(head == NULL)
return;

do {
if(current->data == del) {
previous->next = current->next;
delete current;
return; /*Only if you want to break after the deleting the first
node with the data*/
} else {
previous = current;
current = current->next;
}
} while(current != NULL);
}



I hope thats what your looking for

Later days

EDIT: I just realised that my code had a couple of simple mistakes, thought i'd better fix them up

Share this post


Link to post
Share on other sites
Interesting approaches... I prefer recursive functions to clean up linked lists.


//will wipe out all nodes after the given one, and stored data if necessary
void freeNode(NODE* node){
if (!node) return;//don't bother if it's null
//free node data here (if needed)
freeNode(node->next);//doesn't matter if it's null
free(node->next);//free the next node
}

//to wipe out the nodes:
freeNode(header);
free(header);//might not want to do this if you plan on keeping it



This'll work if you're using a doubly-linked list too, but you'll have to set the next pointer to null if you don't plan on wiping them all out.

Share this post


Link to post
Share on other sites
It's a lot faster to do it iteratively than recursively unless you've got a smart compiler. Secondly it's a lot simpler and more understandable.

To free a single node, try something like:


void delete_node( node_t* previous, node_t* node, function_t free_node )
{
previous->next = node->next;
free_node( node );
}




To free the whole list, try something like:


void delete_list( list_t* list, function_t free_node )
{
node_t* node = list->head;

while ( node )
{
node_t next = node->next;
free_node( node );
node = next;
}

free( list );
}


Share this post


Link to post
Share on other sites
Quote:
Original post by eal
i'm learning about making linked lists, and was wondering what the proper way to release a node is. i know how to exclude nodes from the loop, but how should i free the data it previously took up?


I think the appropriate answer to this question is: it depends on how you allocated the node.

If you allocated the memory using malloc(), you should release it using free().

If you allocated it with the new operator, you should release it with delete operator.

If you're not sure, post the code you use to create a new node and someone can point out how to destroy the node properly.


As for the slightly seperate issue of how to modify your code to release the memory, this should work:


int transverse(int del)
{
if (header->next==footer)
return 0;

NODE *cnode=header->next;
NODE *pnode=header;

do
{
if (cnode->data==del)
{
pnode->next=cnode->next;
free(cnode); // this is assuming you allocated with malloc()
}
else
{
pnode=cnode;
}
cnode = pnode->next;
}while (cnode!=footer);

return 1;
}


(By the way, you might want to change that function name to 'traverse' instead of 'transverse')

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement