View Single Post
Join Date: Aug 2005
Posts: 15,170
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: linked list deleteing problem

 
0
  #2
Nov 20th, 2008
try this
  1. //function to clear all pointers
  2. void clearMemory(bus* &pointer)
  3. {
  4. bus *next;
  5. bandMember *current;
  6.  
  7.  
  8. next = pointer;
  9. while(next != NULL)
  10. {
  11. current = next->memListPtr;
  12. while(current)
  13. {
  14. bandMember *hold = current;
  15. current = current->memberPtr;
  16. delete hold;
  17. }
  18. bus* hold = next;
  19. next = next->busPtr;
  20. delete hold;
  21.  
  22. }
  23. pointer = NULL;
  24. }
  25.  
  26. void Add(bus*& head)
  27. {
  28. bus* node = new bus;
  29. node->busPtr = 0;
  30. node->memListPtr = 0;
  31. if(head == NULL)
  32. head = node;
  33. else
  34. {
  35. bus* next = head;
  36. while(next->busPtr)
  37. next = next->busPtr;
  38. next->busPtr = node;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. bus* head = 0;
  45. for(int i = 0; i < 5; i++)
  46. Add(head);
  47. clearMemory(head);
  48. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote