使用扩展方法在c#中单链表

问题描述 投票:-5回答:1
  1. 为什么扩展方法不会在Insertion操作中返回修改后的节点。
  2. 但它在链接列表创建时工作正常。
  3. Extension方法应该返回修改后的Node。
  4. 这样做的完美方式是什么?
  5. IS扩展方法性能良好

代码如下

public class Node
{
    public Object Data { get; set; }
    public Node NextNode { get; set; }
}
public static class ListOperations
{
    public static void CreateLinkedList(this Node node, Object data)
    {
        if (node.Data == null)
        {
            node.Data = data;
        }
        else
        {
            Node newnode = new Node();
            newnode.Data = data;
            Node current = new Node();
            current = node;
            while (current.NextNode != null)
            {
                current = current.NextNode;
            }
            current.NextNode = newnode;
            node = current;
        }
    }
    public static void InsertNode(this Node node1, Object data, int position)
    {

        Node newnode = new Node();
        newnode.Data = data;
        if (position == 1)
        {
            newnode.NextNode = node1;
            node1 = newnode;

        }

    }
}
class Program
{
    static void Main(string[] args)
    {
        Node node = new Node();
        //random Singly LinkedList
        node.CreateLinkedList(10);
        node.CreateLinkedList(11);
        node.CreateLinkedList(12);
        node.CreateLinkedList(13);
        node.CreateLinkedList(14);
        node.CreateLinkedList(15);
        node.InsertNode(20, 1);// this method does not return node value what is inserted.


    }
}
c# algorithm linked-list extension-methods
1个回答
1
投票

您的代码有很多问题,我们稍后可以处理它们。但我们先回答你的问题。我会有点文字和直接,因为我不能假设你为什么这样做完成它。

为什么扩展方法不会在Insertion操作中返回修改后的节点。

由于您的方法不返回任何内容

但它在链接列表创建时工作正常。

是的,因为该代码不会修改this Node node参数

Extension方法应该返回修改后的Node。

仅当您实际从方法返回任何数据时!

这样做的完美方式是什么?

见下文

IS扩展方法性能良好

扩展方法与什么相比?与类似编写的成员方法相比,在与您的示例相关的情况下确实应该没有性能差异

完美的方式:

首先要做的是:这里没有必要编写扩展方法。你为什么不写一个普通的成员方法?扩展通常在您要添加功能的类无法直接供您编辑时完成,通常是因为代码属于第三方

其次,您似乎不太了解引用以及传值的工作原理。首先让我发布一个更好的代码,然后解释它

public class Node {
    public object Data { get; set; }
    public Node NextNode { get; set; }

    public Node(object data) {
        Data = data;
    }

    public Node AppendNode(object data) {
        var newNode = new Node(data);

        var current = this;
        while (current.NextNode != null)
            current = current.NextNode;
        current.NextNode = newNode;

        return newNode;
    }

    public Node SetFirstNode(object data) {
        return new Node(data) { NextNode = this };
    }
}

class Program {
    static void Main(string[] args) {
        var linkedList = new Node(10);
        linkedList.AppendNode(11);
        linkedList.AppendNode(12);
        linkedList.AppendNode(13);
        linkedList.AppendNode(14);
        linkedList.AppendNode(15);
        linkedList = linkedList.SetFirstNode(20);

    }
}

从主要问题的角度注意的重要事项(为什么插入不起作用)是方法SetFirstNode实际上返回新创建的节点,在Main中,我们重新分配链接列表,如linkedList = linkedList.SetFirstNode(20);

现在,您实际上可以编写一个静态方法并通过引用链表来传递,但在我看来,这不是一个好的做法。不过,代码如下所示

public static class ListOperations {
    public static void InsertNode(ref Node linkedList, object data) {
        linkedList = new Node(data) { NextNode = linkedList };
    }
}

除了需要注意的事项之外,我将node对象称为linkedListCreateLinkedList称为AppendNodeInsertNode称为SetFirstNode,因此您可以更好地理解代码。

下面是使用泛型参数而不是object Data并使用适当的InsertNode方法的相同代码

public class Node<T> {
    public T Data { get; set; }
    public Node<T> Next { get; set; }

    public override string ToString() {
        return Data.ToString();
    }

    public Node(T data) {
        Data = data;
    }

    public Node<T> AppendNode(T data) {
        var newNode = new Node<T>(data);

        var current = this;
        while (current.Next != null)
            current = current.Next;
        current.Next = newNode;

        return newNode;
    }

    /// <summary>
    /// Inserts a new node into the linkedlist as the desired position
    /// </summary>
    /// <param name="position">0-based index for the final position of new node</param>
    /// <param name="newNode">The newly created node containing data</param>
    /// <returns>returns the first node of the linkedlist</returns>
    public Node<T> InsertNode(T data, int position, out Node<T> newNode) {
        var current = this;
        position--;
        newNode = new Node<T>(data);
        if (position < 0) {
            newNode.Next = current;
            return newNode;
        }

        for (int i = 0; i < position; ++i)
            current = current.Next;
        newNode.Next = current.Next;
        current.Next = newNode;
        return this;
    }
}

class Program {
    static void Main(string[] args) {
        var linkedList = new Node<int>(10);
        linkedList.AppendNode(11);
        linkedList.AppendNode(12);
        linkedList.AppendNode(13);
        linkedList.AppendNode(14);
        linkedList.AppendNode(15);
        linkedList = linkedList.InsertNode(20, 0, out var newNode);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.