LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2005, 04:23 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Linked List - Free Nodes?


Hello,

Could someone please post some code for freeing all the nodes of a linked list in the most efficient manner.

The nodes pointer to next have to be referenced to null and then freed. I keep getting errors when I try to implement it.

I know its simple. Could anyone please post the code please?

Thanks,
Mistro116

Edit:

The code I have so far is:

Code:
void DeleteList (NODEPTR headPtr)
{
   NODEPTR curr, prev;
   
   if (IsEmpty (headPtr))
   {
      printf ("\nThe list is empty - All nodes have been ");
      printf ("deleted and freed.\n\n");
   }
   else
   {
      prev = NULL;
      
      curr = headPtr;
      
      while (prev != NULL)
      {
	 /*  Traverse the list.  */
	 prev = curr;
	 curr = curr -> next;
	 
	 prev -> next = NULL;
	 free (prev);
      }
      
      /*  Insert the node, temp, at the end of the list.  */
      headPtr = NULL;
   }
}

Last edited by Mistro116@yahoo.com; 12-04-2005 at 04:33 PM.
 
Old 12-04-2005, 04:46 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Two thing that I spotted:

The first is your while condition should be on curr not prev. prev is initiated to NULL and so you'd never enter the loop anyway.

The second is the you don't need to set prev->next to NULL since you are about to free the memory anyway.

hope that helps,


graeme.
 
Old 12-04-2005, 05:02 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Are you sure we don't need to set prev's --> next equal to NULL? I was told to do this as a safety precaution. It might avoid segementation faults and memory leaks????

And yes I caught that error with the loop (prev should be curr) right after I posted this, and the code worked.

But yes so far so good, thanks.

Mistro116
 
Old 12-04-2005, 05:09 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Well I can't think why you would need it, because the very next statement is free'ing the memory and so you should not try and access anything that prev points to. However I'm open to persuasion


graeme.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
C program problem on delete duplicated nodes in linked list ! antony_csf Programming 2 10-28-2004 10:42 AM
how to free linked list containers? rgiggs Programming 3 07-30-2004 01:24 PM
linked lists w/ nodes? |2ainman Programming 1 05-19-2004 01:32 AM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration