awelgorithms

Split work between two stacks

Updated

A useful split: one stack is pending, the other is done, or left vs right of a cursor, or undo vs redo. Each op pushes to exactly one unless it transfers. Name the invariant: all pending are unprocessed.

What to focus on

If both stacks represent one sequence, their concatenation in some order is that sequence. If they are unrelated roles, do not concatenate them in your head.

Worked examples

undo/redo

undo [4, 1], redo [9]
  1. undo pops 1 onto redo
  2. redo [9, 1]

Result: roles stay separate

left/right

left [4, 1] (tops toward cursor), right [9]
  1. cursor between 1 and 9

Result: one sequence 4,1 | 9

pending/done

pending [4, 1, 9], done []
  1. pop pending push done to process 9 first

Result: LIFO processing

Try it

Write splitRoles(kind). kind is "undoRedo", "cursor", or "pendingDone". Print both stacks and one sentence of invariant. Pop only the named role stack. undo [4, 1], redo [9] → roles stay separate. left [4, 1] (tops toward cursor), right [9] → one sequence 4,1 | 9. pending [4, 1, 9], done [] → LIFO processing.

Named stacks start as given. Pop only a non-empty role stack. Values are integers.