Undo/Redo operation in a document file

 A document is collection of words which is sequence of characters. Once some wrong sentence/word/character is written, there should be provision to undo or redo operation.

Here undo and redo operation will happen on the content(character set) which is written at last. Means undo/redo will start from the last character added/modified to the document. So we need some mechanism to store characters of document, so that undo/redo operation could be performed on latest data(character added modified at last). Here last entered character is processed first. In computer science word to implement such mechanism Stack(Last In First Out LIFO) data structure is used. Below figure depicts the Undo and Redo operation mentioned in above example. Here File data is organized in two Stacks Undo Stack and Redo Stack.


1.a Example of Undo operation:

Algorithm for Undo operation is given below:

Undo(charCount)

{

For i=1 to charCount

{

If(Undo Stack is not empty)

{

Char c=removeItemFromTopOfUndoStack()

pushItemOnRedoStack(c)

}

}

formatAndShowContentOfUndoStackToFile()

}


1.b Example of Redo operation:


Algorithm for Redo operation is given below:

Redo(charCount)

{

For i=1 to charCount

{

If(Redo Stack is not empty)

{

Char c=removeItemFromTopOfRedoStack()

pushItemOnUndoStack(c)

}

}

formatAndShowContentOfUndoStackToFile()

}