Undo and redo with two stacks

A simple text editor implements undo and redo with two stacks. Every typing action appends one character to the end of the document and pushes that character onto the undo stack. The editor follows three rules:

  • A new typing action pushes onto the undo stack and empties the redo stack.
  • Undo pops the top of the undo stack, removes that character from the document, and pushes it onto the redo stack. Undo does nothing if the undo stack is empty.
  • Redo pops the top of the redo stack, appends that character to the document again, and pushes it onto the undo stack. Redo does nothing if the redo stack is empty.

Starting from an empty document with both stacks empty, the user performs this sequence: type a, type b, type c, undo, undo, type d, undo, redo, redo.


After the whole sequence, give the number of elements in the undo stack and in the redo stack as whole numbers, the single character at the top of the undo stack, and the full text of the document.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Stacks Questions: