在单个链表交换节点 [英] Swapping Nodes on a single linked list

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

问题描述

由于长时间后,我没有使用C或C ++,所以完全忘记了指针。我熟悉C#和已经写了这个基本版本。要知道我是否做对/错?

输入:链表A-> B-> C-> D-> E->空

输出:链表B-> A-> D-> C-> E->空

我们必须写code,这样的内存位置交换,而不是节点值。

 公共无效SwapLinkedList(LinkedList的< INT> LL)
    {
        一个LinkedListNode< INT>电流= LL.First;
        而(current.Next!= NULL)
        {
            INT TEMP = current.Next.Value;
            current.Next.Value = current.Value;
            current.Value =温度;
            电流= current.Next.Next;
        }
    }
 

解决方案

一个LinkedListNode 顺序的LinkedList 即可T事业可以改变的一个LinkedListNode 只允许获得的 previous 下一步属性。因此,要在修改排序的的LinkedList 你只能交换价值(允许一组)。

因此​​,为了得到这个工作,我会用这样的一些推广方法,使交换一点更普遍的:

 公共静态类LinkedListExtensions
{
    公共静态链表< T> SwapPairwise< T>(此LinkedList< T>源)
    {
        如果(来源== NULL)
            抛出新ArgumentNullException(源);

        无功电流= source.First;

        如果(当前== NULL)
            返回源;

        而(current.Next!= NULL)
        {
            current.SwapWith(current.Next);
            电流= current.Next;

            如果(电流!= NULL)
                电流= current.Next;
        }

        返回源;
    }

    公共静态无效SwapWith< T>(这一个LinkedListNode< T>首先,一个LinkedListNode< T>第二个)
    {
        如果(第一== NULL)
            (第一)抛出新ArgumentNullException;

        如果(二== NULL)
            抛出新ArgumentNullException(二);

        VAR TMP = first.Value;
        first.Value = second.Value;
        second.Value = TMP;
    }
}
 

Since long back I have not used C or C++ ,so forget completely about Pointers. I'm familiar with C# and have written a basic version of this. Need to know whether I'm doing right/wrong?

Input:Linked List a->b->c->d->e->null

Output: Linked List b->a->d->c->e->null

We have to write code such that memory position is swapped and not the node value.

    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;
        }
    }

解决方案

The LinkedListNode order within a LinkedList can't be changed cause the LinkedListNode only allows a get on the Previous and Next properties. So to change the ordering within a LinkedList you can only swap the values (which allow a set).

So to get this to work, i would use some extension methods like these to make the swapping a little more general:

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;
    }
}

这篇关于在单个链表交换节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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