Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Storing Lists in Files

To store a linked list in a file, follow the following steps:

1.  Open the file for writing.
2.  Make the “CurrentPointer” point to the first node.
         CurrentPointer:= FirstPointer;
3.  Read the data field (CurrentPointer^.DataField), and write it to the file.
         WRITE(MyListFile, CurrentPointer^.DataField);
4.  Move the “CurrentPointer” to the next node by updating its direction to point to the pointer field (NextField)
         CurrentPointer:= CurrentPointer^.NextField;
5.  Repeat steps 3 and 4 until you reach the end of the list. At this point, the “CurrentPointer” will be NIL.
         CurrentPointer <> NIL
6.  Close the file.

The following program segment summarizes the preceding steps:

VAR
  CurrentPointer:ListPointer;
BEGIN
 ASSIGN(MyListFile, FileName);
 REWRITE(MyListFile);
 CurrentPointer:= FirstPointer;
 WHILE CurrentPointer <> NIL DO
  BEGIN
   WRITE(MyListFile, CurrentPointer^.DataField);
   CurrentPointer:= CurrentPointer^.NextField
  END;
 CLOSE(MyListFile)
END;

Reading Lists from Files

When you store a linked list in a file, you only store the data. The list pointers are only used in memory to control the list. Therefore, when the file is written to the disk, it becomes a regular data file, and may be read using the regular procedures. After reading the file, it is your preference to build the data as a linked list. To add the data read from a file to a linked list, do the following:

1.  Open the file for reading.
2.  Read a data item from the file.
3.  Add the item to the list using the procedure “BuildList” explained earlier.
In the following segment, the data item “Name” is read from the file “MyListFile” and added to the list:
WHILE NOT EOF (MyListFile) DO
  BEGIN
   READ(MyListFile, Name);
   BuildList(FirstPointer, Name);
  END;
4.  Repeat steps 2 and 3 until you reach end-of-file.

Drill 11-3

Modify the previous program to add the two options:

  Save the list to a file.
  Add data from file.

For the type of data you are using so far, you may use either a TEXT or a non-TEXT file.

Example: A List of Records

In this section, you are going to work with a more practical linked list, a list of employee records. Look at these types:

TYPE
{Declaration of data type }
  SSNstring = STRING[11];
  DataRecord = RECORD
         ID            :STRING[5];
         Name, Position       :STRING[20];
         SSN             :SSNstring;
         Rate            :REAL
        END;
{Declaration of the list }
  ListPointer = ^ListRecord;
  ListRecord = RECORD
            DataField:DataRecord;
            NextField:ListPointer
          END;
  EmpFile = FILE OF DataRecord;

These declarations are divided into two main parts:

1.  The definition of the data type (the record), which is used as a data field in the linked list.
2.  The linked list definition.

Note in these declarations that the “SSNstring” type comes first, because it is used in the definition of the employee record (DataRecord). Note also that the data field (DataField) in the linked list is of the type “DataRecord.”

A file of “DataRecords,” in which you are going to store the list, is also defined. Using a file of records makes the file handling much easier.

The global variables you are going to use are a list pointer, a file variable, and a record variable:

VAR
  FirstPointer:ListPointer;
  MyListFile  :EmpFile;
  EmpRecord      :DataRecord;

When you deal with a list of records, use the same procedures used with simple lists, because you are still dealing with nodes. Only remember to use fielded variables to read the fields. For example, in a list of strings you refer to each string using the variable

         CurrentPointer^.DataField

In a list of records, you refer to the “SSN” field (as an example) using the variable

         CurrentPointer^.DataField.SSN

or you may use a WITH statement to do the same thing:

WITH CurrentPointer^.DataField DO
     BEGIN
      WRITE(ID:7);
      WRITE(Name:22);...


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.