943,770 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1511
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2008
0

Linked List - Need help deleting!

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


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

your coded logic reads something like this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

The whole code for deleteSong is wrong? o_O
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

Click to Expand / Collapse  Quote originally posted by Lerner ...
your coded logic reads something like this:
C++ Syntax (Toggle Plain Text)
  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;


C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

I think it has something to do with my pointer, but how do i point it to the right direction?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

Click to Expand / Collapse  Quote originally posted by Lerner ...
your coded logic reads something like this:
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

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
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

check line no. 73. & 74.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

Click to Expand / Collapse  Quote originally posted by Lerner ...
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?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Nov 20th, 2008
0

Re: Linked List - Need help deleting!

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
Rhohitman is offline Offline
81 posts
since Dec 2007

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: Convert Binary to Decimal and from Decimal to Binary
Next Thread in C++ Forum Timeline: How set fullscreen under win 2000 pro(console)?





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


Follow us on Twitter


© 2011 DaniWeb® LLC