链表中的链接列表 [英] Linked list within a linked list

查看:93
本文介绍了链表中的链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我很抱歉,如果之前已经问过这个问题,但我确实在发布之前搜索了

并且无法找到问题的答案。我有两个

类,每个类都有相应的链表,list1和list2,每个在list1中的$​​ b $ b节点都有各种数据,需要指向

list2中的相应节点,但我无法弄清楚如何做到这一点。


有人可以解释我可能会遗漏的内容,或者可能会指出我的内容

一篇好文章的方向会帮助我吗?因为

应该这么简单,这真的让我感到难过。


谢谢,

Josh

Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in the
direction of a good article which would help me out? Because for
something that should be so simple, it''s really got me stumped.

Thanks,
Josh

推荐答案

" joshd" < jo *** @ joshd.cawrote in message

news:11 ********************** @ h48g2000cwc.googlegr oups .com
"joshd" <jo***@joshd.cawrote in message
news:11**********************@h48g2000cwc.googlegr oups.com

您好,


对不起,如果之前已经问过这个问题,但我确实搜了过来
发布前
并无法找到问题的答案。我有两个

类,每个类都有相应的链表,list1和list2,每个在list1中的$​​ b $ b节点都有各种数据,需要指向

list2中的相应节点,但我无法弄清楚如何做到这一点。


有人可以解释我可能会遗漏的内容,或者可能会指出我的内容

一篇好文章的方向可以帮助我吗?因为

应该是这么简单的东西,这真的让我难过。
Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in
the direction of a good article which would help me out? Because for
something that should be so simple, it''s really got me stumped.



我不知道这个问题与你帖子的标题有什么关系。您的描述中没有任何内容可以在链表中建议链接列表。


更一般地说,明确说明问题会有所帮助。这是一次性的同步列表,还是必须重复完成

列表?如果是这样,列表如何添加到?这两个班级是否是b $ b b彼此的朋友?是对应节点吗?在
中出现的节点在列表中的位置相同?


假设对上一个问题的答案是肯定的,对于一次性的

同步,您可以通过迭代两个列表来实现。

a列表项目的地址由


&(* iter)


给出,其中iter是迭代器。同时迭代两个列表意味着

您可以同时检索相应项目的地址并且

然后在每个列表中存储指针。显然,


列表中的T1< T1list1;


将需要一个成员变量,该变量是指向

list< T2list2;


,反之亦然。


如果你想在运行中同步,然后你需要同时添加两个

列表(或者在每个列表上搜索以找到对应的节点

)。如果你添加到两个列表的末尾,那么你可以




&(* list1.end())和& (* list2.end())


添加完成后。在开头添加是类似的。


如果在中间添加使用insert添加单个元素,则插入

返回指向新的迭代器元件。你可以按照上面说明的相同模式从

这个迭代器中检索一个指针。


-

John Carson

I don''t see how this question relates to the title of your post. There is
nothing in your description to suggest a linked list within a linked list.

More generally, stating the problem clearly would help. Is this a one-off
synchronisation of the lists or does it have to be done repeatedly as the
lists are added to? If so, how are the lists added to? Are the two classes
friends of each other? Is the "corresponding node" the node that occurs in
the same position in the list?

Assuming the answer to the last question is yes, for a one-off
synchronisation, you can do it by iterating over both lists. The address of
a list item is given by

&(*iter)

where iter is the iterator. Iterating over both lists simultaneously means
you can retrieve the addresses of corresponding items at the same time and
then store a pointer in each. Obviously the T1 in

list<T1list1;

will need to have a member variable that is a pointer to the T2 type in

list<T2list2;

and vice versa.

If you want synchronisation on the run, then you will need to add to both
lists at the same time (or else do a search over each list to find the nodes
that are "corresponding"). If you add to the end of both lists, then you can
get the address from

&(*list1.end()) and &(*list2.end())

after you have made the addition. Adding at the beginning is analogous.

If you add in the middle using insert to add a single element, then insert
returns an iterator pointing to the new element. You retrieve a pointer from
this iterator following the same pattern as illustrated above.

--
John Carson



joshd写道:
joshd wrote:

你好,


我很抱歉,如果之前已经问过这个问题,但我确实在发布之前搜索了

并且无法找到问题的答案。我有两个

类,每个类都有相应的链表,list1和list2,每个在list1中的$​​ b $ b节点都有各种数据,需要指向

list2中的相应节点,但我无法弄清楚如何做到这一点。


有人可以解释我可能会遗漏的内容,或者可能会指出我的内容

一篇好文章的方向会帮助我吗?因为

应该是这么简单的东西,这真的让我难过。
Hello,

Im sorry if this question has been asked before, but I did search
before posting and couldnt find an answer to my problem. I have two
classes each with corresponding linked lists, list1 and list2, each
node within list1 has various data and needs to have a pointer to the
corresponding node in list2, but I cant figure out how to do this.

Could someone explain what I might be missing, or maybe point me in the
direction of a good article which would help me out? Because for
something that should be so simple, it''s really got me stumped.



这只是一个概念证明,即如何使用std :: list。

这些是使用迭代器指向彼此。


#include< list>


//声明2个结构

struct A; < br $>
struct B;


//定义相应的列表类型

typedef std :: list< A Alist_t;

typedef std :: list< B Blist_t;


//定义类

struct A

{

Blist_t :: iterator bitr;


A(const A&){} //没有复制bitr

A() {}

};


struct B

{

Alist_t :: iterator aitr;

B(const B&){} //没有复制aitr!

B(){}

};


//一些愚蠢的示例代码

void tst()

{

Alist_t al;

Blist_t bl;


//制作A元素

al.push_fron t(A());


//制作B元素

bl.push_front(B());


//设置迭代器以指向彼此

al.front()。bitr = bl.begin();

bl.front()。 aitr = al.begin();


}


int main()

{

tst();

}

This is just a proof of concept, i.e. how to do it using std::list.
These are using iterators to point to one another.

#include <list>

// declare 2 structs
struct A;
struct B;

// define the respective list type
typedef std::list<A Alist_t;
typedef std::list<B Blist_t;

// define the classes
struct A
{
Blist_t::iterator bitr;

A( const A & ) {} // no copying bitr
A() {}
};

struct B
{
Alist_t::iterator aitr;
B( const B & ) {} // no copying aitr !
B() {}
};

// some silly example code
void tst()
{
Alist_t al;
Blist_t bl;

// make an A element
al.push_front( A() );

// make a B element
bl.push_front( B() );

// Set up iterators to point to each other
al.front().bitr = bl.begin();
bl.front().aitr = al.begin();

}

int main()
{
tst();
}


我很抱歉我的第一篇帖子太清楚了,我还在尝试

来掌握链表概念。也许这个可视化的表现形式我会用父母的方式记录下来,这将有助于你了解我的

需求。


父链接列表

|

父节点-child linklist + child2 list + child3 list + ....

父节点-child linklist + child2 list + ....

父节点-child linklist + child2 list + ....


这可以在没有迭代器的情况下完成吗?或者我应该学习更多东西来围绕STL和迭代器包裹我的




感谢您的帮助!


Josh

I apologize that I wasnt too clear with my first post, Im still trying
to grasp the linked list concept. Maybe this visual representation I
jotted out, with a parent-child scenario, will help you understand my
needs.

parent linklist
|
parent node -child linklist + child2 list + child3 list + ....
parent node -child linklist + child2 list + ....
parent node -child linklist + child2 list + ....

Can this be done without iterators? or should I study more to wrap my
mind around STL and iterators?

Thanks for all your help!

Josh


这篇关于链表中的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆