创建对象的链接列表

发布于 2024-10-26 07:02:27 字数 951 浏览 2 评论 0原文

我有一个类作业(阅读:不使用 STL),我需要在其中创建一个排序的对象链接列表,但我不太确定如何执行此操作。

我使用的类同时包含整数和字符串成员,但只会对这些整数成员之一进行排序。我目前有一个功能齐全的链表模板,可以使用整数数据成功运行。

现在我的问题在于将其转换为适用于我的班级。实例化此链表时,必须定义 ,在本例中,类型为 Poster,位于我正在排序的 Poster 类之后。但是,在链表类的声明中,有一个 class Node 的声明和定义,其中写着:

class Node
{
public:
    Type Element;
    Node *Next, *Previous;

    Node() : Next(NULL), Previous(NULL) {} // Default constructor
    Node (Type Data, Node *PNode = NULL) : // Non-default constructor
        Element (Data),
        Next (PNode),
        Previous (PNode) {}
};

我不确定当 Poster 的成员时,此现有定义将如何工作code> 在 LinkedList时引入listOfPosters 已声明。我是否应该将上面的 Node 定义替换为 class Poster 的内容,或者将节点中的 Type Element 标记为某种 catch -class Poster 成员的所有容器,以便可以通过 Element.GetMemberValue() 访问 Poster 的成员?

I have a class assignment (read: no use of the STL) where I need to create a sorted linked list of objects, but I'm not quite sure how to go about doing this.

The class that I am using contains both integer and string members, but it is only one of these integer members that will be sorted. I currently have a fully functioning linked list template that will successfully run with integer data.

Now my problem lies in converting this to work with my class. When instantiating this linked list, a <Type> must be defined, in this case that type is Poster, after the class Poster that I am sorting. However, in the declaration of the linked list class, there is a declaration and definition of the class Node which reads

class Node
{
public:
    Type Element;
    Node *Next, *Previous;

    Node() : Next(NULL), Previous(NULL) {} // Default constructor
    Node (Type Data, Node *PNode = NULL) : // Non-default constructor
        Element (Data),
        Next (PNode),
        Previous (PNode) {}
};

I'm unsure how this existing definition will work when the members of Poster are introduced when LinkedList<Poster> listOfPosters is declared. Should I replace the above definition of Node with the contents of class Poster, or will Type Element in the node be marked as a sort of catch-all container for the members of class Poster, so that members of Poster can be accessible via Element.GetMemberValue()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-11-02 07:02:28

我猜测 LinkedList 类的声明看起来像

template<class Type>
class LinkedList
{ ... }

当使用链表类(主程序)的程序实例化一个 LinkedList 类

LinkedList<Poster> myLinkedList;

时,如下所示 编译器在编译时从模板生成代码,并替换所有出现的“类型”与“海报”。因此,您不需要更改链表或节点类。

Im guessing that the declaration of the LinkedList class looks something like

template<class Type>
class LinkedList
{ ... }

When the program that uses the linked list class (the main program) instantiates a LinkedList class like following

LinkedList<Poster> myLinkedList;

The compiler, at compile time, generates code from the template, and replaces all occurrences of "Type" with "Poster". Therefore, you dont need to change the linked-list or node class.

苏大泽ㄣ 2024-11-02 07:02:28

Node.Element 成员将存储 Poster 的(按值)副本。无需手动替换 Node 定义中的任何内容;这就是 C++ 模板的全部意义。

当您声明 LinkedList 对象时,编译器将生成一个新类,并用 Poster 替换 Type 占位符的所有实例。这将在 LinkedList 类中自动创建一个适当的 Node 类,该类表示保存 Poster 对象的节点。

The Node.Element member will store a (by-value) copy of the Poster. There is no need to manually replace anything in the definition of Node; that's the whole point of C++ templates.

When you declare a LinkedList<Poster> object, the compiler will generate a new class and substitute all instances of the Type placeholder with Poster. This will automatically create an appropriate Node class inside the LinkedList<Poster> class that represents nodes which hold a Poster object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文