| View previous topic :: View next topic |
| Author |
Message |
jho Apprentice


Joined: 24 May 2007 Posts: 153 Location: Laukaa, Finland
|
Posted: Fri Feb 29, 2008 2:47 pm Post subject: C: Empty a linked list? (solved) |
|
|
Hello (again ),
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 |
|
 |
barophobia Apprentice


Joined: 26 Apr 2004 Posts: 219 Location: somewhere
|
Posted: Fri Feb 29, 2008 3:02 pm Post subject: |
|
|
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 |
|
 |
jho Apprentice


Joined: 24 May 2007 Posts: 153 Location: Laukaa, Finland
|
Posted: Fri Feb 29, 2008 3:04 pm Post subject: |
|
|
| 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 |
|
 |
timeBandit Moderator


Joined: 30 Dec 2004 Posts: 2115 Location: here, there or in transit
|
Posted: Fri Feb 29, 2008 3:30 pm Post subject: |
|
|
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 |
|
 |
|