Download Introduction to Programming in Turing First Edition

Transcript
356
Introduction to Programming in Turing
be found. This is called the link or pointer to the next record. The
last record in the linked list has a link which is zero (null) to
indicate that there are no more records.
Here is a program that allows insertions and deletions to a
linked list of name and address records that is maintained in
alphabetical order. It also allows you to list the file in order. The
list could be used to print mailing labels. In the program there are
two linked lists being maintained: one of the label records, the
other of vacant record spaces in the array. The variable first
points to the first record in the list of labels; the variable vacant
points to the first vacant space.
% The "MaintainLinkedList" program
% Allows insertions and deletions to list
% Permits listing in order
type labelType :
record
name : string (20)
address : string (25)
link : int
end record
const null := 0
% Avoid using keyword label
const maxLabels := 100
var labels : array 1 .. maxLabels of labelType
% Initialize pointers to beginning of linked lists
% The label record list is empty
var first := null
% Initialize links of vacant array
var vacant : int := 1
% Each vacant record in array points to next
for i : 1 .. maxLabels – 1
labels (i ).link := i + 1
end for
% Last record in array has null link
labels (maxLabels).link := null
procedure insert (newLabel : labelType)
% Uses global variables
% Insert a new label record