Good collection to implement Undo / Redo?

I read about undo / redo methods, I understand how this should be implemented (I found it intuitively).

However, I’m thinking of a collection that should be used as a story,

Many people use the stack, but C # stack is implemented as an array, and this is the problem: If I use a "limited" history (for example, for 2000 commands) when the limit is reached, I have no way to remove elements from the end of the stack, and if I find The way to do this, I have to move all the elements of the array (and this is for every moment the command is executed).

LinkedList looks good, but it spends a lot of memory.

My last option is a custom implementation of a linked list, SingleLinkedList. A node of this list consists of the Value property and the NextNode pointer property, so I use double memory for each element (but nothing more, except when I use things smaller than "sizeof (void *)").

I also keep a pointer to the first item and a pointer to the last item in the collection.

I can easily add commands to the story and move them to the replay in this way, however I cannot create a “limited” story because RemoveLast is not allowed (I need to go through the entire collection to remove the last item).

So my question is: Should I use LinkedList or my own SingleLinkedList?

Update 1:

, , , , , " " ", / (obviusly " ++ ", ).

, singlelinkedlist , , Photoshop, "".

, , , 8 . LinkedList.

- , , 60000 ( , ), (4 * 60000) singlelinkedlist.

, , LinkedList, , , , ?

2:

@Akash Kava , , , LinkedList . Stack , , , ( , , ).

( , ) , ( - O (1)) . , , , singlelinkedlist ( ), node node.

, LinkedList, Previous. 2 "Stack" ( ), 3 , , ( , singlelinkedlist 2 , 3- ).

, "" -.

, 60000 1 , 5 , .

, /, LinkedList, SingleLinkedList.

+5
6

LinkedList , , . / . , , LinkedList, (), .

; , , , , , . ; , , .

+3

, LinkedList. , ? 16 ?

+2

LinkedList , / , .

Stack<Action> undoStack;
Stack<Action> redoStack;

/

undoStack.Count > 0 (We can undo)
redoStack.Count > 0 (We can redo)

redoStack, - , VS, -, .

redoStack.Clear() <-- important step
undoStack.Push(action);

Action action = undoStack.Pop();
redoStack.Push(action); <-- necessary...

Action action = redoStack.Pop();
undoStack.Push(action);

, .

, , undo redo stack, . , , ( ), , .

+2

, , - , 1 addictional- Node , , , 60000 8 node. 480 , , .

, - SingleLinkedList, "" ( SingleLinkedList).

, LinkedList.

+1

startindex endindex. , startIndex endIndex, (2) .

0

" ", , , , , 2000 .

. . , ArrayList, , , , , , . , , ( , , ).

, , - , , , , . . , ( , - ), , , .

, , , , , ( , ). / , , . , , , "". (: ) - , .

, , , , . , node . . - :

struct Node
{
     Command commands[n];
     Node next;
     Node prev;
     int first;
     int last;
}

, last node. last == first, node . , last. last == n, node . (.. ), first node. first == last, node node . , - - , node, ( n undo, , n a large number, for example 512, reducing the overhead of a linked list to 1/512 or ~ 1.95%) and improving the locality of links.

0
source

All Articles