linked list deleteing problem

Thread Solved
Reply

Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

linked list deleteing problem

 
0
  #1
Nov 20th, 2008
whenever the clearMemory function is called i get a segmentation fault and i can not figure out whats wrong. The function is supposed to delete all pointers. The function brings in the pointer to the linked list by reference. The nodes in the list are structs i named bus that contain another liked list i called bandmembers.

  1. //band member struct to hold band member info
  2. struct bandMember
  3. {
  4. //first name holder
  5. string firstName;
  6.  
  7. //last name holder
  8. string lastName;
  9.  
  10. //instrument holder
  11. string instrument;
  12.  
  13. //pointer to another bandmember
  14. bandMember *memberPtr;
  15. };
  16.  
  17. //struct to hold bus info
  18. struct bus
  19. {
  20. //bus designation
  21. int busNum;
  22.  
  23. //bus capasity, same for all buses
  24. int busCap;
  25.  
  26. //list of bandmembers on each bus
  27. bandMember *memListPtr;
  28.  
  29. //pointer to another bus struct
  30. bus *busPtr;
  31. };


the function prototype and definition are:



  1. //function prototype
  2. void clearMemory(bus* &ptr);
  3.  
  4.  
  5. //function to clear all pointers
  6. void clearMemory(bus* &pointer)
  7. {
  8. bus *last, *next;
  9. bandMember *current, *previous;
  10.  
  11.  
  12. last = pointer;
  13. next = last;
  14. current = last->memListPtr;
  15. previous = current;
  16.  
  17. while(last != NULL)
  18. {
  19. while(current != NULL)
  20. {
  21. previous = current;
  22. current = current->memberPtr;
  23. delete previous;
  24.  
  25. }
  26. next = last;
  27. last = last->busPtr;
  28. delete next;
  29. current = last->memListPtr;
  30. previous = current;
  31.  
  32. }
  33.  
  34. pointer = NULL;
  35. }


thank you in advance for any help
Last edited by kyosuke0; Nov 20th, 2008 at 6:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,141
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: 1434
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 Quick reply to this message  
Join Date: Sep 2008
Posts: 20
Reputation: kyosuke0 is an unknown quantity at this point 
Solved Threads: 0
kyosuke0 kyosuke0 is offline Offline
Newbie Poster

Re: linked list deleteing problem

 
0
  #3
Nov 20th, 2008
that did it, thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 13
Reputation: rajenpandit is an unknown quantity at this point 
Solved Threads: 1
rajenpandit rajenpandit is offline Offline
Newbie Poster

Re: linked list deleteing problem

 
0
  #4
Nov 21st, 2008
  1. struct node
  2. {
  3. int data;
  4. struct node *link;
  5. };
  6.  
  7.  
  8. void delet(struct node **currNode,int location)
  9. {
  10. int i;
  11. struct node *temp,*temp1,*temp2;
  12. int cnt;
  13. temp= *currNode;
  14. cnt=count(currNode);
  15. if(location>cnt)
  16. {
  17. printf("\nIndex should be <= %d",cnt);
  18. }
  19. for(i=1;i<=cnt;i++)
  20. {
  21. if(location==1&&i==1)
  22. {
  23. *currNode=temp->link;
  24. free(temp);
  25. }
  26. if(i==location-1)
  27. {
  28. temp1=temp->link;
  29. temp->link=temp->link->link;
  30. free(temp1);
  31. }
  32. temp=temp->link;
  33. }
  34. }
  35. int count(struct node **currNode)
  36. {
  37. int count;
  38. struct node *temp;
  39. temp= *currNode;
  40. if(*currNode==NULL)
  41. {
  42. count=0;
  43. }
  44. else
  45. count=1;
  46. while(temp->link!=NULL)
  47. {
  48. temp=temp->link;
  49. count++;
  50. }
  51. return count;
  52. }
  53.  
  54.  
  55. void main()
  56. {
  57. int value,index;
  58. struct node *currNode;
  59. currNode=NULL;
  60. printf("Enter Index : ");
  61. scanf("%d",&value);
  62. //delete node in the given index of a link list
  63. delet(&currNode,value);
  64. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC