25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 132<br />

T<br />

Ted<br />

T<br />

Fred<br />

T<br />

Mary<br />

T Adam<br />

F F T Eva<br />

F F F F<br />

Fig. 4.3. <strong>Data</strong> structure linked by pointers<br />

It must be emphasized that the use of pointers to implement recursive structures is merely a technique.<br />

The programmer need not be aware of their existence. Storage may be allocated automatically the first time<br />

a new component is referenced. However, if the technique of using references or pointers is made explicit,<br />

more general data structures can be constructed than those definable by purely recursive data definiton. In<br />

particular, it is then possible to define potentially infinite or circular (graph) structures <strong>and</strong> to dictate that<br />

certain structures are shared. It has therefore become common in advanced programming languages to<br />

make possible the explicit manipulation of references to data in addition to the data themeselves. This<br />

implies that a clear notational distinction must exist between data <strong>and</strong> references to data <strong>and</strong> that<br />

consequently data types must be introduced whose values are pointers (references) to other data. The<br />

notation we use for this purpose is the following:<br />

TYPE T<br />

= POINTER TO T0<br />

This type declaration expresses that values of type T are pointers to data of type T0. It is fundamentally<br />

important that the type of elements pointed to is evident from the declaration of T. We say that T is bound<br />

to T0. This binding distinguishes pointers in higher-level languages from addresses in assembly codes, <strong>and</strong><br />

it is a most important facility to increase security in programming through redundancy of the underlying<br />

notation.<br />

p: POINTER TO T<br />

p^: T<br />

Fig. 4.4. Dynamic allocation of variable p^.<br />

Values of pointer types are generated whenever a data item is dynamically allocated. We will adhere to<br />

the convention that such an occasion be explicitly mentioned at all times. This is in contrast to the situation<br />

in which the first time that an item is mentioned it is automatically allocated. For this purpose, we introduce<br />

a procedure NEW. Given a pointer variable p of type T, the statement NEW(p) effectively allocates a<br />

variable of type T0 <strong>and</strong> assigns the pointer referencing this new variable to p (see Fig. 4.4). The pointer<br />

value itself can now be referred to as p (i.e., as the value of the pointer variable p). In contrast, the variable<br />

which is referenced by p is denoted by p^. The referenced structures are typically records. If the<br />

referenced record has, for example, a field x, then it is denoted by p^.x. Because it is clear that not the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!