Lilspree
Lilspree

Reputation: 135

Linked Lists, structs, and classes

The CD object should have a data member that is a linked list of your song structure that you have in CD. The CD class needs a function that allows it to add a song to the object, that function would then append the song to that instance of the linked list (the one with the structure). I have been up all night and I am so confused by this when I shouldn't be. Any pointers to lead me in the right direction?

How do I create a CD object CD cd; that has a data member that is a linked list of my struct Song?

CD class with the struct

class CD : public Media
{
    private:
        string artist;      // To hold the artist name

    public:
        // Declare a struct
        struct Song
        {
            string title;
            double length;
        }my_disc;

        CD();
        CD(string);
        CD(string, string, double);

        // Mutators
        void setArtist(string);

        // Accessors
        string getArtist();

        // Overloaded operators
        bool operator == (const CD &e);
        bool operator != (const CD &e);
}

Linked List

#include "CD.h"

template <class T>
class LinkedList1 
{
    private:
        // Declare a structure
        struct discList
        {
            T value;
            struct discList *next;  // To point to the next node
        };

        discList *head;     // List head pointer

    public:
        // Default Constructor
        LinkedList1()
        { head = NULL; }


        // Destructor
        ~LinkedList1();

        // Linked list operations
        void appendNode(T);
        void insertNode(T);
        void deleteNode(T);
        void displayList() const;
};
//***********************************************
// appendNode function adds the node to the end *
// list. It accepts 3 arguments.                *
//***********************************************
template <class T>
void LinkedList1<T>::appendNode(T newValue)
{
    discList *newNode;      // To point to a new node
    discList *nodePtr;      // To move through the list

    // Allocate a new node and store num there
    newNode = new discList;
    newNode->value = newValue;
    newNode->next = NULL;

    // If there are no nodes in the list make 
    // newNode the first node
    if (!head)
    {
        head = newNode;
    }
    else        // Otherwise, insert newNode at end
    {
        // Intialize nodePtr to head of list
        nodePtr = head;

        // Find the last node in the list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }

        // Insert newNode as the last node
        nodePtr->next = newNode;
    }
}

//***********************************************
// insertNode function inserts the node in      *
// numerical order. It accepts 3 arguments.     *
//***********************************************
template <class T>
void LinkedList1<T>::insertNode(T newValue)
{
    discList *newNode;              // A new node
    discList *nodePtr;              // To traverse the list
    discList *previousNode = NULL;  // The previous node

    // Allocate a new node and store the title there
    newNode = new discList<T>(newValue);

    // If there are no nodes in the list make 
    // newNode the first node
    if (!head)
    {
        head = newNode;
        newNode->next = NULL;
    }
    else    // Otherwise, insert newNode
    {
        // Position nodePtr at the head of list
        nodePtr = head;

        // Initialize previousNode to NULL
        previousNode = NULL;

        // Skip all nodes whose value is less than the title
        while (nodePtr != NULL && nodePtr->title < t)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        // If the new node is to be the 1st in the list
        // insert it before all other nodes
        if (previousNode == NULL)
        {
            head = newNode;
            newNode->next = nodePtr;
        }
        else    // Otherwise insert after the previous node
        {
            previousNode->next = newNode;
            newNode->next = nodePtr;
        }
    }
}

//***********************************************
// deleteNode function removes the node from the*
// list without breaking the links created by   *
// the next pointers and removes the node from  *
// memory.                                      *
//***********************************************
template <class T>
void LinkedList1<T>::deleteNode( T searchValue)
{
    discList *nodePtr;          // To traverse the list
    discList *previousNode;     // To point to the previous node

    // If the list is empty, do nothing
    if (!head)
    {
        return;
    }

    // Determine if the first node is the one
    if (head->value==searchValue)
    {
        nodePtr = head->next;
        delete head;
        head = nodePtr;
    }
    else
    {
        // Intialize nodePtr to head of list
        nodePtr = head;

        // Skip all nodes whose value member is not 
        // equal to num
        while (nodePtr != NULL && nodePtr->value != searchValue)
        {
            previousNode = nodePtr;
            nodePtr = nodePtr->next;
        }

        // If nodePtr is not at the end of the list, link
        // the previous node to the node after nodePtr,
        // the delete nodePtr
        if (nodePtr)
        {
            previousNode->next = nodePtr->next;
            delete nodePtr;
        }
    }
}

//***********************************************
// displayList shows the value stored in each   *
// node of the linked list pointed to by head   *
//***********************************************
template <class T>
void LinkedList1<T>::displayList() const
{
    discList *nodePtr;          // To move through the list

    // Postion nodePtr at the head of the list
    nodePtr = head;



    // While nodePtr points to a node, traverse the list
    while (nodePtr)
    {
        // Display the value in this node
        cout << left << setw(12) << nodePtr->value.getArtist();
        cout << setw(12) << nodePtr->value.getName();
        cout << setw(12) << nodePtr->value.getLength();
        cout << setw(8) << nodePtr->value.my_disc.title;
        cout << setw(8) <<  nodePtr->value.my_disc.length << endl;

        // Move to the next node
        nodePtr = nodePtr->next;
    }
}


//***********************************************
// Destructor function deletes every node in the*
// list.                                        *
//***********************************************
template <class T>
LinkedList1<T>::~LinkedList1()
{
    discList *nodePtr;          // To traverse the list
    discList *nextNode;         // To point to the next node

    // Position nodePtr at the head of the list
    nodePtr = head;

    // While nodePtr is not at the end of the list
    while (nodePtr != NULL)
    {
        // Save a pointer to the next node
        nextNode = nodePtr->next;

        // Delete the current node
        delete nodePtr;

        // Position nodePtr at the next node
        nodePtr = nextNode;
    }
}

Upvotes: 1

Views: 934

Answers (2)

001
001

Reputation: 13533

You need to add a member to CD to hold the list of songs:

class CD : public Media
{
    // Declare a struct
    // It only holds data for one song
    struct Song
    {
        string title;
        double length;
    };
private:
    // LL to hold all songs
    LinkedList1<Song>my_disc;

public:
    // Method to add song
    void AddSong(string title, double length)
    {
       Song temp = {title, length};
       my_disc.appendNode(temp);
    }
// Other stuff removed
};

Also, you should remove void LinkedList1<T>::displayList() const as it really belongs in the CD class. And you need to add a way to iterate through the list.

Upvotes: 2

Thomas Matthews
Thomas Matthews

Reputation: 57749

Try this:

#include <list>

class Song
{
};

class CompactDisc
{
  public:
    std::list<Song> song_list;
};

The CompactDisc class contains a {linked} list of songs.

Upvotes: 1

Related Questions