C# - LinkedList - 如何删除指定节点后的所有节点? [英] C# - LinkedList - How to remove all nodes after specified node?

查看:36
本文介绍了C# - LinkedList - 如何删除指定节点后的所有节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing an undo/redo buffer with generic LinkedList.

在这种状态下:
[顶部]
state4(撤销)
state3(撤销)
state2 <-- 当前状态
状态1
[底部]

In this state:
[Top]
state4 (undone)
state3 (undone)
state2 <-- current state
state1
[bottom]

当我执行 Push 时,我想删除当前状态之后的所有状态,并推送新的状态.

When I do a Push, I would like to remove all states after the current one, and push the new one.

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

My current bypass is to do while (currentState != list.last), list.removeLast(); but it sucks

LinkedList 只支持 Remove、RemoveFirst &删除最后一个...

LinkedList just support Remove, RemoveFirst & removeLast...

我想要像 RemoveAllNodesAfter(LinkedListNode ...) 之类的东西?

I would like something like RemoveAllNodesAfter(LinkedListNode ...) ?

如何在不遍历所有节点的情况下很好地编码?也许有扩展?...

How can I code that nicely, without iterating throught all nodes ? Maybe with extensions ?...

推荐答案

我在标准 LinkedList<T> 中看不到任何可以让您执行此操作的内容.您可以查看 PowerCollectionsC5 集合 如果你愿意 - 或者只是滚动你自己的 LinkedList 类型.这是实现起来更简单的集合之一,特别是如果您可以及时"地添加功能.

I can't see anything in the standard LinkedList<T> which lets you do this. You could look in PowerCollections and the C5 collections if you want - or just roll your own LinkedList type. It's one of the simpler collections to implement, especially if you can add functionality in a "just in time" manner.

这篇关于C# - LinkedList - 如何删除指定节点后的所有节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆