繁体   English   中英

链接列表,结构和类

[英]Linked Lists, structs, and classes

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

您需要向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类。 并且您需要添加一种遍历列表的方法。

尝试这个:

#include <list>

class Song
{
};

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

CompactDisc类包含一个{link}歌曲列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM