Gentoo Forums
Gentoo Forums
Quick Search: in
C: Empty a linked list? (solved)
View unanswered posts
View posts from last 24 hours

rackathon
 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Laukaa, Finland

PostPosted: Fri Feb 29, 2008 2:47 pm    Post subject: C: Empty a linked list? (solved) Reply with quote

Hello (again :oops: ),

I have a singly linked list. At the certain point it needs to be emptied because it will be populated with new values. How can I empty it?

I thought about setting the first entry's pointer to the next one to be NULL. But this still leaves the first entry intact. And as far as I understand, it would still leave all the previous entries to the memory. How would I delete them? Memory management isn't my strongest points. ^^

Edit: Thanks for the answers, got it. :)


Last edited by jho on Fri Feb 29, 2008 3:58 pm; edited 1 time in total
Back to top
View user's profile Send private message
barophobia
Apprentice
Apprentice


Joined: 26 Apr 2004
Posts: 219
Location: somewhere

PostPosted: Fri Feb 29, 2008 3:02 pm    Post subject: Reply with quote

1) find the first item call it a
2) find the next after a item call it b
3) delete a
4) set a to be b
5) goto step 2 and repeat until you zap your entire list
_________________
An apple is an apple unless you say it is not an apple!
Back to top
View user's profile Send private message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Laukaa, Finland

PostPosted: Fri Feb 29, 2008 3:04 pm    Post subject: Reply with quote

barophobia wrote:
1) find the first item call it a
2) find the next after a item call it b
3) delete a
4) set a to be b
5) goto step 2 and repeat until you zap your entire list

How do I delete it? AFAIK the delete command is available only for C++.
free() something?
Back to top
View user's profile Send private message
timeBandit
Moderator
Moderator


Joined: 30 Dec 2004
Posts: 2115
Location: here, there or in transit

PostPosted: Fri Feb 29, 2008 3:30 pm    Post subject: Reply with quote

If you allocated the nodes using malloc(), yes, pass the pointer to free().

When you have questions about correct usage of a library function, such as when learning from example code (as I suspect you may be doing in this case), the manual page is always a good place to start. It often includes examples of correct usage and references to complementary functions. For example, man 3 malloc would have directed you to the free() routine. (Section 3 of the UNIX manuals covers development information such as standard C and kernel routines. You usually need not give the section number except in those few instances where a library function has the same name as a command (e.g., printf.)
_________________
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum