c++ - 具有结构数据类型的链表

标签 c++ struct linked-list declaration

如何在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;

编辑:

这是幕后发生的事情,编译器将替换每个 TDVD .

回到你的代码,我们得到这个:

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/

相关文章:

java - 使用链表上的递归进行快速排序

c++ - 使用 std::optional<int> 是否与使用 int 一样有效?

c - 读取 CSV 文件 - 内存问题

c - 我应该更好地使用全局变量吗?

c - 修改链表中结构体的字段

java - 在运行时在类中修改值

algorithm - 同步两个有序列表

c++ - QList::iterator 只返回 (error)0

c++ - 需要更好的等待解决方案

c++ - find_if 的 Lambda 表达式