如何添加 LinkedList<T>到 LinkedList<T>在 C# 中? [英] How does one add a LinkedList<T> to a LinkedList<T> in C#?

查看:24
本文介绍了如何添加 LinkedList<T>到 LinkedList<T>在 C# 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会想到简单的代码

llist1.Last.Next = llist2.First;
llist2.First.Previous = llist1.Last;

可以工作,但显然在 C# 的 LinkedList、First、Last 中,并且它们的属性仅为 Get.

would work, however apparently in C#'s LinkedList, First, Last, and their properties are Get only.

我能想到的另一种方法是

The other method I could think of was

llist1.AddLast(llist2.First);

然而,这也不起作用——它失败了,因为 llist2 的第一个节点已经在一个链表中.

However, this does not work either - it fails because the first node of llist2 is already in a linked list.

这是否意味着我必须有一个循环将 llist2 的每个节点手动添加到 llist1?这不是破坏链表的效率吗????

Does this mean that I have to have a loop that manually AddLast's each node of llist2 to llist1? Doesn't this defeat the efficiency of linked lists????

推荐答案

是的,不幸的是,您必须循环.这是一个 O(n) 操作 - 对于添加的每个条目 O(1).没有需要调整缓冲区大小和复制等的风险 - 尽管垃圾收集当然可能会做粗略的 :) 您甚至可以编写方便的扩展方法:

Yes, you have to loop, unfortunately. This is an O(n) operation - O(1) for each entry added. There's no risk of requiring a buffer to be resized and copied, etc - although of course garbage collection might do roughly that :) You could even write handy extension methods:

public static class LinkedListExtensions   
{
    public static void AppendRange<T>(this LinkedList<T> source,
                                      IEnumerable<T> items)
    {
        foreach (T item in items)
        {
            source.AddLast(item);
        }
    }

    public static void PrependRange<T>(this LinkedList<T> source,
                                       IEnumerable<T> items)
    {
        LinkedListNode<T> first = source.First;
        // If the list is empty, we can just append everything.
        if (first is null)
        {
            AppendRange(source, items);
            return;
        }

        // Otherwise, add each item in turn just before the original first item
        foreach (T item in items)
        {
            source.AddBefore(first, item);
        }
    }
}

Erich 的评论说明了为什么您可能认为这是低效的 - 为什么不通过更新next"将两个列表连接在一起?第一个列表尾部的指针和prev"第二个头部的指针?好吧,想想第二个列表会发生什么......也会发生变化.

Erich's comment suggests why you might think this is inefficient - why not just join the two lists together by updating the "next" pointer of the tail of the first list and the "prev" pointer of the head of the second? Well, think about what would happen to the second list... it would have changed as well.

不仅如此,这些节点的所有权会怎样?现在每个本质上都是两个列表的一部分......但是 LinkedListNode.List 属性只能谈论其中一个.

Not only that, but what would happen to the ownership of those nodes? Each is essentially part of two lists now... but the LinkedListNode<T>.List property can only talk about one of them.

虽然我可以理解为什么在某些情况下您可能想要这样做,但 .NET LinkedList 类型的构建方式基本上禁止它.我认为这个文档评论解释得最好:

While I can see why you might want to do this in some cases, the way that the .NET LinkedList<T> type has been built basically prohibits it. I think this doc comment explains it best:

LinkedList) 类不支持链接、拆分、循环或其他可以将列表保留在不一致的位置状态.

The LinkedList<T>) class does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.

这篇关于如何添加 LinkedList&lt;T&gt;到 LinkedList&lt;T&gt;在 C# 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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