Linked List - Need help deleting!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List - Need help deleting!

 
0
  #21
Nov 21st, 2008
Now, i am able to get my 3rd song in the list to be deleted. I'm still having problems getting the song that I want the user to delete though. What do I need to change to make it work? Please help!


Here is my deleteSong function:


  1. void deleteSong(mp3Type*& first)
  2. {
  3.  
  4. mp3Type *last,*current,*trailcurrent,*previous;
  5. int id = 0;
  6. bool found = false;
  7.  
  8. current = first;
  9.  
  10. cout<<"Enter song ID that you like to delete"<<endl;
  11. cout<<endl;
  12. cin>>id;
  13.  
  14.  
  15. while(current == NULL && current->id != id)
  16. {
  17. previous = current;
  18. current = current->link;
  19. }
  20. if (current == NULL)
  21. {
  22. found = false;
  23. first = current->link;
  24. }
  25. else
  26. {
  27. found = true;
  28. current = current->id;
  29. delete current;
  30. }
  31. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #22
Nov 21st, 2008
>>Should I use bool found?
found is not needed at all in this function. You might want to use something like it as a return value indicating to the calling function that id was found, but in the mechinics of the deletion process it is not needed.

line 23 post # 21 is irrelevant and should be removed.

if you don't want to do anything in terms of user notification if id is not found in the list then you can ignore the case where current is NULL at the end of the loop and do something like this:
  1. if(current != NULL) //id was found
  2. {
  3. if(current == first)// id is in the first node of the list
  4. //do something, see my previous posts for pseudocode
  5. else
  6. //do somethng else, see my previous posts for psuedocode.
  7. }

It is very unlikely that you really want to assign the int in current->id as the address of an object stored in current like you try to do in line 28 post #21
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List - Need help deleting!

 
0
  #23
Nov 21st, 2008
Now, I'm back to deleting the first node again. Is the contents in this function is what you were guiding me towards?

should it be current = current->link->link

  1. void deleteSong(mp3Type*& first)
  2. {
  3.  
  4. mp3Type *last,*current,*trailcurrent,*previous;
  5. int id = 0;
  6. bool found = false;
  7.  
  8. current = first;
  9.  
  10. cout<<"Enter song ID that you like to delete"<<endl;
  11. cout<<endl;
  12. cin>>id;
  13.  
  14.  
  15. if (current != NULL && current->id == id)
  16. {
  17. if (current == first)
  18. {
  19. first = current->link;
  20. delete current;
  21. }
  22. else
  23. current = current->link;
  24. delete current;
  25. }
  26. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #24
Nov 21st, 2008
  1. if (current == first)
  2. {
  3. first = current->link;
  4. delete current;
  5. }
This is fine.

I've emphasized the importance of the role the previous node has in deleting a node from either the interior or the end of list in several previous posts and have to do so again. You won't get the code working correctly without it. Look at my pseudocode and my posts to figure out how to use it.

In addition
  1. else
  2. current = current->link;
  3. delete current;
current = current->link; advances current one node down the list. It does nothing about removing current from the list. Then deleting current without connecting previous to next breaks the list into two and the second half is lost because you can no longer access it since the last node of the first part of the list doesn't know the first node of the last part of the list. Take a look at the following:

A->B->C->D

If you delete B without connecting A to C before deleting B then you end up with:

A and C->D

A would still be accessable as first, but there is no way to reach C and D any longer since A knows nothing about C since you didn't store Cs address in As pointer before deleting B.

In your function you could find B okay, but then the current = current->link; statement will advance current to C and your delete current; statement would then delete C, the third node in the list, not the second, giving you

A->B and D but not A->C->D like you want.




Also the delete current statement would often be inside the body of the else statement, not outside of it. Since you don't use {}s after the else statement in post #23 it is outside the body of the else. In this case that's okay since it is legal to call delete on a NULL pointer, though it isn't something I recommend.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List - Need help deleting!

 
0
  #25
Nov 21st, 2008
Hey, I updated the function. I added "previous" to the code, but am I suppose to set it to an mp3type? My function for deleteSong is updated and my full program is below. I tested out the deleteSong function, but when I delete the 3rd song, it does nothing. When delete the first song, it deletes the 2nd song and then it returns garbage numbers afterwards! Where do I go from here? and I closer to my destination? lol


Here is my updated function for deleteSong:

  1. void deleteSong(mp3Type*& first)
  2. {
  3.  
  4. mp3Type *last,*current,*previous;
  5. int id = 0;
  6. bool found = false;
  7.  
  8. current = first;
  9.  
  10. cout<<"Enter song ID that you like to delete"<<endl;
  11. cout<<endl;
  12. cin>>id;
  13.  
  14.  
  15. if (current != NULL && current->id == id)
  16. {
  17. if (current == first)
  18. {
  19. previous = current;
  20. current = current->link;
  21. delete current;
  22. }
  23. else
  24. {
  25. current = current->previous;
  26. delete current;
  27. }
  28. }
  29. }



Here is the full program:



  1. #include <iostream>
  2. #include <string>
  3.  
  4. const int SONGS = 3;
  5.  
  6. using namespace std;
  7. struct mp3Type
  8. {
  9. int id;
  10. string songTitle;
  11. string artist;
  12. string length;
  13. string size;
  14. mp3Type *link,*previous;
  15. };
  16.  
  17.  
  18. void addSong(mp3Type*& first, mp3Type*& last);
  19. void deleteSong(mp3Type*& first);
  20. void modifySong(mp3Type*& first, mp3Type*& last);
  21. void printSong(mp3Type*& first);
  22.  
  23.  
  24. int main()
  25. {
  26. mp3Type *first, *last,*next;
  27. char selection;
  28.  
  29.  
  30. do
  31. {
  32. cout<<"a - Add a song "<<endl;
  33. cout<<"d - Delete a song"<<endl;
  34. cout<<"m - Modify a song's information "<<endl;
  35. cout<<"p - Print a list of songs "<<endl;
  36. cout<<"q - Quit "<<endl;
  37. cin>>selection;
  38. switch(selection)
  39. {
  40. case 'a': addSong(first,last);
  41. break;
  42. case 'd': deleteSong(first);
  43. break;
  44. case 'm': modifySong(first,last);
  45. break;
  46. case 'p': printSong(first);
  47. break;
  48. case 'q':
  49. break;
  50. default: cout<<"Invalid option "<<endl;
  51. }
  52. }
  53. while (selection != 'q');
  54.  
  55.  
  56. system("PAUSE");
  57. return 0;
  58. }
  59.  
  60. void addSong(mp3Type*& first, mp3Type*& last)
  61. {
  62.  
  63.  
  64. int id;
  65. char songTitle[100];
  66. char artist[100];
  67. string length;
  68. string size;
  69. mp3Type *newNode;
  70. char selection;
  71.  
  72. first = NULL;
  73. last = NULL;
  74.  
  75.  
  76.  
  77. for (int i = 0; i < SONGS; i++)
  78. {
  79. cout<<endl;
  80. cout<<"Adding song # "<<i+1<<endl;
  81. cout<<endl;
  82. cout<<"\nSong title: ";
  83. std::cin.getline(songTitle, 100);
  84. std::cin.getline(songTitle, 100);
  85. cout<<"ID: ";
  86. cin>>id;
  87. cout<<"Artist: ";
  88. std::cin.getline(artist, 100);
  89. std::cin.getline(artist, 100);;
  90. cout<<"Length: ";
  91. cin>>length;
  92. cout<<"Size: ";
  93. cin>>size;
  94.  
  95. newNode = new mp3Type; // create new node
  96. newNode->songTitle = songTitle;
  97. newNode->id = id;
  98. newNode->artist = artist;
  99. newNode->length = length;
  100. newNode->size = size;
  101. newNode->link = NULL;
  102.  
  103. if (first == NULL)
  104. {
  105. first = newNode;
  106. last = newNode;
  107. }
  108. else
  109. {
  110. last->link = newNode;
  111. last = newNode;
  112. }
  113. }
  114.  
  115. }
  116.  
  117. void deleteSong(mp3Type*& first)
  118. {
  119.  
  120. mp3Type *last,*current,*previous;
  121. int id = 0;
  122. bool found = false;
  123.  
  124. current = first;
  125.  
  126. cout<<"Enter song ID that you like to delete"<<endl;
  127. cout<<endl;
  128. cin>>id;
  129.  
  130.  
  131. if (current != NULL && current->id == id)
  132. {
  133. if (current == first)
  134. {
  135. previous = current;
  136. current = current->link;
  137. delete current;
  138. }
  139. else
  140. {
  141. current = current->previous;
  142. delete current;
  143. }
  144. }
  145. }
  146.  
  147. void modifySong(mp3Type*& first, mp3Type*& last)
  148. {
  149.  
  150. int id;
  151. char newsongTitle[100];
  152. char newartist[100];
  153. string newlength;
  154. string newsize;
  155. bool found = false;
  156. mp3Type *current;
  157.  
  158. printSong(first);
  159.  
  160. current = first;
  161.  
  162.  
  163. cout<<"Enter the id of the song to modify information"<<endl;
  164. cin>>id;
  165.  
  166.  
  167. while (current != NULL)
  168. {
  169. if (current->id == id)
  170. {
  171. cout<<endl;
  172. cout<<"Song title: ";
  173. std::cin.getline(newsongTitle, 100);
  174. std::cin.getline(newsongTitle, 100);
  175. cout<<"Artist: ";
  176. std::cin.getline(newartist, 100);
  177. cout<<"Length: ";
  178. cin>>newlength;
  179. cout<<"Size: ";
  180. cin>>newsize;
  181.  
  182. current->songTitle = newsongTitle;
  183. current->artist = newartist;
  184. current->length = newlength;
  185. current->size = newsize;
  186. }
  187. current = current->link;
  188. }
  189.  
  190. }
  191.  
  192. void printSong(mp3Type*& first)
  193. {
  194.  
  195. cout<<"Printing song list..."<<endl;
  196. mp3Type *current;
  197.  
  198.  
  199. current = new mp3Type;
  200. current = first;
  201. while (current != NULL)
  202. {
  203. cout<<endl;
  204. cout<<current-> songTitle <<endl;
  205. cout<<current-> id <<endl;
  206. cout<<current-> artist <<endl;
  207. cout<<current-> length <<endl;
  208. cout<<current-> size <<endl;
  209. cout<<endl;
  210. current = current->link;
  211. }
  212. }
Last edited by NinjaLink; Nov 21st, 2008 at 7:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Linked List - Need help deleting!

 
0
  #26
Nov 21st, 2008
Mission completed. Thanks guys! =)
Reply With Quote Quick reply to this message  
Reply

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




Views: 1234 | Replies: 25
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC