gpt4 book ai didi

c++ - 链接列表、结构和类

转载 作者:行者123 更新时间:2023-11-28 00:32:35 26 4
gpt4 key购买 nike

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

关于c++ - 链接列表、结构和类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22259093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com