Computer Science in Everyday Life
Undo starts with what just happened. A simple print queue starts with whoever has been waiting longest.
Those two rules explain the difference between a stack and a queue. A stack takes the newest item first, which lets an editor reverse your latest change. A queue takes the oldest waiting item first, which lets a printer serve jobs in arrival order. Both can hold the same items. The difference is which item gets picked next.
- C · next out
- B
- A
- A · next out
- B
- C
Start by asking which item should come out next
Set three books on a table, one on top of the other. If you keep adding and removing books at the top, the last one you put down is the first you take back. That’s how a stack behaves. Now imagine three people joining a checkout line. New arrivals stand behind the people already waiting. Whoever joined first reaches the cashier first. That’s how a simple queue behaves.
In programming, adding to a stack is called pushing, and removing from it is called popping. Both operations use the same end, called the top. Adding to a queue is called enqueueing, and removing from it is called dequeueing. Those operations use opposite ends. Both structures can also let a program look at the next item without removing it. That operation is often called peeking.
The order stays meaningful when additions and removals are mixed. Suppose you add A, then B, take one item out, and add C. The stack gives you B on the first removal and C on the next. The queue gives you A on the first removal and B on the next, because B has been waiting longer than C. If the collection is empty, there’s nothing to remove. An app might respond by disabling Undo or putting the printer into an idle state until work arrives.
How a stack lets an editor retrace your changes
Suppose you type a title, make it bold, and then change its color. An editor can record each change as an entry in an undo history. The color change goes on top because it happened last. Press Undo once and the color reverts while the title stays bold. Press it again and the bold formatting goes away. Press it a third time and the title disappears. The editor works back through the changes in reverse order.
That order helps keep each reversal understandable. The bold formatting and color were applied to a title that already existed. If an ordinary Undo button removed the title first, the two later changes would refer to text that was gone. Reversing the latest change first usually returns the document to a state it actually occupied while you were working.
What the editor stores depends on how it’s built. It might keep a record of an action and the information needed to reverse it. A deletion would need a copy of the removed text and its location so it could be put back. A color change would need the previous color. Another design might save earlier document states or use a mixture of snapshots and change records. The visible undo order can be the same in each case.
Why redo can use a second stack
Undo moves backward through a history. Redo brings back the most recent change you took out. One way to support both is to keep an undo stack and a redo stack. Each time you undo an action, the editor reverses it and moves the entry from the undo stack to the redo stack. When you redo, the editor reapplies the action and returns its entry to the undo stack.
Use the title example again. First undo the color, then undo the bold formatting. Bold is now on top of the redo stack because it was the last action reversed. Press Redo once and the title becomes bold again. Press it twice and the color comes back. Moving entries between the two stacks preserves the sequence, even though you were moving backward when you added them to the redo side.
Now undo the color and type a subtitle. A conventional linear history usually clears the redo stack when a new edit starts from that earlier point. You’ve chosen a different continuation for the document. Some tools can preserve more than one history branch, but a basic two-stack design keeps only the forward path associated with the current sequence of edits.
Why undo history can have a limit
Keeping earlier changes takes memory. Restoring a deleted word may require little information. Restoring a deleted image, layer, or large part of a document can require much more. An app can set a limit on how many actions it remembers or how much memory its history uses. Once older entries are discarded, you can still undo recent work, but the history can no longer take you all the way back to the start of the session.
Saved files and version histories give you another way to recover work. Saving records a document state for later use. Undo history usually serves the immediate editing session, and different apps make different choices about whether to preserve it across a close and reopen. If you’re trying to recover something from yesterday, checking for a saved version may be more productive than repeatedly pressing Undo.
How a queue keeps print jobs in order
Picture a shared office printer that’s already producing a report. While it’s busy, three people submit an agenda, an invoice, and a flyer, in that order. In a simple first-in, first-out system, those jobs wait in that same sequence. The agenda is next after the report, even if the flyer is only one page and arrived just before the printer became free.
A printing system generally has a service that accepts jobs and manages their delivery to the printer. This is commonly called a spooler. It can hold job data and settings while the printer is busy or temporarily unavailable. That’s why you can normally go back to editing a document after submitting it. The application hands over the print job, and the printing system takes responsibility for processing it.
It helps to distinguish the job in progress from the jobs still waiting. A basic scheduler chooses the next waiting job when the printer is ready to accept it. It generally doesn’t interrupt a document halfway through to interleave pages from a later job. The print window may show an active job beside several waiting ones, so the first line you see can be the job already printing rather than the one waiting to start next.
A long job can make a short one wait
If someone submits a 200-page manual ahead of a one-page receipt, first-in, first-out gives the manual the earlier turn. That’s predictable. It also means a small job may wait a long time behind a large one. A shared office might solve that by making another printer available, setting a policy for large jobs, or allowing an administrator to change priority when needed.
A strict queue is the simplest place to begin, and real print systems can add exceptions. A job might be held until someone releases it at the printer, paused while the correct paper is loaded, or cancelled before it starts. Depending on the scheduler and its settings, other eligible jobs may be able to pass a held job. Priority and readiness can therefore affect the visible order. In an ordinary FIFO queue with no special rules, arrival order determines who goes next.
Run the same sequence through both structures
Imagine feeding the same sequence of events into two empty collections. The stack column lists items from top to bottom. The queue column lists them from front to back. In both columns, the first item shown is the next one available to remove.
| Event | Stack result | Queue result |
|---|---|---|
| Add A | A | A |
| Add B | B, A | A, B |
| Remove one item | Returns B. A remains. | Returns A. B remains. |
| Add C | C, A | B, C |
| Remove one item | Returns C. A remains. | Returns B. C remains. |
By the final row, the stack still holds A. Each newer item took precedence and went out first. The queue holds C, the newest arrival, because A and B received their turns before it. If another item arrives before the next removal, the stack will choose that new item. The queue will still choose C.
This shows why using a stack for ordinary print scheduling would change the experience. If new jobs kept arriving, an old job could remain at the bottom for a long time. Using a queue for standard Undo would surprise you for the opposite reason. Pressing it after a formatting mistake could reverse an edit you made much earlier, leaving the change you wanted to fix in place.
Choose the rule that matches what users expect
When you’re building a program, start with a sentence that explains who or what should go next. “Reverse the most recent completed edit” points to a stack. “Process the oldest waiting request” points to a queue. “Choose the eligible request with the highest priority” calls for a scheduling rule that can take priority into account. That sentence also gives you a simple way to test the design.
- Choose a useful unit of work. For an editor, decide whether an undo entry is a keystroke, a word, or a larger command. For a printer, the usual entry is a job with its settings, even if the document contains many pages.
- Work out what happens before and after removal. An editor has to restore the document correctly before treating the change as undone. A printing system has to distinguish work it has accepted, work it has handed over, and work it knows has completed. Where those boundaries sit affects what the user sees.
- Test the ordinary sequence with three items. Add A, B, and C. Remove one. Then add D and remove again. A stack returns C and D. A queue returns A and B. Mixing adds and removals makes it easier to spot an accidental reversal than checking the collection only after filling it.
- Decide what happens in the awkward cases. What should an editor do after an undo followed by a new edit? What happens to a print job if the printer goes offline? Try the empty state as well. A good implementation makes those answers predictable for the person using it.
The storage can vary while the order stays the same
A stack often fits naturally in a list that can efficiently add and remove entries at the end. A queue needs efficient access to the front and back. If a program keeps a queue in a plain array and shifts every remaining item forward after each removal, large queues can make that repeated copying expensive. A deque or circular buffer can avoid moving the entire collection for each item.
A circular buffer makes the underlying idea concrete. Imagine a row of numbered slots and a marker pointing to the oldest occupied slot. When the program removes that item, it advances the marker. When it adds an item, it writes at the next available position. Once a marker reaches the end, it can wrap back to the beginning and use space that earlier removals freed. The memory layout changes as the markers move, while the oldest waiting item still leaves first.
In practice, the order people observe can also depend on how many workers are doing the job. A single printer usually completes one job before another. If a print service distributes work to two printers, it could assign jobs in arrival order and still finish a short job before a longer one that started earlier. When comparing systems, check whether the queue promises an order for accepting work, starting it, or finishing it. Those are separate events.
Common questions about stacks, queues, and everyday software
Can you undo a specific change from the middle of the history?
Some applications support selective undo or let you edit a history entry in another way. That calls for extra logic because later changes may depend on the one you’re trying to remove. As an example, undoing the insertion of a paragraph affects edits that were made inside it. A standard Undo command avoids that issue by reversing actions from the top of the history.
Is a print queue the same thing as a printer’s memory?
A print queue is the waiting work managed by a device, print server, or computer. The printer’s memory may hold data for the work it has already received. When you look at a job list on your computer, some entries may still be waiting to reach the printer, while another is already being processed by it.
Can a queue let someone cancel a job in the middle?
Yes. A print service can keep job identifiers and support cancellation as a separate operation. Once the cancelled job is removed or marked to be skipped, the remaining jobs can still be handled in their original arrival order. Whether a partly printed job can be stopped immediately depends on what the printer has already received and how it handles cancellation.
Is a priority queue just a faster regular queue?
A priority queue answers a different question. It selects an item according to its priority. A system can use arrival time to break ties between jobs of equal priority. The name describes the selection rule rather than promising shorter waits for every job.
What does deque mean?
A deque is a double-ended queue. It supports adding and removing items at either end. Use the same end for both operations and it can behave like a stack. Add at one end and remove from the other and it can behave like a FIFO queue. The program chooses which operations to use for the behavior it needs.

