使用Memento撤消/重做:堆栈,队列还是仅链接列表?

ser*_*hio 5 .net collections design-patterns

实现Memento模式时的最佳效果(撤消/重做)

在巫婆收藏保持纪念品?

基本上,我需要这个(c = change,u = undo,r = redo):

                  0
                  *c
            -1    0
                  *c
      -2    -1    0
                  *c
-3    -2    -1    0
                  <u
      -2    -1    0    1
                  *c
-3    -2    -1    0
Run Code Online (Sandbox Code Playgroud)

变种:

  • LinkedList - 原则上可能,可能没有优化.
  • 队列 - 不适合这项任务,IMO.
  • 堆栈 - 不适用于撤消和重做;
  • 双栈 - 可能是最佳的,但无法控制撤消最大大小.

ser*_*hio 1

最后我用了LinkedList

Public Sub Add(ByVal item As T)
  If _list.Count > 0 Then
    If Me.IsFull Then
      ' we forgot (delete) the oldest state '
      _list.RemoveFirst()
    End If
    ' remove all the following the current items objects '
    Dim lastNode As LinkedListNode(Of T) = _list.Last
    While Not Object.ReferenceEquals(_currentNode, lastNode)
      _list.RemoveLast()
      lastNode = _list.Last
    End While
  End If

  ' add the new item and point current to it '
  _currentNode = _list.AddLast(item)
End Sub
Run Code Online (Sandbox Code Playgroud)