簡體   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