taocp

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

Volume C2 · Information structures · TAOCP Section 2.2.1

Stacks, queues and deques

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read it next to Section 2.2.1 of the book. Study the section: what to read first and the exercises, easiest first. The items of the section and their status.

Read first
  • C2.1, in C2. Information structures not written yet
  • C1.2.6, in C1. Basic concepts not written yet
Contents
  1. Lists with access at the ends

Lists with access at the ends

Overview

Section 2.2.1 of TAOCP lists nine operations that a program may do on a linear list x1,x2,…,xnx_1, x_2, \ldots, x_n, such as to reach the kkth node, to insert or delete a node at place kk, to join two lists, to split a list, to copy a list and to count its nodes. No one representation makes all nine fast. So the book looks at lists where the work happens at the ends only, and it gives them names: stack, queue and deque. It also gives the notation A⇐xA \Leftarrow x and x⇐Ax \Leftarrow A. The rest of the section is a set of 14 exercises on one question: if the items 1,2,…,n1, 2, \ldots, n enter a list in this order, which orders can leave it? This chapter shows the answer with proofs and with tables. The sections that follow (2.2.2 to 2.2.6) show how to store these lists in memory, and Chapter 8 of the book uses the stack for recursion.

The ideas

Three lists, three rules. Think of a row of items with a left end and a right end.

Kind Where items go in Where items go out Name of the order
stack one end, the top the same end last in, first out
queue one end, the rear the other end, the front first in, first out
deque either end either end none

An input-restricted deque lets items in at one end only and out at both ends. An output-restricted deque lets items out at one end only and in at both ends. A stack and a queue are both special cases of a deque. A stack is an input-restricted deque that always takes its items out at the end where they went in. A queue is an input-restricted deque that always takes its items out at the other end.

A stack as a railway yard. Cars come on the input track in the order 1,2,3,…1, 2, 3, \ldots. A car can go into the stack, a short dead end, and a car can leave the stack to the output track. Only the car at the end of the dead end can leave. Write S for "a car goes from the input into the stack" and X for "a car goes from the stack to the output". The word of S and X that a switchman does is the code of the run. For example, SSXSXX\mathrm{SSXSXX} with three cars: car 1 goes in, car 2 goes in, car 2 goes out, car 3 goes in, car 3 goes out, car 1 goes out. The output order is 2,3,12, 3, 1.

Three questions follow. Which words are meaningful (the stack is never empty when X is done)? Do two different meaningful words give two different output orders? Which output orders are possible? The sections below answer all three, first for the stack and then for the other lists.

Definitions, results and proofs

Definition C2.2.2.1.1 (the three kinds of list, TAOCP Section 2.2.1). A stack is a linear list in which all insertions and deletions, and usually all accesses, are at one end. A queue is a linear list in which all insertions are at one end and all deletions, and usually all accesses, are at the other end. A deque is a linear list in which all insertions and deletions, and usually all accesses, are at the two ends. An input-restricted deque takes insertions at one end only. An output-restricted deque takes deletions at one end only.

Node: 2.2.1-def-stack · Lean: not yet stated

Explanation. The words say which operations the program may use, not how the list sits in memory. A stack stored in an array and a stack stored as a chain of nodes are the same stack, if the same operations give the same results. The word "usually all accesses" says that a program may look at the end item without removing it.

Example. A list holds 5,8,25, 8, 2 in this order from the left. As a stack with its top at the left end, a deletion gives 55 and leaves 8,28, 2. As a queue with its front at the left end, a deletion also gives 55. If the rear is at the right end, an insertion of 99 gives 5,8,2,95, 8, 2, 9. As a stack with the top at the left, the insertion of 99 gives 9,5,8,29, 5, 8, 2. So the two lists differ in the place of an insertion, not in the place of a deletion.

Definition C2.2.2.1.2 (the words of position, TAOCP Section 2.2.1). In a stack the end where items enter and leave is the top, and the other end is the bottom. In a queue items enter at the rear and leave from the front. In a deque the two ends are the left end and the right end.

Node: 2.2.1-def-terms · Lean: not yet stated

Explanation. The words are only names. Nothing moves in memory when an item goes onto a stack: the item is added at the top, and the other items stay where they are. The words "push" and "pop" are common, but this chapter uses "insert" and "delete" to keep this in mind.

Example. Take the queue 4,6,14, 6, 1 with the front on the left. Item 44 is at the front and item 11 is at the rear. After one deletion the front is 66. After the insertion of 33 the rear is 33 and the list is 6,1,36, 1, 3.

Definition C2.2.2.1.3 (insertion, TAOCP eq. 2.2.1(1)). When AA is a stack, the statement A⇐xA \Leftarrow x inserts the value xx on the top of AA. When AA is a queue, it inserts xx at the rear of AA.

Node: 2.2.1-(1) · Lean: not yet stated

Explanation. One symbol serves both lists, since the algorithm that uses it does not need to say where the item goes. The kind of list says where.

Example. Let AA be an empty stack. After A⇐7A \Leftarrow 7 and A⇐4A \Leftarrow 4 the list from the top is 4,74, 7. If AA is an empty queue instead, the same two statements give the list 7,47, 4 from the front to the rear.

Definition C2.2.2.1.4 (deletion, TAOCP eq. 2.2.1(2)). The statement x⇐Ax \Leftarrow A sets the variable xx to the item at the top of the stack AA, or at the front of the queue AA, and deletes that item from AA. It has no meaning when AA is empty.

Node: 2.2.1-(2) · Lean: not yet stated

Explanation. A deletion from an empty list is an error that the program must prevent. In a program the test for an empty list comes before the deletion. Section 2.2.2 calls the error underflow.

Example. Take the lists of the previous example. For the stack 4,74, 7, the statement x⇐Ax \Leftarrow A gives x=4x = 4 and leaves 77. For the queue 7,47, 4, it gives x=7x = 7 and leaves 44. A second statement on the stack gives x=7x = 7 and leaves the empty stack. A third has no meaning.

Definition C2.2.2.1.5 (the top, TAOCP eq. 2.2.1(3)). For a nonempty stack AA, top⁡(A)\operatorname{top}(A) is the item at the top of AA. It does not change AA.

Node: 2.2.1-(3) · Lean: not yet stated

Explanation. The function top⁡\operatorname{top} is the access that the definition of a stack allows without a deletion. It is not defined for an empty stack.

Example. For the stack 4,74, 7 from the top, top⁡(A)=4\operatorname{top}(A) = 4 and the stack still has two items. After x⇐Ax \Leftarrow A the value top⁡(A)\operatorname{top}(A) is 77.

Theorem C2.2.2.1.6 (the admissible words, TAOCP exercise 2.2.1-3). A word of S and X with nn letters S and nn letters X is the code of a complete run on a stack of nn cars if and only if each prefix of the word has at least as many S as X. Two different admissible words give two different output orders.

Node: 2.2.1-res-admissible · Lean: not yet stated

Explanation. A word with this prefix property is called admissible. The first half is the rule "never delete from an empty stack": the stack holds (the number of S) minus (the number of X) cars after a prefix. The second half says that the output order tells the word back. The rule shows how to check a word by one pass with a counter.

Example. For n=3n = 3 the word SSXSXX\mathrm{SSXSXX} has the prefix counts (S−X)(S - X) equal to 1,2,1,2,1,01, 2, 1, 2, 1, 0, never below 00, so it is admissible. The word SXXSSX\mathrm{SXXSSX} has the counts 1,0,−11, 0, -1 and it is not admissible: the third letter deletes from an empty stack. The words SSXSXX\mathrm{SSXSXX} and SSSXXX\mathrm{SSSXXX} are two admissible words, and they give 2,3,12, 3, 1 and 3,2,13, 2, 1.

Proof. After a prefix with ss letters S and xx letters X the stack holds s−xs - x cars. An X needs a car, so s−x≥0s - x \ge 0 must hold after each letter. This is the prefix rule, and it is enough, since then each X finds a car. Now let two admissible words give the same output, and suppose they differ. Let t+1t + 1 be the first place where they differ. The first tt letters are the same, so both runs have the same stack and the same next input car at this point. One word has X at the place t+1t + 1 and the other has S. Say the first word has X. It outputs the car cc at the top of the stack, and this is its mmth output, for some mm. In the second word the car cc stays in the stack, and the letter S puts a car above it. The mmth output of the second word is made by the next X after the place t+1t + 1, and no X lies between. At that X the top of the stack is a car that lies above cc, so it is not cc. So the two outputs differ at the position mm. □\square

Theorem C2.2.2.1.7 (the number of orders of a stack, TAOCP exercise 2.2.1-4). The number of orders of 1,2,…,n1, 2, \ldots, n that a stack can make is an=1n+1(2nn).a_n = \frac{1}{n+1}\binom{2n}{n}.

Node: 2.2.1-res-catalan · Lean: Mathlib catalan

Explanation. By the previous theorem, ana_n is the number of admissible words, so the proof counts the words. The numbers 1,1,2,5,14,42,132,4291, 1, 2, 5, 14, 42, 132, 429 for n=0,1,…,7n = 0, 1, \ldots, 7 are the Catalan numbers. Mathlib defines them as catalan n. This chapter does not name a lemma that links the two forms.

Example. For n=3n = 3 the formula gives 14(63)=204=5\frac{1}{4}\binom{6}{3} = \frac{20}{4} = 5. The five admissible words are SSSXXX, SSXSXX, SSXXSX, SXSSXX and SXSXSX, and they give the orders 321321, 231231, 213213, 132132 and 123123. The order 312312 is the only order of 1,2,31, 2, 3 that the stack cannot make. For n=4n = 4 the formula gives 705=14\frac{70}{5} = 14 of the 2424 orders.

Proof. There are (2nn)\binom{2n}{n} words with nn letters S and nn letters X. Count the words that are not admissible. Such a word has a first place where the counter S−XS - X is −1-1. Swap S and X in all letters up to and including that place. The prefix had one more X than S, so the new prefix has one more S than X, and the whole word gets n+1n + 1 letters S and n−1n - 1 letters X. Conversely, take a word with n+1n + 1 letters S and n−1n - 1 letters X. Its counter goes from 00 to 22 in steps of 11, so it has a first place where the counter is +1+1. Swap the letters up to that place, and the counter at that place becomes −1-1 for the first time. The two maps undo each other. So the number of inadmissible words is (2nn+1)=(2nn)⋅nn+1\binom{2n}{n+1} = \binom{2n}{n} \cdot \frac{n}{n+1}, and an=(2nn)−(2nn+1)=(2nn)(1−nn+1)=1n+1(2nn).□a_n = \binom{2n}{n} - \binom{2n}{n+1} = \binom{2n}{n}\left(1 - \frac{n}{n+1}\right) = \frac{1}{n+1}\binom{2n}{n}. \quad \square

Theorem C2.2.2.1.8 (the forbidden pattern, TAOCP exercise 2.2.1-5). A stack can turn 1 2…n1\,2 \ldots n into p1p2…pnp_1 p_2 \ldots p_n if and only if there are no indices i<j<ki < j < k with pj<pk<pip_j < p_k < p_i.

Node: 2.2.1-res-pattern · Lean: not yet stated

Explanation. The three values form the pattern 3 1 23\,1\,2: a large value, then a small one, then a middle one. A stack can never output a large car, then a small car, then a middle car, since the small and the middle car are both under the large car or before it, and the stack returns them in the reverse order of their input. The theorem makes the test for a given order easy to think about, and the next algorithm makes it easy to run.

Example. The order 2 3 1 5 42\,3\,1\,5\,4 has no triple of this kind. The order 2 5 4 1 32\,5\,4\,1\,3 has the triple (p2,p4,p5)=(5,1,3)(p_2, p_4, p_5) = (5, 1, 3), and also (4,1,3)(4, 1, 3). So the first order is possible and the second is not. For n=4n = 4 the ten orders with the pattern are 14231423, 24132413, 31243124, 31423142, 34123412, 41234123, 41324132, 42134213, 42314231 and 43124312, and 24−10=1424 - 10 = 14.

Proof. Suppose that a stack gives the order pp and that i<j<ki < j < k with pj<pk<pip_j < p_k < p_i exist. Cars enter in the order 1,2,…,n1, 2, \ldots, n, so when the car pip_i leaves, the cars pjp_j and pkp_k, which are smaller, have entered already. Both are still in the stack, since they leave later. They entered in increasing order, so pkp_k is above pjp_j, and pkp_k must leave before pjp_j. This contradicts j<kj < k. For the other half, assume that the order has no such triple and run the test of Algorithm 2.2.1-T below. If it fails, it fails at an output v=pmv = p_m when the car vv is in the stack and a car t>vt > v lies above it. The car tt is not yet output, so t=prt = p_r with r>mr > m. The car tt entered the stack because an earlier output pip_i with i<mi < m was larger than tt (the test enters cars only up to the wanted one). So pi>pr>pmp_i > p_r > p_m with i<m<ri < m < r, which is the forbidden pattern. □\square

Theorem C2.2.2.1.9 (the queue, TAOCP exercise 2.2.1-6). A queue can give only one order of 1,2,…,n1, 2, \ldots, n, the order 1,2,…,n1, 2, \ldots, n itself.

Node: 2.2.1-res-queue · Lean: not yet stated

Explanation. A queue keeps the order of its items. So it cannot permute. The exercise has rating 00 for this reason.

Example. Put 1,2,31, 2, 3 into a queue with any mix of insertions and deletions that is legal. Item 11 is inserted first, so it is at the front, and it is the first to leave. Then 22 is the front, and so on. The output is 1,2,31, 2, 3 for each mix.

Proof. Items enter in the order 1,…,n1, \ldots, n, and the front item is always the oldest item in the queue. So the item that leaves is the oldest item that has not left. By induction the mmth item to leave is mm. □\square

Result (the deques, TAOCP exercises 2.2.1-7 to 2.2.1-9). Let bnb_n be the number of orders of 1,…,n1, \ldots, n that an input-restricted deque can make, and cnc_n the number for an output-restricted deque. Let gng_n be the number for a general deque. Then bn=cnb_n = c_n for all nn, and the values are in the table. For n=4n = 4 the order 41324132 is possible with an input-restricted deque only, the order 42134213 with an output-restricted deque only, and the order 42314231 with neither of the two.

nn 1 2 3 4 5 6 7
stack ana_n 1 2 5 14 42 132 429
queue 1 1 1 1 1 1 1
input-restricted bnb_n 1 2 6 22 90 394 1806
output-restricted cnc_n 1 2 6 22 90 394 1806
deque gng_n 1 2 6 24 116 634 3762

Node: 2.2.1-res-deques · Lean: not yet stated

Explanation. The values come from a search over all states of the run (the deque content, the next input car and the cars output so far), done by python for this chapter. The proof of bn=cnb_n = c_n is in the X1 solution of exercise 9: a run on one kind of deque, read backward in time and with the cars renamed, is a run on the other kind. The general deque makes all 2424 orders for n=4n = 4, and it misses 44 of the 120120 orders for n=5n = 5. These are 5 1 3 2 45\,1\,3\,2\,4, 5 1 3 4 25\,1\,3\,4\,2, 5 2 3 1 45\,2\,3\,1\,4 and 5 2 3 4 15\,2\,3\,4\,1. No simple closed form for gng_n is known, and the question is open (the exercise has rating 48).

Example. The order 41324132 with an input-restricted deque that takes items in at the right end only: insert 11, 22, 33, 44 to get [1,2,3,4][1, 2, 3, 4]. Delete from the right to output 44. Delete from the left to output 11. Delete from the right to output 33, and then 22. The output is 4,1,3,24, 1, 3, 2. The order 42134213 with an output-restricted deque that takes items out at the left end only: insert 11 to get [1][1]; insert 22 at the left to get [2,1][2, 1]; insert 33 at the right to get [2,1,3][2, 1, 3]; insert 44 at the left to get [4,2,1,3][4, 2, 1, 3]. Delete four times at the left to output 4,2,1,34, 2, 1, 3.

Algorithms and programs

Algorithm C2.2.2.1.10 (can a stack make this order, TAOCP Section 2.2.1, exercise 2.2.1-5). The input is an order p1…pnp_1 \ldots p_n of 1,…,n1, \ldots, n. The output is "yes" with the code word of S and X, or "no".

Node: 2.2.1-alg-test · Lean: not yet stated

The idea. Read the wanted outputs one at a time. For the wanted car vv, move input cars into the stack until vv is at the top. If vv is already in the stack but not at the top, the order is not possible. Else delete vv.

The steps.

StackTest(p[1..n]):
  T1: S := empty stack; next := 1; word := ""
  T2: for m := 1 to n:
        v := p[m]
        while (S is empty or top(S) != v) and next <= n and v >= next:
          S <= next; next := next + 1; word := word + "S"
        if S is empty or top(S) != v: return "no"
        x <= S; word := word + "X"
  T3: return "yes", word

Invariant. At the start of each pass of the loop on mm, the stack holds, from the bottom to the top, an increasing list of cars from 1,…,next−11, \ldots, \text{next} - 1 that are not yet output, and it holds all of them. The cars next,…,n\text{next}, \ldots, n are still on the input. The loop on next keeps the stack increasing, since each car pushed is larger than all cars in the stack.

A worked run, a possible order. The order is p=2 3 1 5 4p = 2\,3\,1\,5\,4. Each row is the state after the pass for the wanted car.

mm vv Moves Stack, bottom to top next word
start empty 1
1 2 insert 1, insert 2, delete 2 1 3 SSX
2 3 insert 3, delete 3 1 4 SSXSX
3 1 delete 1 empty 4 SSXSXX
4 5 insert 4, insert 5, delete 5 4 6 SSXSXXSSX
5 4 delete 4 empty 6 SSXSXXSSXX

The answer is "yes" with the word SSXSXXSSXX\mathrm{SSXSXXSSXX}, which is admissible.

A worked run, an impossible order. The order is p=2 5 4 1 3p = 2\,5\,4\,1\,3.

mm vv Moves Stack, bottom to top next Result
start empty 1
1 2 insert 1, insert 2, delete 2 1 3
2 5 insert 3, 4, 5, delete 5 1, 3, 4 6
3 4 delete 4 1, 3 6
4 1 none 1, 3 6 top is 3, not 1: "no"

The failure shows the pattern. The car 33 is above 11 in the stack, and 33 is not yet output. The car 33 entered because an earlier output (55 or 44) was larger than 33. So the triple (5,1,3)(5, 1, 3) is of the form of the theorem.

Correctness. The test makes the only moves that can lead to the wanted car. Cars enter in increasing order, so the car vv can leave only when it is at the top. If it is in the stack and not at the top, a car above it must leave first, which is not what the order asks. If it is not in the stack, it is on the input and the test moves cars in until it is on the top. The loop ends, since each pass of the inner loop moves one car and each outer pass deletes one car.

Cost. The test inserts each car at most once and deletes each car at most once, so it does at most 2n2n stack operations and at most nn comparisons with the top, whatever the order. The two runs above use 1010 and 88 operations.

Algorithm C2.2.2.1.11 (a queue from two stacks, TAOCP exercise 2.2.1-14). The input is a series of insertions Q⇐xQ \Leftarrow x and deletions x⇐Qx \Leftarrow Q. The output is the same as for a queue. The program has two stacks, IN and OUT, and both are empty at the start.

Node: 2.2.1-alg-twostacks · Lean: not yet stated

The idea. New items go on IN. Items leave from OUT. When OUT is empty and a deletion comes, move all items from IN to OUT one by one. This reverses their order, so the oldest item is then on the top of OUT.

The steps.

Insert(x):  IN <= x
Delete():
  D1: if OUT is empty:
        while IN is not empty: y <= IN; OUT <= y
  D2: if OUT is empty: error (the queue is empty)
  D3: x <= OUT; return x

Invariant. The queue, from the front to the rear, is the list OUT read from the top to the bottom, followed by the list IN read from the bottom to the top. Insert changes the end of that list, and a move from IN to OUT does not change the list.

A worked run. The operations are: insert aa, insert bb, delete, insert cc, delete, delete, insert dd, delete. Each row is the state after the operation. IN and OUT are shown from the bottom to the top.

Operation Result IN OUT Items moved
start empty empty
insert aa aa empty 0
insert bb a,ba, b empty 0
delete aa empty bb 2
insert cc cc bb 0
delete bb cc empty 0
delete cc empty empty 1
insert dd dd empty 0
delete dd empty empty 1

The outputs are a,b,c,da, b, c, d, the order of the insertions, as for a queue.

Correctness. The invariant holds at the start, with both stacks empty. A move of one item from IN to OUT takes the top of IN, which is the newest item of IN, and puts it on the top of OUT. When all items have moved, OUT holds them with the oldest on the top, and then the list in the invariant is the same as before. The deletion takes the top of OUT, which is the front of the queue.

Cost. An item is inserted on IN once, moved at most once and deleted from OUT once, so it takes part in at most four stack operations (insert on IN, delete from IN, insert on OUT, delete from OUT). So mm queue operations with n≤mn \le m insertions cost at most 4n4n stack operations in all, though one deletion alone can cost 2⋅2 \cdot (the size of IN) +1+ 1 stack operations. The run above has 44 items and 44 moves.

The exercises

The table has the 14 exercises of Section 2.2.1. The Markers column has the symbols of the book (M for mathematics, HM for higher mathematics).

Exercise Rating Markers Kind Lean Solution
2.2.1-1 06 K2 not yet stated X1, Exercise 2.2.1-1
2.2.1-2 15 K2 not yet stated X1, Exercise 2.2.1-2
2.2.1-3 25 K1 not yet stated X1, Exercise 2.2.1-3
2.2.1-4 34 M K3 Mathlib catalan X1, Exercise 2.2.1-4
2.2.1-5 28 M K1 not yet stated X1, Exercise 2.2.1-5
2.2.1-6 00 K3 not yet stated X1, Exercise 2.2.1-6
2.2.1-7 25 K3 not yet stated X1, Exercise 2.2.1-7
2.2.1-8 22 K2 not yet stated X1, Exercise 2.2.1-8
2.2.1-9 20 M K1 not yet stated X1, Exercise 2.2.1-9
2.2.1-10 25 M K1 not yet stated X1, Exercise 2.2.1-10
2.2.1-11 40 M K3 not yet stated X1, Exercise 2.2.1-11
2.2.1-12 34 HM K4 not yet stated X1, Exercise 2.2.1-12
2.2.1-13 48 M K9 not yet stated X1, Exercise 2.2.1-13
2.2.1-14 26 K5 not yet stated X1, Exercise 2.2.1-14

Exercises 1 and 6 are quick checks of the definitions. Exercises 2 and 3 introduce the code of S and X, and this chapter proves the answer of exercise 3. Exercises 4 and 5 are the two theorems above. Exercises 7 to 9 compare the restricted deques, and the table above shows the counts. Exercise 8 asks if a general deque can make all orders. For n≤4n \le 4 it can, and for n=5n = 5 it misses the four orders listed. Exercises 10 and 11 count the orders of an output-restricted deque by a code of its own and by a generating function. Exercise 12 asks for the growth of ana_n and bnb_n. Exercise 13 is open: the number gng_n of orders of a general deque has no known closed form, and the table above gives the first seven values. Exercise 14 is the queue from two stacks.

Lean

The repository tamnd/taocp-lean has no declaration for Chapter 2 yet, so nothing here is stated in Lean. Mathlib defines the Catalan numbers as catalan n, and the theorem above would say that the number of stack orders of nn cars equals catalan n. A formal model needs the three objects of the proofs: a word of S and X, the run of a stack on it, and the output order. Then "admissible" is a property of the word, and the theorems are statements about the map from admissible words to orders. The statements are not stated yet.

Notes and history

The railway picture of a stack is due to E. W. Dijkstra. Many names exist for the same lists. The stack is also the push-down list, the cellar and the LIFO list, and the queue is also the FIFO list. A deque is a "double ended queue", and it is pronounced like "deck". The count ana_n is the Catalan number named for Eugene Catalan. L. Euler found them for the problem of the cuts of a polygon into triangles, and Section 2.3.4.4 of the book meets the same numbers for binary trees. The forbidden pattern 3 1 23\,1\,2 is an early example of a "pattern avoiding" class of orders, a topic that has its own literature today. The O(n)O(n) algorithm of Rosenstiehl and Tarjan, which the book quotes in exercise 13, decides in linear time if a given order is possible for a general deque.

Section 2.2.1 of TAOCP names three kinds of linear list: the stack, the queue and the deque. In each of them, insertions and deletions happen at the ends of the list only. The section gives the words and the notation that all later sections use, and it uses a railway switching yard to ask a question: which orders of 1,2,…,n1, 2, \ldots, n can come out of such a list when the items go in in the order 1,2,…,n1, 2, \ldots, n? This chapter of C2 states the answer for the stack (the Catalan numbers and a forbidden pattern), for the queue (one order), and for the two restricted deques and the deque (counts found by exhaustive search). It adds two small algorithms that the book leaves to its exercises: a test that says if a given order can come out of a stack, and a queue built from two stacks. Both are run by hand and costed.

The source of this page. Something is wrong? Report it.