There are actually several ways you can do this, some impromptu, some not impromptu.
For the non-impromptu, try looking into stacks, and from there, it should be pretty easy to come up with your own method.
For the impromptu version, look into a term called "clocking".
Also, you...