Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Reid G.C.Thinking in PostScript.1990.pdf
Скачиваний:
17
Добавлен:
23.08.2013
Размер:
846.44 Кб
Скачать

When a new chunk of data is created to add to the list (in much the same manner as this one was created), the list may be maintained by judicious definition of the next name to point to the next chunk in the list (see Example 12.11).

Example 12.11: Linking a New Element to the List

9 dict begin

% field definitions here

/next null def % end of list currentdict end

dup head exch /next exch put /tail exch def

Notice that the next pointer in the first chunk of data has been updated to be a reference to the newly-created list element, which is now stored as the tail of the list. The only elements in the list that have names are the head and tail entries in the list. The rest are anonymous, and can only be referenced by following the next pointers from the other dictionaries in the list.

Queues, Trees, and Other Data Structures

The concepts that have just been introduced for creating lists can easily be extended to create queues, stacks, or even trees of all sorts, since pointers are so easily created and assigned in PostScript.

For example, a queue can be implemented as a linked list of items that are always accessed in a first-in, first-out basis (FIFO). A stack could be built the same way, accessing the data on a last-in, first-out basis (LIFO), but it makes much more sense to use the PostScript operand stack to collect the objects if you need them in that order anyway. For more complicated structures like binary trees, you need only extend the basic “structure” model of a dictionary or an array to contain references to the appropriate elements of the tree. The structure of these classic data structures is beyond the scope of this book, but is discussed at great length in other volumes.

Chapter 12: STORING AND USING DATA

155