我有一个Doubly Linked List和I方法,它应该删除一个给定的节点。当我最初开发代码时,它最初与我的3个测试节点一起使用但是当我向Doubly Linked List添加更多节点时它停止工作,现在我得到一个NullReferenceException:“对象引用未设置为对象的实例”我知道我可以使用通用链表但我正在尝试学习双链表如何工作。
我将在我的代码中标记行,我在行的开头用“**”获取此异常(它位于Doubly Linked List类的RemoveNode方法中)。
这是我的代码:
Doubly Linked List类:
class DoublyLinkedList
{
public Node head, current;
public void AddNode(object n) // add a new node
{
if (head == null)
{
head = new Node(n); //head is pointed to the 1st node in list
current = head;
}
else
{
while (current.next != null)
{
current = current.next;
}
current.next = new Node(n, current); //current is pointed to the newly added node
}
}
public Node FindNode(object n) //Find a given node in the DLL
{
current = head;
while ((current != null) && (current.data != n))
current = current.next;
if (current == null)
return null;
else
return current;
}
public string RemoveNode(object n)//remove nodes
{
String Output = "";
if (head == null)
{
Output += "\r\nLink list is empty";
}
else
{
current = FindNode(n);
if (current == null)
{
Output += "Node not found in Doubly Linked List\r\n";
}
else
{
****current.next.prev = current.prev;**
current.prev.next = current.next;
current.prev = null;
current.next = null;
Output += "Node removed from Doubly Linked List\r\n";
}
}
return Output;
}
public String PrintNode() // print nodes
{
String Output = "";
Node printNode = head;
if (printNode != null)
{
while (printNode != null)
{
Output += printNode.data.ToString() + "\r\n";
printNode = printNode.next;
}
}
else
{
Output += "No items in Doubly Linked List";
}
return Output;
}
}
节点类:
class Node
{
public Node prev, next; // to store the links
public object data; // to store the data in a node
public Node()
{
this.prev = null;
this.next = null;
}
public Node(object data)
{
this.data = data;
this.prev = null;
this.next = null;
}
public Node(object data, Node prev)
{
this.data = data;
this.prev = prev;
this.next = null;
}
public Node(object data, Node prev, Node next)
{
this.data = data;
this.prev = prev;
this.next = next;
}
}
答案 0 :(得分:2)
我得到了它的工作。虽然我略微改变了你的逻辑。我使用current
字段标记尾部,并使用单独的变量进行搜索。现在,我用整数值测试它,导致问题的是你比较值的行
(current.data != n)
你可以得到那个10!= 10,因为这些值被装箱并且引用不同。只需使用current.data.Equals(n)
代替。 Equals()
是一个虚拟方法,它向下冒泡到真实对象并比较数据而不是引用。
此外,您的删除逻辑必须更复杂。您还可以通过删除所有必要的if / else语句来使其更具可读性。
public void AddNode(object n) // add a new node
{
if (head == null)
{
head = new Node(n); //head is pointed to the 1st node in list
current = head;
}
else
{
var newNode = new Node(n, current);
current.next = newNode;
current = newNode; //current is pointed to the newly added node
}
}
public Node FindNode(object n) //Find a given node in the DLL
{
var index = head;
while (index != null)
{
if (index.data.Equals(n))
break;
index = index.next;
}
return index ?? null;
}
public string RemoveNode(object n) //remove node
{
if (head == null)
return "\r\nLink list is empty";
var node = FindNode(n);
if (node == null)
return "Node not found in Doubly Linked List\r\n";
if (node != head)
node.prev.next = node.next;
if (node.next != null)
node.next.prev = node.prev;
return "Node removed from Doubly Linked List\r\n";
}
答案 1 :(得分:0)
您是否尝试从仅包含一个元素(= head
)的列表中删除对象?你在那里得到一个错误,检查你的删除例程。您正在使用FindNode
(然后将返回您的头节点)收到一个元素,之后您正在调用current.prev.next = current.next;
,这将失败,因为您的headnode既没有previous
也没有{ {1}}节点。
我认为你的删除功能应该类似于:
next