6

我正在使用通用 LinkedList 实现撤消/重做缓冲区。

在这种状态下:
[Top] state4 (undo
)
state3 (undone)
state2 <-- 当前状态
state1
[bottom]

当我进行推送时,我想删除当前状态之后的所有状态,然后推送新状态。

我目前的旁路是要做的,while (currentState != list.last), list.removeLast();但它很烂

LinkedList 只支持 Remove、RemoveFirst 和 removeLast...

我想要像 RemoveAllNodesAfter(LinkedListNode ...) 这样的东西?

我怎样才能很好地编码,而不遍历所有节点?也许带有扩展名?...

4

7 回答 7

6

我在标准中看不到任何LinkedList<T>可以让你这样做的东西。如果需要,您可以查看PowerCollectionsC5 集合- 或者只是滚动您自己的LinkedList类型。它是实现起来更简单的集合之一,特别是如果您可以“及时”地添加功能。

于 2009-02-24T15:15:43.800 回答
5

如果我要自己实现它,我会选择一种不同的方式来实现它。

.RemoveAllNodesAfter(node)我会选择创建一个方法来代替该方法,该.SplitAfter(node)方法返回一个新的链表,该链表从 after 的下一个节点开始node。这将成为一个更方便的工具,而不仅仅是能够砍掉尾巴。如果您想要您的RemoveAllNodesAfter方法,它只需要在SplitAfter内部调用该方法并丢弃结果。

天真的实现:

public LinkedList<T> SplitAfter(Node node)
{
    Node nextNode = node.Next;

    // break the chain
    node.Next = null;
    nextNode.Previous = null;

    return new LinkedList<T>(nextNode);
}

public void RemoveAllNodesAfter(Node node)
{
    SplitAfter(node);
}
于 2009-02-24T15:35:09.417 回答
4

链表(尤其是单链表)是最基本的集合结构之一。我确信您可以毫不费力地实现它(并添加您需要的行为)。

实际上,您实际上并不需要集合类来管理列表。您可以在没有集合类的情况下管理节点。

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; }
    }
}

但是,如果您对可能的实现感兴趣,这里有一个简单的 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;
    }
}

现在你可以这样做:

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);
}

事情取决于你的情况,它并不比这更好:

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;
    }
}

这完全消除了对集合类的需要。

于 2009-02-24T15:26:04.323 回答
3

或者,您可以这样做:

while (currentNode.Next != null)
    list.Remove(currentNode.Next);

实际上,链表是一种相当简单的数据结构,尤其是在托管代码中实现(阅读:没有内存管理麻烦)。

这是我破解的一个支持足够功能(阅读:YAGNI)来支持您的撤消/重做操作的功能:

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;
        }
    }

}

然后你可以SevereAt在你的代码中使用这个方法来简单地“剪切”链表。

于 2009-02-24T16:12:09.173 回答
0

想到的第一个想法是设置Node.Next.Previous = null(如果它是一个双向链表),然后Node.Next = null.

不幸的是,因为LinkedListNode<T>.NextLinkedListNode<T>.Previous是链接列表的 .NET 实现中的只读属性,我认为您可能必须实现自己的结构才能实现此功能。

但正如其他人所说,这应该很容易。如果您只是在 Google 中搜索链表 C#,则可以使用大量资源作为起点。

于 2009-02-24T15:47:00.793 回答
0
if(this.ptr != null && this.ObjectName != null)
{
    LinkedListNode<ObjectType> it = ObjectName.Last;
                for (; it != this.ptr; it = it.Previous) 
                {
                    this.m_ObjectName.Remove(it);
                }
}

this.ptr仅供LinkedListNode<ObjectType>参考

this.ptr是指向您当前所在节点的指针,我假设您要删除它右侧的所有内容。

不要复制你的结构,这是有史以来最糟糕的主意。它是一个完整的内存猪,结构可能非常大。除非绝对必要,否则复制对象不是好的编程习惯。尝试进行就地操作。

于 2011-12-28T16:28:24.543 回答
0

我为“删除特定节点之前的所有节点”和“删除特定节点之后的所有节点”做了两种扩展方法。但是,这些扩展方法是 LinkedListNode 的扩展,而不是 LinkedList 本身,只是为了方便:

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();
}

好吧,这不是最有效的方法,如果您发现自己在大型列表上或经常调用此方法,那么此处描述的其他方法可能更合适(例如编写您自己的链表类,它允许按照其他答案中的描述进行拆分)但是如果它只是偶尔的“在这里和那里删除节点”,而不是简单且非常直观。

于 2013-10-28T11:53:53.463 回答