创建自定义链表C++

Creating a custom Linked List C++

本文关键字:C++ 链表 自定义 创建      更新时间:2023-10-16

我最近开始学习C++,目前正在尝试制作自己的双链表。我不知道应该怎么做。我是否应该使用已经存在的列表类来存储我的数据和键或创建我自己的节点和链表类。还是我应该只使用 C 使用的链表?

节点结构和容器结构不同。

单链表:

struct node{
    node* next;
    int data; // Not necessarily an int, but whatever the node will contain.
};
struct container{
    node* head;
};

双链表:

struct node{
    node* next;
    node* prev;  // Unlike the single linked list, a double linked list's node contains a pointer to the previous element as well.
};
struct container{
   node* head;
   node* tail;
};

请记住,当您执行任何操作(即推、弹出、拉等)时,您必须考虑这两个指针。