| | |
Linked List - Need help deleting!
Thread Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 345
Reputation:
Solved Threads: 0
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?
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)
#include <iostream> #include <string> const int SONGS = 2; using namespace std; struct mp3Type { int id; string songTitle; string artist; string length; string size; mp3Type *link; }; void addSong(mp3Type*& first, mp3Type*& last); void deleteSong(mp3Type*& first); void modifySong(mp3Type*& first, mp3Type*& last); void printSong(mp3Type*& first); int main() { mp3Type *first, *last; char selection; do { cout<<"a - Add a song "<<endl; cout<<"d - Delete a song"<<endl; cout<<"m - Modify a song's information "<<endl; cout<<"p - Print a list of songs "<<endl; cout<<"q - Quit "<<endl; cin>>selection; switch(selection) { case 'a': addSong(first,last); break; case 'd': deleteSong(first); break; case 'm': modifySong(first,last); break; case 'p': printSong(first); break; case 'q': break; default: cout<<"Invalid option "<<endl; } } while (selection != 'q'); system("PAUSE"); return 0; } void addSong(mp3Type*& first, mp3Type*& last) { int id; char songTitle[100]; char artist[100]; string length; string size; mp3Type *newNode; char selection; first = NULL; last = NULL; for (int i = 0; i < SONGS; i++) { cout<<endl; cout<<"Adding song # "<<i+1<<endl; cout<<endl; cout<<"\nSong title: "; std::cin.getline(songTitle, 100); std::cin.getline(songTitle, 100); cout<<"ID: "; cin>>id; cout<<"Artist: "; std::cin.getline(artist, 100); std::cin.getline(artist, 100);; cout<<"Length: "; cin>>length; cout<<"Size: "; cin>>size; newNode = new mp3Type; // create new node newNode->songTitle = songTitle; newNode->id = id; newNode->artist = artist; newNode->length = length; newNode->size = size; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } } } void deleteSong(mp3Type*& first) { mp3Type *last,*current,*trailcurrent; bool found; int id; current = first; cout<<"Enter song ID that you like to delete"<<endl; cout<<endl; cin>>id; for (int i = 0; i < SONGS; i++) { if (first->link != NULL) { current = first->link; first->link = first->link->link; delete current; } else { found = false; trailcurrent = first; } current = first->link; } } void modifySong(mp3Type*& first, mp3Type*& last) { int id; char newsongTitle[100]; char newartist[100]; string newlength; string newsize; bool found = false; mp3Type *current; printSong(first); current = first; cout<<"Enter the id of the song to modify information"<<endl; cin>>id; while (current != NULL) { if (current->id == id) { cout<<endl; cout<<"Song title: "; std::cin.getline(newsongTitle, 100); std::cin.getline(newsongTitle, 100); cout<<"Artist: "; std::cin.getline(newartist, 100); cout<<"Length: "; cin>>newlength; cout<<"Size: "; cin>>newsize; current->songTitle = newsongTitle; current->artist = newartist; current->length = newlength; current->size = newsize; } current = current->link; } } void printSong(mp3Type*& first) { cout<<"Printing song list..."<<endl; mp3Type *current; current = new mp3Type; current = first; while (current != NULL) { cout<<endl; cout<<current-> songTitle <<endl; cout<<current-> id <<endl; cout<<current-> artist <<endl; cout<<current-> length <<endl; cout<<current-> size <<endl; cout<<endl; current = current->link; } }
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
your coded logic reads something like this: That doesn't correlate very well with your intentions.
C++ Syntax (Toggle Plain Text)
get information from user, but ignore it for as many songs as there are in the list if list is longer than two delete the second song in the list else assign a value to a flag but never evaluate the flags value
Klatu Barada Nikto
•
•
Join Date: Mar 2008
Posts: 345
Reputation:
Solved Threads: 0
•
•
•
•
your coded logic reads something like this:That doesn't correlate very well with your intentions.C++ Syntax (Toggle Plain Text)
get information from user, but ignore it for as many songs as there are in the list if list is longer than two delete the second song in the list else assign a value to a flag but never evaluate the flags value
should it be
for (int i = id; i < songs; i++)
first->link = current->link->link;
C++ Syntax (Toggle Plain Text)
void deleteSong(mp3Type*& first) { mp3Type *last,*current,*trailcurrent; bool found; int id; current = first; cout<<"Enter song ID that you like to delete"<<endl; cout<<endl; cin>>id; for (int i = 0; i < SONGS; i++) { if (first->link != NULL) { current = first->link; first->link = first->link->link; delete current; } else { found = false; trailcurrent = first; } current = first->link; } }
Last edited by NinjaLink; Nov 20th, 2008 at 2:24 pm.
•
•
•
•
your coded logic reads something like this:That doesn't correlate very well with your intentions.C++ Syntax (Toggle Plain Text)
get information from user, but ignore it for as many songs as there are in the list if list is longer than two delete the second song in the list else assign a value to a flag but never evaluate the flags value
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.....
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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
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
•
•
Join Date: Mar 2008
Posts: 345
Reputation:
Solved Threads: 0
•
•
•
•
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?
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- adding and deleting node in a linked list (C++)
- deleting a node with out traversing the linkedlist (C)
- Deleting Pointed node in Singly Linked List (C)
- linked list..quick question (Java)
- Bumping my object on a linked list (C++)
Other Threads in the C++ Forum
- Previous Thread: Convert Binary to Decimal and from Decimal to Binary
- Next Thread: How set fullscreen under win 2000 pro(console)?
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






