Software: Apache/2.0.54 (Unix) mod_perl/1.99_09 Perl/v5.8.0 mod_ssl/2.0.54 OpenSSL/0.9.7l DAV/2 FrontPage/5.0.2.2635 PHP/4.4.0 mod_gzip/2.0.26.1a uname -a: Linux snow.he.net 4.4.276-v2-mono-1 #1 SMP Wed Jul 21 11:21:17 PDT 2021 i686 uid=99(nobody) gid=98(nobody) groups=98(nobody) Safe-mode: OFF (not secure) /usr/doc/slang-1.4.5/html/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 13. Structures and User-Defined Types
A structure is a heterogeneous container object, i.e., it is an object with elements whose values do not have to be of the same data type. The elements or fields of a structure are named, and one accesses a particular field of the structure via the field name. This should be contrasted with an array whose values are of the same type, and whose elements are accessed via array indices. A user-defined data type is a structure with a fixed set of fields defined by the user.
13.1 Defining a Structure
The
This creates and returns a structure with N fields whose names
are specified by field-name-1, field-name-2, ...,
field-name-N. When a structure is created, all its fields are
initialized to NULL.
For example,
creates a structure with three fields and assigns it to the
variable t.
Alternatively, a structure may be created by dereferencing
These are useful when creating structures dynamically where one does
not know the name of the fields until run-time.
Like arrays, structures are passed around via a references. Thus,
in the above example, the value of
both t and u refer to the same structure,
since only the reference was used in the assignment. To actually
create a new copy of the structure, use the dereference
operator, e.g.,
13.2 Accessing the Fields of a Structure
The dot (
described in the last section. Then,
are all valid statements involving the fields of t.
13.3 Linked Lists
One of the most important uses of structures is to create a dynamic data structure such as a linked-list. A linked-list is simply a chain of structures that are linked together such that one structure in the chain is the value of a field of the previous structure in the chain. To be concrete, consider the structure discussed earlier:
and suppose that we desire to create a list of such structures.
The purpose of the next field is to provide the link to the
next structure in the chain. Suppose that there exists a function,
read_next_city, that reads city names and populations from a
file. Then we can create the list via:
In this function, the variables list_root and list_tail
represent the beginning and end of the list, respectively. As long
as read_next_city returns a non-zero value, a new structure is
created, initialized, and then appended to the list via the
next field of the list_tail structure. On the first
time through the loop, the list is created via the assignment to the
list_root variable.
This function may be used as follows:
We can create other functions that manipulate the list. An example is
a function that finds the city with the largest population:
The get_largest_city is a typical example of how one traverses
a linear linked-list by starting at the head of the list and
successively moves to the next element of the list via the
next field.
In the previous example, a
Here a foreach loop has been used to walk the list via its
next field. If the field name was not next, then it
would have been necessary to use the using form of the
foreach statement. For example, if the field name implementing the
linked list was next_item, then
would have been used. In other words, unless otherwise indicated
via the using clause, foreach walks the list using a field
named next.
Now consider a function that sorts the list according to population. To illustrate the technique, a bubble-sort will be used, not because it is efficient, it is not, but because it is simple and intuitive.
Note the test for equality between list and node, i.e.,
It is important to appreciate the fact that the values of these
variables are references to structures, and that the
comparison only compares the references and not the actual
structures they reference. If it were not for this, the algorithm
would fail.
13.4 Defining New Types
A user-defined data type may be defined using the
This data type can be used like all other data types. For example,
an array of Population_Type types can be created,
and `populated' via expressions such as
The new type Population_Type may also be used with the
typeof function:
The dereference @ may be used to create an instance of the
new type:
Next Previous Contents |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #13 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.0042 ]-- |