Linked List - Need help deleting!

Please support our C++ advertiser: Intel Parallel Studio Home
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

Linked List - Need help deleting!

 
0
  #1
Nov 20th, 2008
Hello, I'm having a simple problem.

For my program, each song has an id number which stores the songs information. For my deleteSong function, I am asking the user what id of the song does he/she wants to be deleted. After the user enters the id of the song he/she wants to delete, I then print out the list of songs. After I print the list of songs, the problem is the song that is chosen to be deleted remains on the list, while all other songs that were on the list is now deleted. Basically, I just want the song that user chooses to delete, but instead of it being deleted, the others are deleted. How do I solve this problem in my code?


  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;
  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;
  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;
  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. for (int i = 0; i < SONGS; i++)
  131. {
  132. if (first->link != NULL)
  133. {
  134. current = first->link;
  135. first->link = first->link->link;
  136. delete current;
  137. }
  138. else
  139. {
  140. found = false;
  141. trailcurrent = first;
  142. }
  143. current = first->link;
  144. }
  145.  
  146.  
  147. }
  148.  
  149. void modifySong(mp3Type*& first, mp3Type*& last)
  150. {
  151.  
  152. int id;
  153. char newsongTitle[100];
  154. char newartist[100];
  155. string newlength;
  156. string newsize;
  157. bool found = false;
  158. mp3Type *current;
  159.  
  160. printSong(first);
  161.  
  162. current = first;
  163.  
  164.  
  165. cout<<"Enter the id of the song to modify information"<<endl;
  166. cin>>id;
  167.  
  168.  
  169. while (current != NULL)
  170. {
  171. if (current->id == id)
  172. {
  173. cout<<endl;
  174. cout<<"Song title: ";
  175. std::cin.getline(newsongTitle, 100);
  176. std::cin.getline(newsongTitle, 100);
  177. cout<<"Artist: ";
  178. std::cin.getline(newartist, 100);
  179. cout<<"Length: ";
  180. cin>>newlength;
  181. cout<<"Size: ";
  182. cin>>newsize;
  183.  
  184. current->songTitle = newsongTitle;
  185. current->artist = newartist;
  186. current->length = newlength;
  187. current->size = newsize;
  188. }
  189. current = current->link;
  190. }
  191.  
  192. }
  193.  
  194. void printSong(mp3Type*& first)
  195. {
  196.  
  197. cout<<"Printing song list..."<<endl;
  198. mp3Type *current;
  199.  
  200.  
  201. current = new mp3Type;
  202. current = first;
  203. while (current != NULL)
  204. {
  205. cout<<endl;
  206. cout<<current-> songTitle <<endl;
  207. cout<<current-> id <<endl;
  208. cout<<current-> artist <<endl;
  209. cout<<current-> length <<endl;
  210. cout<<current-> size <<endl;
  211. cout<<endl;
  212. current = current->link;
  213. }
  214.  
  215.  
  216. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
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: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #2
Nov 20th, 2008
your coded logic reads something like this:
  1. get information from user, but ignore it
  2.  
  3. for as many songs as there are in the list
  4. if list is longer than two
  5. delete the second song in the list
  6. else
  7. assign a value to a flag but never evaluate the flags value
That doesn't correlate very well with your intentions.
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
  #3
Nov 20th, 2008
The whole code for deleteSong is wrong? o_O
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
  #4
Nov 20th, 2008
Originally Posted by Lerner View Post
your coded logic reads something like this:
  1. get information from user, but ignore it
  2.  
  3. for as many songs as there are in the list
  4. if list is longer than two
  5. delete the second song in the list
  6. else
  7. assign a value to a flag but never evaluate the flags value
That doesn't correlate very well with your intentions.


should it be

for (int i = id; i < songs; i++)

first->link = current->link->link;


  1. void deleteSong(mp3Type*& first)
  2. {
  3.  
  4. mp3Type *last,*current,*trailcurrent;
  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. for (int i = 0; i < SONGS; i++)
  15. {
  16. if (first->link != NULL)
  17. {
  18. current = first->link;
  19. first->link = first->link->link;
  20. delete current;
  21. }
  22. else
  23. {
  24. found = false;
  25. trailcurrent = first;
  26. }
  27. current = first->link;
  28. }
  29.  
  30.  
  31. }
Last edited by NinjaLink; Nov 20th, 2008 at 2:24 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
  #5
Nov 20th, 2008
I think it has something to do with my pointer, but how do i point it to the right direction?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Linked List - Need help deleting!

 
0
  #6
Nov 20th, 2008
Originally Posted by Lerner View Post
your coded logic reads something like this:
  1. get information from user, but ignore it
  2.  
  3. for as many songs as there are in the list
  4. if list is longer than two
  5. delete the second song in the list
  6. else
  7. assign a value to a flag but never evaluate the flags value
That doesn't correlate very well with your intentions.

Let me put in right way..
you are creating the renewing list each time you add new nodes (new songs).
in every insertion ... your list pointer is set in to NULL. causing the earlier information to run in vein.

....other part later...
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
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: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Linked List - Need help deleting!

 
0
  #7
Nov 20th, 2008
It's more than that.

In order to delete the node you have to find it first. Then, since this is a singly linked list, you need to find the node before the node you want to delete (unless of course you want to delete the first node in the list). Then you point the node prior to the one you want to delete to the one after the one you want to delete. That will remove the desired node from the list, but not delete the memory used to store the node so use the keyword delete to actually delete the desired node
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Linked List - Need help deleting!

 
0
  #8
Nov 20th, 2008
check line no. 73. & 74.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
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
  #9
Nov 20th, 2008
Originally Posted by Lerner View Post
It's more than that.

In order to delete the node you have to find it first. Then, since this is a singly linked list, you need to find the node before the node you want to delete (unless of course you want to delete the first node in the list). Then you point the node prior to the one you want to delete to the one after the one you want to delete. That will remove the desired node from the list, but not delete the memory used to store the node so use the keyword delete to actually delete the desired node

Do I need an identifier called previousnode?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: Linked List - Need help deleting!

 
0
  #10
Nov 20th, 2008
There come 3 condition while deleting..
1. node is at the front
2. node is at last
3. node is in between

You need to have the previous node of the list in order to join again...

You can always refer to any reference book for the while.
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
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