What Is a Stack in Computer Science and How Does Undo Use It?

Type cat into a blank document, change it to cats, and then add a period. Press Undo once and you get cats, not a blank page. If you are asking “what is a stack in computer science,” that first Undo shows the key idea: the newest item added is the first one you can take out.
What Is a Stack in Computer Science?
A stack is a data structure that holds items and lets you add or remove them at one end, called the top. Think of a pile of index cards. When you put a card on top, it covers the previous card. When you lift off the top card, the previous one becomes visible again. The rule is called last in, first out, or LIFO. Here, “last” means the last item added that is still in the stack.
The three operations differ in what they do to the top. Push adds an item and makes it the new top. Pop removes the top item and gives it back to the caller. Peek gives you the top item without removing it. A stack does not sort items or search for the most recent one. The order of the pushes, minus any items already popped, determines what is on top.
Three Edits Build the History
Imagine a simple editor that records three separate actions. It records typing cat as one action, adding s as a second, and adding the period as a third. Each time the editor applies an edit, it pushes a record with enough information to reverse that edit later. In a real implementation, an insertion record might include the text and its position. In this example, all three insertions happen at the end, so short labels are enough to trace the history.
The rightmost item in each bracketed list is the top. The letters name the records, not individual characters in the document: A means “insert cat,” B means “append s,” and C means “append a period.”
| Step | Document after the step | Stack after the step |
|---|---|---|
| Start with an empty history | Blank | [] |
| Apply A, then push A | cat | [A] |
| Apply B, then push B | cats | [A, B] |
| Apply C, then push C | cats. | [A, B, C] |
| Peek and read C | cats. | [A, B, C] |
| Pop C and reverse it | cats | [A, B] |
| Pop B and reverse it | cat | [A] |
| Pop A and reverse it | Blank | [] |
Look at the peek row: both the document and the stack stay exactly as they were. An editor could use peek to show what the next Undo would reverse. If you peek twice without doing anything else, you still get C both times. The next pop gets C too, but now C is gone from the stack and B is exposed.
Why Undo Reverses the Newest Edit First
Pop by itself does not change the document. It just gives the editor an action record and removes that record from the history. To finish Undo, the editor uses the record to perform the opposite edit. For C, that means removing the period. The document becomes cats, exactly as it was before C. Reversing B next restores cat, the state before B.
Taking the oldest edit first would not have the same effect. If you removed the original cat from cats. while leaving the newer edits in place, you would get s. That text never appeared during the original sequence. A regular Undo command is meant to take one step back through the states you actually visited, not to pull out an arbitrary earlier change.
Some edits also depend on previous ones. Say you type cat and then replace the c with b to make bat. To reverse the insertion as originally recorded, first reverse the replacement so the document contains cat again. The stack puts the replacement on top, so the editor gets the records in the order it needs.
An Empty History and a New Edit
Once the last record has been popped, there is no top item to peek at or pop. In this editor, pressing Undo with an empty stack should leave the document unchanged. The editor can check for an empty history before trying to pop, or it can disable the Undo command when there is nothing to reverse.
Now return to the moment just after one Undo, when the document says cats and the stack is [A, B]. Instead of pressing Undo again, type an exclamation point. Call that action D. The document becomes cats! and the editor pushes D, giving [A, B, D]. The next Undo reverses D, not C. C was already popped, and the latest record still in the history is the one the editor just added.
Practice With a Different Edit
Start with a document that already contains dog and an empty undo history. Record each of these as a separate action: delete the final g, append t, then append !. Draw the stack after each edit. Peek once, then carry out three Undo commands. Write both the document and the stack after each command, using the same rightmost-top convention as in the table.
After the peek, the document still reads dot!. The three Undo commands should produce dot, then do, then dog. The last step is easy to miss: reversing a deletion means putting text back. The record for that first action needs to tell the editor what was deleted and where it belonged.
If your stack and document get out of sync on paper, try the method in our guide to tracing code by hand: keep separate columns for the changing values. For more exercises, use the ICSC training and practice problems.
Frequently asked questions
Does peek remove anything from the stack?
No. Peek reads the top item and leaves it in place. Pop reads and removes it.
Does popping an edit automatically undo it?
No. Popping retrieves and removes the edit record. The editor still has to use the record to reverse the change.
Why not undo the oldest edit first?
Later edits may depend on earlier ones. Undoing the newest edit first restores the document to the state it was in just before that edit.



