II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
136 c.A.R. HOARE<br />
(3) If the <strong>on</strong>ly operati<strong>on</strong>s are writing and reading back (push down and<br />
pop up), the sequence is known as a stack; the initial value of a stack is<br />
always empty, and the final value is not usually preserved.<br />
(4) If the <strong>on</strong>ly operati<strong>on</strong>s are writing to the end and reading from the<br />
beginning, the sequence is known as a queue; again, the initial value is always<br />
empty, and the final value is not usually preserved.<br />
(5) If" reading and writing at both ends of a sequence are permitted, the<br />
sequence is sometimes known as a deque (double-ended queue). However,<br />
to make all four operati<strong>on</strong>s equally efficient requires some complexity of<br />
representati<strong>on</strong>, so it is fortunate that most programs can get by without<br />
using deques.<br />
8.2. REPRESENTATION<br />
8.2.1. C<strong>on</strong>tiguous representati<strong>on</strong><br />
The simplest method of representing a sequence is to allocate to it a fixed<br />
c<strong>on</strong>tiguous area of storage, adequate to hold all items actually required.<br />
This method is suitable if the value (or at least the length) of the sequence is<br />
c<strong>on</strong>stant throughout the executi<strong>on</strong> of the program--for example, a string of<br />
characters intended to be used as an output message or title.<br />
In some cases, the length of the sequence is unknown at the time the<br />
program is written, but is known <strong>on</strong> entry to the block in which the sequence<br />
is declared, and this length remains c<strong>on</strong>stant throughout the existence of the<br />
sequence. In such cases, it is possible to allocate a c<strong>on</strong>tiguous area of storage<br />
in the local workspace of the block, using the standard stack method of store<br />
allocati<strong>on</strong> and deallocati<strong>on</strong>.<br />
Even if the length of the sequence is subject to variati<strong>on</strong>, it is sometimes<br />
possible to place an acceptably small upper bound <strong>on</strong> its length, and allocate<br />
permanently this maximum area. If the limit is exceeded during a run of the<br />
program, the programmer must be willing to accept its immediate termina-<br />
ti<strong>on</strong>. In additi<strong>on</strong> to the fixed area, a pointer or count is required to indicate<br />
the current beginning and end of the sequence. In the case of a stack, the first<br />
item is always at the beginning, and <strong>on</strong>ly <strong>on</strong>e pointer to the top of the stack<br />
is required. In the case of a queue, the sequence will at times overlap the<br />
end of the store area, and be c<strong>on</strong>tinued again at the beginning. Such a<br />
representati<strong>on</strong> is known as a cyclic buffer, and may be used in a parallel<br />
programming situati<strong>on</strong> to communicate informati<strong>on</strong> between processes<br />
running in parallel. In this case, when a writing process finds the buffer full,<br />
it has to wait until a reading process reduces the size of the sequence again.<br />
Similarly, the reading process must wait when the buffer is empty.<br />
Another case where the c<strong>on</strong>tiguous representati<strong>on</strong> is the best is when the<br />
program requires <strong>on</strong>ly a single sequence, which may therefore occupy the