943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 526
  • C++ RSS
Nov 20th, 2008
0

linked list deleteing problem

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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:



C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Nov 20th, 2008
0

Re: linked list deleteing problem

try this
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 20th, 2008
0

Re: linked list deleteing problem

that did it, thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyosuke0 is offline Offline
20 posts
since Sep 2008
Nov 21st, 2008
0

Re: linked list deleteing problem

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 9
Solved Threads: 1
Newbie Poster
rajenpandit is offline Offline
13 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with c++ coursework - code so far included.
Next Thread in C++ Forum Timeline: Help... Link list problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC