C# linkedlist如何获取最后一个元素之前的元素
我尝试在我的 Windows 窗体应用程序中实现重做撤消。
我建立了一个链表,列表中的每个条目都是一个类,用于保存表单中所有元素的状态。
每次单击保存按钮,都会将表单元素的最后状态插入到此列表中。
当用户单击撤消按钮时,我想获取列表的输入(最后一个之前的一个) 并加载它。
我不知道在链表中的元素之前获取这个的简单方法是什么?
我的代码如下所示:
public class SaveState {
public int comboBox1;
public int comboBox2;
..........
public SaveState() {
.......
}
}
LinkedList<SaveState> RedoUndo = new LinkedList<SaveState>();
# in save function
var this_state = new SaveState();
this_state = getAllState();
RedoUndo.AddLast(this_state);
# when click undo
var cur_state = new SaveState();
# this lines dont work !!!!!!!!!
int get = RedoUndo.Count - 1;
cur_state = RedoUndo.Find(get);
setAllState(cur_state);
i try to implement a redo undo in my windows form application.
i build a linkedlist , every entery of the list is a class that save the state of all the elemnts in the form.
every click on the save button , insert to this list the last state of the form elements.
when the user click on undo button i want to get the entery of the list (one before the last)
and load it.
i dont know what is the simple way to get this one before elemnts from the linked list ?
my code like look like:
public class SaveState {
public int comboBox1;
public int comboBox2;
..........
public SaveState() {
.......
}
}
LinkedList<SaveState> RedoUndo = new LinkedList<SaveState>();
# in save function
var this_state = new SaveState();
this_state = getAllState();
RedoUndo.AddLast(this_state);
# when click undo
var cur_state = new SaveState();
# this lines dont work !!!!!!!!!
int get = RedoUndo.Count - 1;
cur_state = RedoUndo.Find(get);
setAllState(cur_state);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过
LinkedList.Last
和倒数第二个节点通过
LinkedListNode.Previous
请注意,这是一个
LinkedListNode;
并且您需要使用LinkedListNode< ;T>.Value
属性获取T
的底层实例。当然,您应该注意检查
list
是否不为 null,并且list.Last
是否不为 null(在空列表的情况下),并且>list.Last.Previous
不为空(在单元素列表的情况下)。You can get the last node via
LinkedList<T>.Last
and the penultimate node via
LinkedListNode<T>.Previous
Note that this is a
LinkedListNode<T>
and you need to use theLinkedListNode<T>.Value
property get the underlying instance ofT
.Of course, you should take care to check that
list
is not null, andlist.Last
is not null (in the case of an empty list), and thatlist.Last.Previous
is not null (in the case of a single-element list).@Haim,您可能想查看 Krill Osenkov 的撤消框架。它使撤消/重做变得非常容易。
@Haim, you may want to check out Krill Osenkov's Undo Framework. It makes undo/redo very easy.