撤消链接列表的功能 class
Undo function to the Linked List class
嘿,我是C++的初学者,我想在C++中的linkedList
class中添加一个Undo
函数,该函数反转列表中最后完成的操作例如 (append
, insertAt
, deleteAt
, clear
) ,关于撤消这些命令的最佳方法的任何想法?
我可以想到两种简单的方法来实现 "undo" 的魔力:将更改保存在队列中,直到你真的必须推送它们,或者将反向操作推送到你所做的每个更改撤消队列。
第一个看起来像这样:
User Action | Action Queue | actual list
---------------------------------------
nothing | empty | {}
push 5 | {push 5} | {}
push 2 | {push 2, push 5} | {}
print list | {} | {5, 2}
只有当用户执行您需要渗透更改的操作(如打印或获取)时,您才真正进行更改。然后撤消将只是从操作队列中弹出。
另一种选择是存储反向队列:
User Action | Reverse Queue | actual list
---------------------------------------
nothing | empty | {}
push 5 | {pop} | {5}
push 2 | {pop, pop} | {5, 2}
pop | {push 2, pop, pop} | {5}
undo | {pop, pop} | {5, 2}
在这里,用户所做的任何事情都会立即渗透,然后您将操作的反向推入反向队列。然后撤消是从反向队列中弹出并执行操作。
虽然第一个更容易实现,但这意味着您失去了在 "the changes are pushed."
之后撤消的能力
嘿,我是C++的初学者,我想在C++中的linkedList
class中添加一个Undo
函数,该函数反转列表中最后完成的操作例如 (append
, insertAt
, deleteAt
, clear
) ,关于撤消这些命令的最佳方法的任何想法?
我可以想到两种简单的方法来实现 "undo" 的魔力:将更改保存在队列中,直到你真的必须推送它们,或者将反向操作推送到你所做的每个更改撤消队列。
第一个看起来像这样:
User Action | Action Queue | actual list
---------------------------------------
nothing | empty | {}
push 5 | {push 5} | {}
push 2 | {push 2, push 5} | {}
print list | {} | {5, 2}
只有当用户执行您需要渗透更改的操作(如打印或获取)时,您才真正进行更改。然后撤消将只是从操作队列中弹出。
另一种选择是存储反向队列:
User Action | Reverse Queue | actual list
---------------------------------------
nothing | empty | {}
push 5 | {pop} | {5}
push 2 | {pop, pop} | {5, 2}
pop | {push 2, pop, pop} | {5}
undo | {pop, pop} | {5, 2}
在这里,用户所做的任何事情都会立即渗透,然后您将操作的反向推入反向队列。然后撤消是从反向队列中弹出并执行操作。
虽然第一个更容易实现,但这意味着您失去了在 "the changes are pushed."
之后撤消的能力