简体   繁体   English

C ++链表模板和节点

[英]c++ linked list template and Nodes

I'm currently attempting to create a template for my list class - which should be able to create a linkedlist of Employee classes which inherit Employee (Manager and Staff). 我目前正在尝试为我的列表类创建一个模板-该模板应该能够创建继承Employee(经理和职员)的Employee类的链表。

I'm confused as to how I initialize the list as when I do, it asks for a data type. 我对如何初始化列表感到困惑,因为它需要一种数据类型。

Here are some snipits of my code: 这是我的代码的一些片段:

template <class DataType>
struct Node
{
    DataType data;
    Node<DataType> *next;
};



template <class DataType>
class List
{
public:
    List();
    List(const List<DataType> & aplist);
    ~List();

    bool search(const DataType & element);  

    Node<DataType> first();
private:
    Node<DataType>* head;
    Node<DataType>* current;              
};

template <class DataType>
List<DataType>::List() 
{
    head = NULL;
}

template <class DataType>
List<DataType>::List(const List<DataType> & aplist)
{
    deepCopy(aplist);
}
... etc

I am slightly confused when it comes to creating the list; 在创建列表时,我有些困惑。 previously when I had my linkedlist in a .header file I used List myList; 以前,当我将链接列表放在.header文件中时,我使用了List myList; so now I tried this 所以现在我尝试了

List<> myList;

and it expects a data type. 并且期望数据类型。 So I tried List< DataType> myList; 所以我尝试了List <DataType> myList; and it doesn't like DataType even though it's declared in my template? 即使在模板中声明了它也不喜欢DataType? I also tried Node and get and error saying that the argument list for class template "Node" is missing. 我还尝试了Node,并得到了一个错误,并说缺少类模板“ Node”的参数列表。

I was wondering if anyone could explain how to enable my linked list (myList) to initialise and then allow entry of 3 different classes Employee (Base class), (Managerand Staff inherit Employee). 我想知道是否有人可以解释如何启用我的链表(myList)初始化,然后允许输入3个不同的类Employee(基类),(Manager和Staff继承Employee)。

Cheers 干杯

I would suggest you to try your implementation with something like an int: 我建议您尝试使用类似int的实现:

List<int> myList;

If that works, wour basic implementation of a linked list using a template is correct. 如果可行,则使用模板的链表的基本实现是正确的。 Now you can start creating your own datatypes like an employee class (if you do not already have some) and use that one. 现在,您可以开始创建自己的数据类型,例如员工类(如果您还没有),并使用该数据类型。 It should work exactly the same way. 它应该以完全相同的方式工作。

Please note that the name "DataType" in your class templates is not actually a data type, but a placeholder for a real data type like int, Employee, ... 请注意,类模板中的名称“ DataType”实际上不是数据类型,而是诸如int,Employee,...等真实数据类型的占位符。

You need to give the specific data type that you want the list to hold. 您需要提供要列表保留的特定数据类型。 So if it will hold Employee instances only this could be 因此,如果它将仅包含Employee实例,则可能是

List<Employee> myList

But if you want polymorphic behavior, then you'll need the nodes to store pointers; 但是,如果您想要多态行为,则将需要节点来存储指针。 eg 例如

List<Employee*> myList

Or, depending on your resource management needs 或者,取决于您的资源管理需求

List<std::unique_ptr<Employee>> myList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM