链接列表,结构和类

时间:2014-03-07 19:34:04

标签: c++ struct linked-list

CD对象应该有一个数据成员,它是CD中您的歌曲结构的链接列表。 CD类需要一个允许它向对象添加歌曲的功能,该功能然后将歌曲附加到链接列表的实例(具有结构的那个)。我一整夜都在上班,当我不应该这样的时候,我很困惑。是否有指导我的方向正确?

如何创建一个CD对象CD cd;,其数据成员是我的结构Song的链接列表?

带结构

的CD类
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);
}

链接列表

#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;
    }
}

2 个答案:

答案 0 :(得分:2)

您需要将一个成员添加到CD以保存歌曲列表:

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
};

此外,您应该删除void LinkedList1<T>::displayList() const类,因为它确实属于CD类。而且你需要添加一种迭代列表的方法。

答案 1 :(得分:1)

试试这个:

#include <list>

class Song
{
};

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

CompactDisc类包含{链接}歌曲列表。