Frage

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;
}
War es hilfreich?

Lösung

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.

Andere Tipps

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.

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top