freeing linked list data
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;
}
How about this?
This'll traverse your linked list until point->next = null, at which point the while will stop.
From,
Nice Coder
Linkedlist * pointLinkedlist * Cpoint//Set point to the beginning of your listdo {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
Myself to delete a node would do the following
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
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
Interesting approaches... I prefer recursive functions to clean up linked lists.
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.
//will wipe out all nodes after the given one, and stored data if necessaryvoid 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.
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:
To free the whole list, try something like:
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 );}
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')
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement