taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.2.1-1

Exercise 2.2.1-1

not stated yet in Lean solution written

Rating06
KindK2, decide: say whether a claim holds from the written solution
SectionSection 2.2.1 of Volume 1

The task

Decide whether an output-restricted deque can also work as a stack or as a queue.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

An output-restricted deque lets you insert at both ends and remove at one end only. Choose the end for the inserts so that the order of removal is the one you want.

Solution

Show the solution

Yes, it can do both. Call the end where items leave the left end. Inserts may go to either end.

If every insert goes to the left end, the newest item is the first to leave. The deque is then a stack.

If every insert goes to the right end, the oldest item is the first to leave. The deque is then a queue.

Check

Insert 1,2,31, 2, 3 and remove all. With left inserts the output is 3 2 13\,2\,1. With right inserts the output is 1 2 31\,2\,3.

Remarks

The two restricted deques are not equal in power. Exercise 2.2.1-7 shows permutations that one of them gives and the other does not. The case in this exercise is symmetric to the input-restricted case in the text, which also works as a stack or as a queue.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.