[英]Nested Doubly Linked Lists in C++
What is the best way to go about storing doubly linked lists inside of one doubly linked list?在一个双向链表中存储双向链表的最佳方法是什么? Preferably, I would like to use only one struct, like so:
最好,我只想使用一个结构,如下所示:
struct node{
string data;
node* next = NULL;
node* prev = NULL;
};
and use this to be able to store a doubly linked list inside of a doubly linked list to hold all of my doubly linked lists.并使用它能够在双向链表中存储双向链表以保存我所有的双向链表。
I have an ordinary insert function, but that wouldn't work because the parameters are (node*& start, string data)
.我有一个普通的插入函数,但这不起作用,因为参数是
(node*& start, string data)
。 So I've created another insertion method that attempts to store a doubly linked list into a node of a larger doubly linked list, but it gets cloudy from there.因此,我创建了另一种插入方法,尝试将双向链表存储到更大的双向链表的节点中,但从那里开始变得模糊。 Any help would be appreciated.
任何帮助,将不胜感激。
Your node consists of data and links.您的节点由数据和链接组成。
You could insert a linked list as part of the data section:您可以插入一个链表作为数据部分的一部分:
struct Node
{
std::string Data
std::list</*...*/> nested_list;
Node * previous;
Node * next;
};
If you don't like using std::list
you could go with this:如果你不喜欢使用
std::list
你可以用这个:
struct Nested_Node
{
Nested_Node * previous;
Nested_Node * next;
/* add data if necessary */
};
struct Node
{
std::string data;
Nested_Node * nested_head;
Nested_Node * nested_tail;
Node * previous;
Node * next;
};
The above structures depend on how you want your nested linked list organized.上述结构取决于您希望如何组织嵌套链表。
Each Node
contains the head and tail of a linked list.每个
Node
包含一个链表的头和尾。 If you declare the nested list as a separate structure, you could replace the Nested_Node
pointers with the list class.如果将嵌套列表声明为单独的结构,则可以用列表类替换
Nested_Node
指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.