II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
NOTES ON DATA STRUCTURING 139<br />
in the chain. The empty sequence is represented by a value which could not<br />
possibly be an address (say zero or minus <strong>on</strong>e); and the link locati<strong>on</strong> of the<br />
last item in the sequence c<strong>on</strong>tains this value. The first item in the chain is<br />
pointed to by the base locati<strong>on</strong> of the sequence.<br />
A single linked chain is useful when the directi<strong>on</strong> in which the sequence<br />
will be read is known; for the links have to point in this directi<strong>on</strong>. In the<br />
case of a stack they will point backwards, and in the case of input and output<br />
sequences and queues they will point forwards. In the case of an input or<br />
output sequence, the base locati<strong>on</strong> of the external variable which is to hold<br />
the initial and/or final value of the sequence points permanently at the<br />
beginning of the chain, while the base locati<strong>on</strong> of the sequence itself steps<br />
through the sequence. In the case of a queue, two base locati<strong>on</strong>s are used,<br />
to point to each end of the sequence.<br />
1<br />
Stack<br />
(a)<br />
end<br />
FIG. 7.<br />
[<br />
Queue<br />
:L...--."-"-<br />
I<br />
Deque<br />
write pointer [., write pointer<br />
read pointer ,[ , read pointer<br />
,, ,<br />
!, "I<br />
ll<br />
(b) (c)<br />
Sequences (Chained Representati<strong>on</strong>)<br />
-<br />
difference<br />
of two<br />
pointers<br />
One possible advantage of the single-chained representati<strong>on</strong> in the case of<br />
stacks is that several stacks can share the same initial segments, which may<br />
save space and time in some applicati<strong>on</strong>s. However, when an item is popped<br />
up from such a stack, the storage space which it occupies cannot be immedi-<br />
ately returned to the free chain, since it may be in use as part of another<br />
stack. One soluti<strong>on</strong> to this problem is never to return storage explicitly,<br />
but to wait until the free chain is exhausted. Then all currently allocated<br />
sequences are scanned, and all blocks currently in use are marked. Then all<br />
unmarked blocks are collected <strong>on</strong>to the free chain. This is known as a scan-