我实现了2个方法,即RemoveAfter和RemoveBefore,它们将在指定节点之前/之后删除节点。方法删除后工作正常,但我不知道为什么RemoveBefore给我错误,因为前面将始终为null。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question7
{
public class LinkedListNode
{
public LinkedListNode next = null;
public int data;
public LinkedListNode(int d) { data = d; }
public void AppendToTail(int d)
{
LinkedListNode end = new LinkedListNode(d);
LinkedListNode n = this;
while (n.next != null) { n = n.next; }
n.next = end;
}
}
class Program
{
#region DeleteAFter
public static bool DeleteAfter(LinkedListNode n)
{
if (n == null || n.next == null)
{
return false; // Failure
}
LinkedListNode next = n.next;
n.next = next.next;
return true;
}
#endregion
static LinkedListNode front;
#region DeleteBefore
public static bool DeleteBefore(LinkedListNode n)
{
LinkedListNode prev = null;
LinkedListNode curr = front;
prev = curr;
while(curr.data != n.data)
{
prev = curr;
curr = curr.next;
}
prev.data = curr.data;
prev.next = curr.next;
return true;
}
#endregion
static void PrintList(LinkedListNode list)
{
while (list != null)
{
Console.Write(list.data + "->");
list = list.next;
}
Console.WriteLine("null");
}
static void Main(String[] args)
{
LinkedListNode myList = new LinkedListNode(5);
myList.AppendToTail(6);
myList.AppendToTail(7);
myList.AppendToTail(8);
// Now the list is 5->6->7->8
Console.Write("Before deletion: ");
PrintList(myList); // 5->6->7->8->null
LinkedListNode deletedNode = myList;
int val = 7;
while (deletedNode.data != val)
{
deletedNode = deletedNode.next;
}
Console.Write("After deletion: ");
if (DeleteBefore(deletedNode))
PrintList(myList);
Console.Read();
}
}
}
答案 0 :(得分:0)
前变量中的值始终为null。稍后您将curr分配给front并尝试访问无效的curr.data。您需要实现DeleteBefore函数,其中curr =' this'它应该从main
调用myList.DeleteBefore(deletedNode) public static bool DeleteBefore(LinkedListNode n)
{
LinkedListNode prev = null;
LinkedListNode curr = this;
while(curr != null)
{
if(curr.next.data == n.data)
//if the next node has the same data as n,
// then curr node has to be deleted
{
prev.next = curr.next;
return true;
}
prev = curr;
curr = curr.next;
}
return false;
}
在main中,您可以按如下方式调用此函数:
myList.DeleteBefore(deletedNode);
答案 1 :(得分:0)
您的front
变量始终为null。如果它始终为null,那么它的用途是什么?并且你正在与它进行比较,所以你得到null exception
。
您可以在不使用前变量的情况下完成此操作。
public static LinkedListNode DeleteBefore(LinkedListNode n,LinkedListNode currentList)
{
LinkedListNode prev = currentList;
while (prev.data!=n.data)
{
prev = prev.next;
}
currentList = prev;
return currentList;
}
LinkedListNode myList = new LinkedListNode(5);
myList.AppendToTail(6);
myList.AppendToTail(7);
myList.AppendToTail(8);
// Now the list is 5->6->7->8
Console.Write("Before deletion: ");
PrintList(myList); // 5->6->7->8->null
LinkedListNode deletedNode = myList;
int val = 7;
while (deletedNode.data != val)
{
deletedNode = deletedNode.next;
}
Console.Write("After deletion: ");
PrintList(DeleteBefore(deletedNode,myList));
}
答案 2 :(得分:0)
因为您没有从节点到其上一个节点的访问权限,所以在DeleteBefore
上,您必须从根目录再次遍历列表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Question7
{
public class LinkedListNode
{
public LinkedListNode next = null;
public int data;
public LinkedListNode(int d) { data = d; }
public void AppendToTail(int d)
{
LinkedListNode end = new LinkedListNode(d);
LinkedListNode n = this;
while (n.next != null) { n = n.next; }
n.next = end;
}
}
class Program
{
#region DeleteAFter
public static bool DeleteAfter(LinkedListNode n)
{
if (n == null || n.next == null)
{
return false; // Failure
}
LinkedListNode next = n.next;
n.next = next.next;
return true;
}
#endregion
static LinkedListNode front;
#region DeleteBefore
public static bool DeleteBefore(LinkedListNode root, LinkedListNode n)
{
LinkedListNode prev = root;
LinkedListNode curr = n;
// we're iterating from the root, looking if the next node is the one we're looking for.
while (prev.next != null && curr.data != prev.next.data)
{
prev = prev.next;
}
// if we found it
if (prev.next != null && curr.data == prev.next.data)
{
// we're rewiring our list. we can also prev.next = curr.next;
prev.next = prev.next.next;
return true;
}
return false;
}
#endregion
static void PrintList(LinkedListNode list)
{
while (list != null)
{
Console.Write(list.data + "->");
list = list.next;
}
Console.WriteLine("null");
}
static void Main(String[] args)
{
LinkedListNode myList = new LinkedListNode(5);
myList.AppendToTail(6);
myList.AppendToTail(7);
myList.AppendToTail(8);
// Now the list is 5->6->7->8
Console.Write("Before deletion: ");
PrintList(myList); // 5->6->7->8->null
LinkedListNode deletedNode = myList;
int val = 7;
while (deletedNode.data != val)
{
deletedNode = deletedNode.next;
}
Console.Write("After deletion: ");
if (DeleteBefore(myList, deletedNode))
PrintList(myList);
Console.Read();
}
}
}