Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
交换单个链接列表上的节点 自从很久以来我就没有使用C或C++,所以完全忘记指针。我熟悉C#,并且已经写了一个基本版本。需要知道我做的对/错吗_C#_.net_Algorithm_Linked List - Fatal编程技术网

交换单个链接列表上的节点 自从很久以来我就没有使用C或C++,所以完全忘记指针。我熟悉C#,并且已经写了一个基本版本。需要知道我做的对/错吗

交换单个链接列表上的节点 自从很久以来我就没有使用C或C++,所以完全忘记指针。我熟悉C#,并且已经写了一个基本版本。需要知道我做的对/错吗,c#,.net,algorithm,linked-list,C#,.net,Algorithm,Linked List,输入:链表a->b->c->d->e->null 输出:链表b->a->d->c->e->null 我们必须编写这样的代码,以便交换内存位置,而不是节点值 public void SwapLinkedList(LinkedList<int> LL) { LinkedListNode<int> current = LL.First; while (current.Next != null) {

输入:链表a->b->c->d->e->null

输出:链表b->a->d->c->e->null

我们必须编写这样的代码,以便交换内存位置,而不是节点值

    public void SwapLinkedList(LinkedList<int> LL)
    {
        LinkedListNode<int> current = LL.First;
        while (current.Next != null)
        {
            int temp = current.Next.Value;
            current.Next.Value = current.Value;
            current.Value = temp;
            current = current.Next.Next;
        }
    }
public void交换列表(LinkedList LL)
{
LinkedListNode当前=LL.First;
while(current.Next!=null)
{
int temp=当前.Next.Value;
current.Next.Value=当前.Value;
当前值=温度;
current=current.Next.Next;
}
}

无法更改
LinkedList
中的
LinkedListNode
顺序,因为
LinkedListNode
仅允许访问
上一个
下一个
属性。因此,要更改
链接列表中的顺序,您只能交换值(允许设置)

因此,为了实现这一点,我将使用一些类似的扩展方法使交换更加通用:

public static class LinkedListExtensions
{
    public static LinkedList<T> SwapPairwise<T>(this LinkedList<T> source)
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var current = source.First;

        if (current == null)
            return source;

        while (current.Next != null)
        {
            current.SwapWith(current.Next);
            current = current.Next;

            if (current != null)
                current = current.Next;
        }

        return source;
    }

    public static void SwapWith<T>(this LinkedListNode<T> first, LinkedListNode<T> second)
    {
        if (first == null)
            throw new ArgumentNullException("first");

        if (second == null)
            throw new ArgumentNullException("second");

        var tmp = first.Value;
        first.Value = second.Value;
        second.Value = tmp;
    }
}
公共静态类LinkedListensions
{
公共静态LinkedList SwapPairwise(此LinkedList源)
{
if(source==null)
抛出新的ArgumentNullException(“源”);
无功电流=电源。首先;
如果(当前==null)
返回源;
while(current.Next!=null)
{
当前.SwapWith(当前.Next);
当前=当前。下一步;
如果(当前!=null)
当前=当前。下一步;
}
返回源;
}
公共静态无效交换(首先是此LinkedListNode,其次是LinkedListNode)
{
if(first==null)
抛出新的ArgumentNullException(“第一”);
if(秒==null)
抛出新的异常(“第二个”);
var tmp=第一个值;
first.Value=second.Value;
第二,价值=tmp;
}
}

无法更改
LinkedList
中的
LinkedListNode
顺序,因为
LinkedListNode
仅允许访问
上一个
下一个
属性。因此,要更改
链接列表中的顺序,您只能交换值(允许设置)

因此,为了实现这一点,我将使用一些类似的扩展方法使交换更加通用:

public static class LinkedListExtensions
{
    public static LinkedList<T> SwapPairwise<T>(this LinkedList<T> source)
    {
        if (source == null)
            throw new ArgumentNullException("source");

        var current = source.First;

        if (current == null)
            return source;

        while (current.Next != null)
        {
            current.SwapWith(current.Next);
            current = current.Next;

            if (current != null)
                current = current.Next;
        }

        return source;
    }

    public static void SwapWith<T>(this LinkedListNode<T> first, LinkedListNode<T> second)
    {
        if (first == null)
            throw new ArgumentNullException("first");

        if (second == null)
            throw new ArgumentNullException("second");

        var tmp = first.Value;
        first.Value = second.Value;
        second.Value = tmp;
    }
}
公共静态类LinkedListensions
{
公共静态LinkedList SwapPairwise(此LinkedList源)
{
if(source==null)
抛出新的ArgumentNullException(“源”);
无功电流=电源。首先;
如果(当前==null)
返回源;
while(current.Next!=null)
{
当前.SwapWith(当前.Next);
当前=当前。下一步;
如果(当前!=null)
当前=当前。下一步;
}
返回源;
}
公共静态无效交换(首先是此LinkedListNode,其次是LinkedListNode)
{
if(first==null)
抛出新的ArgumentNullException(“第一”);
if(秒==null)
抛出新的异常(“第二个”);
var tmp=第一个值;
first.Value=second.Value;
第二,价值=tmp;
}
}

如果您有LinkedListNode首选的引用,请删除并添加:

public static LinkedListNode<T> SwapWith<T>(LinkedListNode<T> first, LinkedListNode<T> second)
{
        first.List.Remove(first);
        second.List.AddAfter(second, first);
        return second;
}
公共静态LinkedListNode交换(LinkedListNode第一,LinkedListNode第二)
{
第一。列表。删除(第一);
second.List.AddAfter(second,first);
返回第二;
}

如果您有LinkedListNode首选的引用,请删除并添加:

public static LinkedListNode<T> SwapWith<T>(LinkedListNode<T> first, LinkedListNode<T> second)
{
        first.List.Remove(first);
        second.List.AddAfter(second, first);
        return second;
}
公共静态LinkedListNode交换(LinkedListNode第一,LinkedListNode第二)
{
第一。列表。删除(第一);
second.List.AddAfter(second,first);
返回第二;
}

定义“交换”。这段代码将第一个列表值正确地放在它的末尾,这是您想要它做的吗?看看这个线程定义的“交换”。这段代码将第一个列表值正确地放在了它的末尾,这是你想要它做的吗?看看这个线程