【发布时间】:2014-04-06 17:49:11
【问题描述】:
如何在链表中附加一个类的结构。 该类将使用具有 dvd 标题及其长度的结构。每个 dvd 都将是该结构的一个实例,并将存储在链表中。此外,链表应具有该结构的数据类型
class DVD
{
private:
struct disc
{
int length;
string title;
}my_disc;
public:
// Constructor
DVD(int, string);
};
链表
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;
};
会不会
// Declare a DVD object
DVD dvd(105, "Spider Man"); // length and title
// Declare a linked list object with the data type of the struct disc.
LinkedList1<DVD> movie;
// or
LinkList1<DVD::my_disc> movie;
// and then append it
movie.appendNode(dvd)
如果我从class DVD 中删除结构并将数据成员length 和title 作为私有成员,那么我知道LinkedList1<DVD> movie; 可以附加节点。结构让我失望。我不明白“链表的数据类型应该是struct”。对我来说这似乎是LinkedList1<disc>movie;,因为disc 是结构的名称。有什么想法吗?
【问题讨论】:
标签: c++ linked-list append structure