c#中单链表的扩展方法 为什么扩展方法在插入操作中不返回修改的节点 但它在创建链表时工作正常 扩展方法应返回修改后的节点 这样做的最佳方式是什么 扩展方法的性能好吗
代码如下c#中单链表的扩展方法 为什么扩展方法在插入操作中不返回修改的节点 但它在创建链表时工作正常 扩展方法应返回修改后的节点 这样做的最佳方式是什么 扩展方法的性能好吗,c#,algorithm,linked-list,extension-methods,C#,Algorithm,Linked List,Extension Methods,代码如下 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)
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.
}
}
您的代码有很多错误,我们可以稍后再处理。但让我们先回答你的问题。我将有点直截了当,因为我不能假设你为什么要这样做 为什么扩展方法在插入操作中不返回修改的节点 因为你的方法没有返回任何东西 但它在创建链表时工作正常 是的,因为该代码从未修改
此节点
参数
扩展方法应返回修改后的节点
只有当您实际返回方法中的任何数据时
这样做的最佳方式是什么
见下文
扩展方法的性能好吗
扩展方法与什么相比?与类似编写的成员方法相比,在与您的示例相关的情况下,应该没有性能差异
完美的方法:
所以首先要做的是:这里不需要编写扩展方法。为什么不编写一个常规成员方法?当您想要添加功能的类不能直接供您编辑时,通常会进行扩展,因为代码属于第三方
其次,您似乎不太了解引用以及传递值是如何工作的。首先让我发布一个更好的代码,然后解释它
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=linkedlist.SetFirstNode(20)代码>
现在,您实际上可以编写一个静态方法并通过ref传递linkedlist,但在我看来,这不是一个好的实践。然而,代码如下所示
public static class ListOperations {
public static void InsertNode(ref Node linkedList, object data) {
linkedList = new Node(data) { NextNode = linkedList };
}
}
在其他需要注意的事项中,我有意将节点
对象调用为linkedList
,CreateLinkedList
调用为AppendNode
,InsertNode
调用为SetFirstNode
,这样您可以更好地理解代码
下面是相同的代码,使用泛型参数而不是对象数据
,并使用适当的插入节点
方法
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);
}
}
公共类节点{
公共T数据{get;set;}
公共节点下一个{get;set;}
公共重写字符串ToString(){
返回Data.ToString();
}
公共节点(T数据){
数据=数据;
}
公共节点AppendNode(T数据){
var newNode=新节点(数据);
无功电流=此;
while(current.Next!=null)
当前=当前。下一步;
current.Next=newNode;
返回newNode;
}
///
///将新节点作为所需位置插入linkedlist
///
///新节点最终位置的基于0的索引
///新创建的包含数据的节点
///返回linkedlist的第一个节点
公共节点InsertNode(T数据、int位置、out节点newNode){
无功电流=此;
位置--;
newNode=新节点(数据);
如果(位置<0){
newNode.Next=当前;
返回newNode;
}
对于(int i=0;i
您无法在扩展方法()中重新分配此
。插入节点
无效这就是为什么它不返回任何内容我该做什么?您尝试过调试吗?为什么要为此创建扩展方法?这些是对节点
数据类型的操作。把它们作为你喜欢的方法。此外,方法的定义也不合适。您的createlist实际上只创建一个节点。