如何在main中声明一个数据类型为struct的链表实例? 例如......我应该有一个包含信息结构的类。链表应该具有结构的数据类型。这是指令:DVD 类具有结构,其数据成员之一是链表的实例,该结构作为数据类型。
class DVD
{
private:
struct disc
{
int length;
string name;
};
链表
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;
};
最佳答案
您所要做的就是在尖括号之间指定类型,如下所示:
LinkedList1<DVD> myLinkedList;
编辑:
这是幕后发生的事情,编译器将替换每个 T
与 DVD
.
回到你的代码,我们得到这个:
struct discList
{
DVD value; // You used to have T here
struct discList *next; // To point to the next node
};
你将有一个链表,其中每个节点都是一个包含 DVD
的结构。值(value)。
关于c++ - 具有结构数据类型的链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178886/