0

I need to add a CD with all of its songs to a collection. I'm trying to figure out how to remove a CD from a CD collection. For example, When I append the artist name, the name of the CD, and the title, and length of each song and call the display function it displays all of the information. When I call the deleteNode function with the artist, name of CD, and length of CD as parameters, the display function still displays what I appended. The program builds fine but I don't think I'm calling the deleteNode function correctly. Also, the linked list has the data type of the class.

The Struct

class CD { private: string artist; // To hold artist nam string name; // To hold name of CD struct disc { string title; // To hold title of the song double length; // To hold length of the song }my_disc; } 

Calling the deleteNode function from main

void remove_cd(LinkedList1<CD> *remove) { cout << "Enter the name of the artist of the CD you wish to remove: "; cin.ignore(); getline(cin, artist); cout << "Enter the title: "; cin >> title; cout << "Enter the length: "; cin >> length; CD removeNode(artist, title, length); remove->deleteNode(removeNode); } 

If I use the artist, name, and length) as parameters for deleteNode how do I get it to remove all of the songs that have been inserted too? I want to actually delete the CD

The deleteNode function in the linkedlist

template <class T> void LinkedList1<T>::deleteNode( T searchValue) { discList **pp = &head; while (*pp && (*pp)->value != searchValue) pp = &(*pp)->next; if (*pp) { discList *victim = *pp; *pp = victim->next; delete victim; } } 

Overloaded operators in CD

bool CD::operator == (const CD &e) { if (artist == e.artist) return true; return false; } bool CD::operator != (const CD &e) { if (artist != e.artist) return true; return false; } 
2
  • I am trying to use a minimal code as necessary. Please tell me if you need to see anymore code. Commented Mar 6, 2014 at 17:20
  • See how is make from liked list data structure. Commented Mar 6, 2014 at 17:26

3 Answers 3

1

I can't think of something much shorter without entering into the ill-advised land of recursion, and even then it would be a stretch.

This will do what you want.

template<typename T> void LinkedList1<T>::deleteNode(const T& searchValue) { discList **pp = &head; while (*pp && (*pp)->value != searchValue) pp = &(*pp)->next; if (*pp) { discList *victim = *pp; *pp = victim->next; delete victim; } } 

And before you ask, yes, it works with an empty list and a null head pointer. This algorithm uses the actual pointers in the actual list, not just their values, the pointers, to traverse and destroy. It assumes your list is also terminated with NULL..

Finally, your algorithm (and this one) relies on there being an logical-equality operator defined for CD, of which I see none. If you've not implemented one yet, you need to do so.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I will try this when I get home. I have overloaded == and != operators in CD. discList is the name of the struct in the linked list.
@Lilspree ok. then that makes sense. I thought it was from something else. this should work for you then.
Ok I just checked it but when I call my display function it still displays the stuff I appended. Maybe there is something wrong with the way I append it? If I post the append code could you look at it?
I'll need more code, but this function provably removes matching elements. Either the equality operator isn't doing its job (easy to test, looks for a known element and debug this function), or you're passing copies of your container into whatever eventually calls this.
I updated code with what you suggested. I also added the overloaded operators above
1

I am trying to use a minimal code as necessary.

I'm so happy that you've added that comment. You can simply use an std::list like this:

class CD { private: std::string artist; // To hold artist nam std::string name; // To hold name of CD struct disc { std::string title; // To hold title of the song double length; // To hold length of the song } my_disc; }; std::list<CD> list; 

add a node with:

list.emplace_back(/* args for CD constructor */); 

and delete a node with:

list.pop_back(); 

This is as minimal and as error free it can get. Of course if you feel the weight of the double-linked list implementation you can fall back to std::forward_list any time you want.

2 Comments

I think he might need to actually devise how to remove an item from a home-made linked list all by himself, in which case std::list and the like won't do him any good. Still, yours is a good suggestion.
Thanks for the responses unfortunately this deleteNode function is the one I was told to use and it may or may not need changing. I appended the nodes using CD songsInfo(artist, title, length);' append->appendNode(songsInfo);` and a loop appended the songs.
0

its c++, but can be useful.

template<class CAdat> bool List<CAdat>::Delete(const string &name) { Node *tmp = head->next; while (tmp != head && tmp->data.getName() != name) { tmp = tmp->next; } if (tmp != head) { tmp->prev->next = tmp->next; tmp->next->prev = tmp->prev; delete tmp; return true; } return false; } 

1 Comment

What about the case in which the list is empty? head will hold a null pointer, so line head->next will make the entire world to come to an end.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.