Linked List - Need help deleting!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 1,692
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: 268
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #11
Nov 20th, 2008
Or something like that yes.
  1. node * currentNode
  2. node * previousNode
  3.  
  4. //where to start
  5. currentNode = first;
  6.  
  7. //find desired node
  8. while(currentNode != NULL && currentNode->id != id)
  9. previousNode = currentNode;
  10. currentNode = currentNode->next;
  11.  
  12. //when loop stops, evaluate why.
  13. if(currentNode == NULL)
  14. id not found in list
  15. //else id found so now do the deletion part
Last edited by Lerner; Nov 20th, 2008 at 7:11 pm.
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
  #12
Nov 21st, 2008
Originally Posted by Lerner View Post
Or something like that yes.
  1. node * currentNode
  2. node * previousNode
  3.  
  4. //where to start
  5. currentNode = first;
  6.  
  7. //find desired node
  8. while(currentNode != NULL && currentNode->id != id)
  9. previousNode = currentNode;
  10. currentNode = currentNode->next;
  11.  
  12. //when loop stops, evaluate why.
  13. if(currentNode == NULL)
  14. id not found in list
  15. //else id found so now do the deletion part


I tried to follow what you told me to do, but now it doesn't delete anything on the list at all. Is there something I did wrong in the code?



Here is the program with deleteSong function updated:



  1. #include <iostream>
  2. #include <string>
  3.  
  4. const int SONGS = 2;
  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,*next;
  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,*trailcurrent,*previous,*next;
  121. bool found;
  122. int id;
  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. while(current != NULL && current->id != id)
  132. {
  133. previous = current;
  134. current = current->next;
  135.  
  136. if(current == NULL)
  137. {
  138. found = false;
  139. }
  140. else
  141. {
  142. found = true;
  143. delete current;
  144. }
  145. }
  146.  
  147.  
  148. }
  149.  
  150. void modifySong(mp3Type*& first, mp3Type*& last)
  151. {
  152.  
  153. int id;
  154. char newsongTitle[100];
  155. char newartist[100];
  156. string newlength;
  157. string newsize;
  158. bool found = false;
  159. mp3Type *current;
  160.  
  161. printSong(first);
  162.  
  163. current = first;
  164.  
  165.  
  166. cout<<"Enter the id of the song to modify information"<<endl;
  167. cin>>id;
  168.  
  169.  
  170. while (current != NULL)
  171. {
  172. if (current->id == id)
  173. {
  174. cout<<endl;
  175. cout<<"Song title: ";
  176. std::cin.getline(newsongTitle, 100);
  177. std::cin.getline(newsongTitle, 100);
  178. cout<<"Artist: ";
  179. std::cin.getline(newartist, 100);
  180. cout<<"Length: ";
  181. cin>>newlength;
  182. cout<<"Size: ";
  183. cin>>newsize;
  184.  
  185. current->songTitle = newsongTitle;
  186. current->artist = newartist;
  187. current->length = newlength;
  188. current->size = newsize;
  189. }
  190. current = current->link;
  191. }
  192.  
  193. }
  194.  
  195. void printSong(mp3Type*& first)
  196. {
  197.  
  198. cout<<"Printing song list..."<<endl;
  199. mp3Type *current;
  200.  
  201.  
  202. current = new mp3Type;
  203. current = first;
  204. while (current != NULL)
  205. {
  206. cout<<endl;
  207. cout<<current-> songTitle <<endl;
  208. cout<<current-> id <<endl;
  209. cout<<current-> artist <<endl;
  210. cout<<current-> length <<endl;
  211. cout<<current-> size <<endl;
  212. cout<<endl;
  213. current = current->link;
  214. }
  215.  
  216.  
  217. }
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
  #13
Nov 21st, 2008
Here is my deleteSong function that I updated. It is still not deleting the node that the user input. I have been trying to figure this out for week. This is frustrating..


  1. void deleteSong(mp3Type*& first)
  2. {
  3.  
  4. mp3Type *last,*current,*trailcurrent,*previous,*next;
  5. bool found;
  6. int id;
  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->next;
  19. }
  20. if(current == NULL)
  21. {
  22. found = false;
  23. }
  24. else
  25. {
  26. found = true;
  27. delete current;
  28. }
  29.  
  30.  
  31.  
  32. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 332
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 64
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Linked List - Need help deleting!

 
0
  #14
Nov 21st, 2008
here's a simple sample code...
i don't test it yet ...
let me know the result...
  1. void push_back(mp3Type **p_type, int _id, string _sTitle, string _artist, string _length, string _size)
  2. {
  3. mp3Type *p = new mp3Type;
  4. if (&p == NULL)
  5. return;
  6. p->id = _id;
  7. p->songTitle = _sTitle;
  8. p->artist = _artist;
  9. p->length = _length;
  10. p->size = _size;
  11. p->next = *p_type;
  12. *p_type = p;
  13. }
  14.  
  15. void delete(mp3Type *p_type, mp3Type **ret_Type)
  16. {
  17. mp3Type *t, *n;
  18. t = p_type;
  19. int id = 0;
  20. cout<<"Enter song ID that you like to delete"<<endl;
  21. cout<<endl;
  22. cin>>id;
  23. while (t != NULL)
  24. {
  25. if (t->id == id)
  26. {
  27. t = t->next;
  28. continue; //-- pass
  29. }
  30. int t_id = t->id;
  31. string t_sTitle = t->songTitle;
  32. string t_artist = t->artist;
  33. string t_length = t->length;
  34. string t_size = t->size;
  35. push_back(&n,t_id,t_sTitle,t_artist,t_length,t_size);
  36. t = t->next;
  37. }
  38. *ret_Type = n;
  39. }
  40.  
  41. //--
  42. //-- To call delete
  43. //-- mp3Type *main_Type, *temp_Type;
  44. //-- delete(main_Type, &temp_Type);
  45. //--
Last edited by cikara21; Nov 21st, 2008 at 4:21 am.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
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: 268
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #15
Nov 21st, 2008
Unless you plan to use the variable called found somewhere there is no sense declaring it, changing it's value, etc.

There is a reason why previousNode is included in the pseudocode. It's because you need to know which node is the one just before the current node in order to connect it with the one just after the current node. That way when you delete the current node the list remains intact, you haven't created two separate lists from the original.

Since the first node in the list doesn't have a previous node you don't need to reconnect previous to next when you delete it, you can just reassign.

The last node in the list doesn't have a node after it. Won't that need to be handled as a special case, too? No it won't. At least it won't if you make sure all pointers to node in new nodes are assigned the value NULL when the new node is declared. If that's done then NULL can be assigned to the previous node just like an actual node.
  1. if(currentNode == NULL)
  2. //id not in list
  3. //if id is in the first node
  4. else if(currentNode == first) OR
  5. else if(previousNode == NULL) //assuming previous is defaulted to NULL on declaration
  6. //reassign second node in list to be first
  7. //delete current node
  8. else //id is in list and it's not in the first position
  9. //assign node after current node to node previous to current node
  10. //delete current node

This algorhithm works well for deleting the first instance of id in the list. It can be modified slightly if you want to delete all instances of id in the list.
Last edited by Lerner; Nov 21st, 2008 at 11:32 am.
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
  #16
Nov 21st, 2008
Originally Posted by cikara21 View Post
here's a simple sample code...
i don't test it yet ...
let me know the result...
  1. void push_back(mp3Type **p_type, int _id, string _sTitle, string _artist, string _length, string _size)
  2. {
  3. mp3Type *p = new mp3Type;
  4. if (&p == NULL)
  5. return;
  6. p->id = _id;
  7. p->songTitle = _sTitle;
  8. p->artist = _artist;
  9. p->length = _length;
  10. p->size = _size;
  11. p->next = *p_type;
  12. *p_type = p;
  13. }
  14.  
  15. void delete(mp3Type *p_type, mp3Type **ret_Type)
  16. {
  17. mp3Type *t, *n;
  18. t = p_type;
  19. int id = 0;
  20. cout<<"Enter song ID that you like to delete"<<endl;
  21. cout<<endl;
  22. cin>>id;
  23. while (t != NULL)
  24. {
  25. if (t->id == id)
  26. {
  27. t = t->next;
  28. continue; //-- pass
  29. }
  30. int t_id = t->id;
  31. string t_sTitle = t->songTitle;
  32. string t_artist = t->artist;
  33. string t_length = t->length;
  34. string t_size = t->size;
  35. push_back(&n,t_id,t_sTitle,t_artist,t_length,t_size);
  36. t = t->next;
  37. }
  38. *ret_Type = n;
  39. }
  40.  
  41. //--
  42. //-- To call delete
  43. //-- mp3Type *main_Type, *temp_Type;
  44. //-- delete(main_Type, &temp_Type);
  45. //--

That is code is hard for me to understand.
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
  #17
Nov 21st, 2008
Originally Posted by Lerner View Post
Unless you plan to use the variable called found somewhere there is no sense declaring it, changing it's value, etc.

There is a reason why previousNode is included in the pseudocode. It's because you need to know which node is the one just before the current node in order to connect it with the one just after the current node. That way when you delete the current node the list remains intact, you haven't created two separate lists from the original.

Since the first node in the list doesn't have a previous node you don't need to reconnect previous to next when you delete it, you can just reassign.

The last node in the list doesn't have a node after it. Won't that need to be handled as a special case, too? No it won't. At least it won't if you make sure all pointers to node in new nodes are assigned the value NULL when the new node is declared. If that's done then NULL can be assigned to the previous node just like an actual node.
  1. if(currentNode == NULL)
  2. //id not in list
  3. //if id is in the first node
  4. else if(currentNode == first) OR
  5. else if(previousNode == NULL) //assuming previous is defaulted to NULL on declaration
  6. //reassign second node in list to be first
  7. //delete current node
  8. else //id is in list and it's not in the first position
  9. //assign node after current node to node previous to current node
  10. //delete current node

This algorhithm works well for deleting the first instance of id in the list. It can be modified slightly if you want to delete all instances of id in the list.

Do I need to have "next" anymore? The last time I tried to include it, it said that it wasn't an mp3Type
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
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: 268
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #18
Nov 21st, 2008
You may use either link or next, but I wouldn't use both. They serve the same purpose. I usually use the name next but the name link makes sense too. It makes it harder for requestors of homework help to just cut and paste if answers I don't use all the same variable names they use. Hopefully, this forces the requestors learn soemthing about the concepts rather than the specifics when it comes to common algorithms. With the more complicated stuff that clearly isn't homework, then I'm more comfortable using the same names for all the variables as the requestor.
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
  #19
Nov 21st, 2008
Should I use bool found?
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
  #20
Nov 21st, 2008
I got it so that it deletes the first node, but the rest are still on the list. When I delete the first node though and then print the list, the first node is delete, but , the thing is, it leaves a piece of junk behind.


Here is my updated 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. }
  24. else
  25. {
  26. found = true;
  27. delete current;
  28. }
  29. }
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC