使用 ref 删除链表中间的节点,仅授予对该节点的访问权限
本文关键字:节点 权限 访问权 访问 删除 ref 链表 中间 使用 | 更新日期: 2023-09-27 18:33:46
我有解决方案。所以我不需要帮助,但我有一个问题。此代码有效:
public void Delete(ref Node n)
{
n = n.next;
}
LinkedList list = new LinkedList();
list.AddTail(4);
list.AddTail(3);
list.AddTail(2);
list.AddTail(1);
list.AddTail(0);
list.Delete(ref list.head.next.next);
但此代码不会:
Node n = list.head.next.next;
list.Delete(ref n);
为什么?
编辑:
public class Node
{
public int number;
public Node next;
public Node(int number, Node next)
{
this.number = number;
}
}
当你打电话时
list.Delete(ref list.head.next.next);
将引用(指针)更改为字段 List.next。
但是如果你打电话
list.Delete(ref n);
更改局部变量的引用(指针)。
只需尝试内联您的代码:
list.Delete(ref list.head.next.next);
看来
list.head.next.next = list.head.next.next.next;
但
Node n = list.head.next.next;
list.Delete(ref n);
看来
Node n = list.head.next.next;
n = n.next;