C#-LinkedList-如何删除指定节点后的所有节点?
我正在使用通用LinkedList实现一个撤消/重做缓冲区 在此状态下:C#-LinkedList-如何删除指定节点后的所有节点?,c#,linked-list,C#,Linked List,我正在使用通用LinkedList实现一个撤消/重做缓冲区 在此状态下: [顶部] 状态4(未完成) 状态3(未完成) state2我在标准LinkedList中看不到任何允许您执行此操作的内容。如果您愿意,您可以查看链接列表,或者直接滚动您自己的LinkedList类型。它是一种更容易实现的集合,特别是如果您可以以“即时”方式添加功能的话。链表(尤其是单链表)是最基本的集合结构之一。我敢肯定,您可能不费吹灰之力就可以实现它(并添加您需要的行为) 实际上,您并不需要一个collection类来管
[顶部]
状态4(未完成)
状态3(未完成)
state2我在标准
LinkedList
中看不到任何允许您执行此操作的内容。如果您愿意,您可以查看链接列表,或者直接滚动您自己的LinkedList
类型。它是一种更容易实现的集合,特别是如果您可以以“即时”方式添加功能的话。链表(尤其是单链表)是最基本的集合结构之一。我敢肯定,您可能不费吹灰之力就可以实现它(并添加您需要的行为)
实际上,您并不需要一个collection类来管理列表。您可以在不使用集合类的情况下管理节点
public class SingleLinkedListNode<T>
{
private readonly T value;
private SingleLinkedListNode<T> next;
public SingleLinkedListNode(T value, SingleLinkedListNode<T> next)
{
this.value = value;
}
public SingleLinkedListNode(T value, SingleLinkedListNode<T> next)
: this(value)
{
this.next = next;
}
public SingleLinkedListNode<T> Next
{
get { return next; }
set { next = value; }
}
public T Value
{
get { return value; }
}
}
公共类SingleLinkedListNode
{
私有只读T值;
私有SingleLinkedListNode下一步;
公共SingleLinkedListNode(T值,SingleLinkedListNode下一个)
{
这个值=值;
}
公共SingleLinkedListNode(T值,SingleLinkedListNode下一个)
:此(值)
{
this.next=next;
}
公共SingleLinkedListNode下一步
{
获取{return next;}
设置{next=value;}
}
公共价值
{
获取{返回值;}
}
}
但是,如果您对可能的实现感兴趣,这里有一个简单的SingleLinkedList实现
public class SingleLinkedList<T>
{
private SingleLinkedListNode<T> head;
private SingleLinkedListNode<T> tail;
public SingleLinkedListNode<T> Head
{
get { return head; }
set { head = value; }
}
public IEnumerable<SingleLinkedListNode<T>> Nodes
{
get
{
SingleLinkedListNode<T> current = head;
while (current != null)
{
yield return current;
current = current.Next;
}
}
}
public SingleLinkedListNode<T> AddToTail(T value)
{
if (head == null) return createNewHead(value);
if (tail == null) tail = findTail();
SingleLinkedListNode<T> newNode = new SingleLinkedListNode<T>(value, null);
tail.Next = newNode;
return newNode;
}
public SingleLinkedListNode<T> InsertAtHead(T value)
{
if (head == null) return createNewHead(value);
SingleLinkedListNode<T> oldHead = Head;
SingleLinkedListNode<T> newNode = new SingleLinkedListNode<T>(value, oldHead);
head = newNode;
return newNode;
}
public SingleLinkedListNode<T> InsertBefore(T value, SingleLinkedListNode<T> toInsertBefore)
{
if (head == null) throw new InvalidOperationException("you cannot insert on an empty list.");
if (head == toInsertBefore) return InsertAtHead(value);
SingleLinkedListNode<T> nodeBefore = findNodeBefore(toInsertBefore);
SingleLinkedListNode<T> toInsert = new SingleLinkedListNode<T>(value, toInsertBefore);
nodeBefore.Next = toInsert;
return toInsert;
}
public SingleLinkedListNode<T> AppendAfter(T value, SingleLinkedListNode<T> toAppendAfter)
{
SingleLinkedListNode<T> newNode = new SingleLinkedListNode<T>(value, toAppendAfter.Next);
toAppendAfter.Next = newNode;
return newNode;
}
public void TruncateBefore(SingleLinkedListNode<T> toTruncateBefore)
{
if (head == toTruncateBefore)
{
head = null;
tail = null;
return;
}
SingleLinkedListNode<T> nodeBefore = findNodeBefore(toTruncateBefore);
if (nodeBefore != null) nodeBefore.Next = null;
}
public void TruncateAfter(SingleLinkedListNode<T> toTruncateAfter)
{
toTruncateAfter.Next = null;
}
private SingleLinkedListNode<T> createNewHead(T value)
{
SingleLinkedListNode<T> newNode = new SingleLinkedListNode<T>(value, null);
head = newNode;
tail = newNode;
return newNode;
}
private SingleLinkedListNode<T> findTail()
{
if (head == null) return null;
SingleLinkedListNode<T> current = head;
while (current.Next != null)
{
current = current.Next;
}
return current;
}
private SingleLinkedListNode<T> findNodeBefore(SingleLinkedListNode<T> nodeToFindNodeBefore)
{
SingleLinkedListNode<T> current = head;
while (current != null)
{
if (current.Next != null && current.Next == nodeToFindNodeBefore) return current;
current = current.Next;
}
return null;
}
}
公共类单链接列表
{
私有单链接列表节点头;
私有单链接ListNode尾部;
公共单链接列表节点头
{
获取{返回头;}
设置{head=value;}
}
公共IEnumerable节点
{
得到
{
SingleLinkedListNode当前=头;
while(当前!=null)
{
产生回流电流;
当前=当前。下一步;
}
}
}
公共SingleLinkedListNode AddToTail(T值)
{
if(head==null)返回createNewHead(value);
如果(tail==null)tail=findTail();
SingleLinkedListNode newNode=新的SingleLinkedListNode(值,null);
tail.Next=newNode;
返回newNode;
}
公共SingleLinkedListNode插入符(T值)
{
if(head==null)返回createNewHead(value);
SingleLinkedListNode oldHead=头;
SingleLinkedListNode newNode=新的SingleLinkedListNode(值,oldHead);
头=新节点;
返回newNode;
}
公共SingleLinkedListNode InsertBefore(T值,SingleLinkedListNode到InsertBefore)
{
如果(head==null)抛出新的InvalidOperationException(“不能在空列表上插入”);
if(head==toInsertBefore)返回insertthead(值);
SingleLinkedListNode nodeBefore=findNodeBefore(toInsertBefore);
SingleLinkedListNode toInsert=新的SingleLinkedListNode(值,toInsertBefore);
nodeBefore.Next=toInsert;
返回到insert;
}
公共SingleLinkedListNode AppendAfter(T值,SingleLinkedListNode toAppendAfter)
{
SingleLinkedListNode newNode=新的SingleLinkedListNode(值,toAppendAfter.Next);
toAppendAfter.Next=newNode;
返回newNode;
}
public void TruncateBefore(SingleLinkedListNode到RuncateBefore)
{
if(head==totruncatefore)
{
head=null;
tail=null;
返回;
}
SingleLinkedListNode nodeBefore=findNodeBefore(toTruncateBefore);
如果(nodeBefore!=null)nodeBefore.Next=null;
}
public void TruncateAfter(SingleLinkedListNode到RuncateAfter)
{
totruncatefafter.Next=null;
}
私有SingleLinkedListNode createNewHead(T值)
{
SingleLinkedListNode newNode=新的SingleLinkedListNode(值,null);
头=新节点;
tail=newNode;
返回newNode;
}
私有SingleLinkedListNode findTail()
{
if(head==null)返回null;
SingleLinkedListNode当前=头;
while(current.Next!=null)
{
当前=当前。下一步;
}
回流;
}
专用SingleLinkedListNode findNodeBefore(SingleLinkedListNode nodeToFindNodeBefore)
{
SingleLinkedListNode当前=头;
while(当前!=null)
{
if(current.Next!=null&¤t.Next==nodeToFindNodeBefore)返回当前值;
当前=当前。下一步;
}
返回null;
}
}
现在您可以执行以下操作:
public static void Main(string[] args)
{
SingleLinkedList<string> list = new SingleLinkedList<string>();
list.InsertAtHead("state4");
list.AddToTail("state3");
list.AddToTail("state2");
list.AddToTail("state1");
SingleLinkedListNode<string> current = null;
foreach (SingleLinkedListNode<string> node in list.Nodes)
{
if (node.Value != "state2") continue;
current = node;
break;
}
if (current != null) list.TruncateAfter(current);
}
while (currentNode.Next != null)
list.Remove(currentNode.Next);
publicstaticvoidmain(字符串[]args)
{
SingleLinkedList=新建SingleLinkedList();
列表。插入日期(“州4”);
列表。AddToTail(“state3”);
列表。AddToTail(“state2”);
列表。AddToTail(“state1”);
SingleLinkedListNode当前值=null;
foreach(list.Nodes中的SingleLinkedListNode节点)
{
如果(node.Value!=“state2”)继续;
电流=节点;
打破
}
if(current!=null)list.TruncateAfter(current);
}
这取决于你的情况,没有比这更好的了:
public static void Main(string[] args)
{
SingleLinkedListNode<string> first =
new SingleLinkedListNode<string>("state4");
first.Next = new SingleLinkedListNode<string>("state3");
SingleLinkedListNode<string> current = first.Next;
current.Next = new SingleLinkedListNode<string>("state2");
current = current.Next;
current.Next = new SingleLinkedListNode<string>("state1");
current = first;
while (current != null)
{
if (current.Value != "state2") continue;
current.Next = null;
current = current.Next;
break;
}
}
publicstaticvoidmain(字符串[]args)
{
SingleLinkedListNode优先=
新的SingleLinkedListNode(“state4”);
first.Next=新的SingleLinkedListNode(“state3”);
SingleLinkedListNode当前=第一个。下一个;
current.Next=新的SingleLinkedListNode(“state2”);
当前=当前。下一步;
current.Next=新的SingleLinkedListNode(“state1”);
电流=第一;
while(当前!=null)
{
如果(current.Value!=“state2”)继续;
current.Next=null;
当前=当前。下一步;
打破
}
}
这完全消除了对collection类的需要。如果我自己实现它,我会选择一种不同的方式来实现它 而不是
.RemoveAllNodesAfter(nod
while (currentNode.Next != null)
list.Remove(currentNode.Next);
public class LinkedListNode<T>
{
public LinkedList<T> Parent { get; set; }
public T Value { get; set; }
public LinkedListNode<T> Next { get; set; }
public LinkedListNode<T> Previous { get; set; }
}
public class LinkedList<T> : IEnumerable<T>
{
public LinkedListNode<T> Last { get; private set; }
public LinkedListNode<T> AddLast(T value)
{
Last = (Last == null)
? new LinkedListNode<T> { Previous = null }
: Last.Next = new LinkedListNode<T> { Previous = Last };
Last.Parent = this;
Last.Value = value;
Last.Next = null;
return Last;
}
public void SevereAt(LinkedListNode<T> node)
{
if (node.Parent != this)
throw new ArgumentException("Can't severe node that isn't from the same parent list.");
node.Next.Previous = null;
node.Next = null;
Last = node;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
var walk = Last;
while (walk != null) {
yield return walk.Value;
walk = walk.Previous;
}
}
}
if(this.ptr != null && this.ObjectName != null)
{
LinkedListNode<ObjectType> it = ObjectName.Last;
for (; it != this.ptr; it = it.Previous)
{
this.m_ObjectName.Remove(it);
}
}
public static class Extensions
{
public static void RemoveAllBefore<T>(this LinkedListNode<T> node)
{
while (node.Previous != null) node.List.Remove(node.Previous);
}
public static void RemoveAllAfter<T>(this LinkedListNode<T> node)
{
while (node.Next != null) node.List.Remove(node.Previous);
}
}
void Main()
{
//create linked list and fill it up with some values
LinkedList<int> list = new LinkedList<int>();
for(int i=0;i<10;i++) list.AddLast(i);
//pick some node from the list (here it is node with value 3)
LinkedListNode<int> node = list.First.Next.Next.Next;
//now for the trick
node.RemoveAllBefore();
//or
node.RemoveAllAfter();
}