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


Searching the List

In real applications, displaying the whole list on the screen is not useful, because the list may be too long. Instead, you need to display a specific record. To do this, you have to search in the list for a unique field such as the social security number “SSN.” These are the steps to search a list:

1.  Start from the first node by setting up the “CurrentPointer” so that it points to the first node:
         CurrentPointer:= FirstPointer;
2.  Match the social security number entered form the keyboard (SSNumber) with the “SSN” field in the node. If they match, set a flag such as “Found”:
         IF CurrentPointer^.DataField.SSN = SSNumber THEN
         Found := TRUE

The “CurrentPointer” in this case, is just pointing to the required node, and may be used to read the information.
3.  If the required record is not found, move the “CurrentPointer” to the next node:
         CurrentPointer:= CurrentPointer^.NextField;
4.  Repeat steps 2 and 3 until you either find the matching record (Found = TRUE), or you reach the end of the list (CurrentPointer = NIL). Thus, your WHILE loop will be using these two conditions:
         WHILE (CurrentPointer <> NIL) AND (NOT Found) DO
         ....

The following is a program segment that includes the preceding steps:

         CurrentPointer:= FirstPointer;
          WHILE (CurrentPointer <> NIL) AND (NOT Found) DO
            IF CurrentPointer^.DataField.SSN = SSNumber THEN
             Found := TRUE
           ELSE
            CurrentPointer:= CurrentPointer^.NextField;

To display the information in the required node, you may use the following segment:

         WITH CurrentPointer^.DataField DO
            BEGIN
             WRITELN('ID: ',ID);
             WRITELN('Name: ',Name);
             WRITELN('Position: ', Position);
             WRITELN('Social Security Number: ',SSN);
             WRITELN('Hourly Rate: ',Rate:2:2)
            END;

The following program is the linked-list version of the employee database. It includes the options to search for and display a specific record, in addition to better file processing. The program includes the following procedures:

• “SearchList” to search for a specific record
• “BuildList” to add records to the list
• “ReadList” to display the whole list
• “GetData” to accept data from the keyboard
• “DisplayRec” to display a specific record
• “DisplayItAll” to display the headers of the fields and invoke “ReadList”
• “ReadFile” to read records from the data file and invoke “BuildList”
• “SaveList” to save the list to a file
• “Menu” to display the user menu
{ -------------------------- figure 11-14 ---------------------------- }
PROGRAM LinkedListDB(INPUT, OUTPUT, MyListFile);
{ This program processes an employee database as list of records. }

CONST
  FileName = 'emplist.bin';
  Header = '------------- Main Menu --------------';
  Separator = '--------------------------------------';

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;

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

{ ----------------------- Procedure SearchList ----------------------- }
{ This procedure searches the linked list for an employee's SSN. If
found, the value of the boolean flag "Found" becomes TRUE, and the
"CurrentPointer" points to the required node. }
PROCEDURE SearchList(FirstPointer:ListPointer;
          VAR CurrentPointer:ListPointer;
          SSNumber:SSNstring;
          VAR Found:BOOLEAN);

BEGIN
 CurrentPointer:= FirstPointer;
 WHILE (CurrentPointer <> NIL) AND (NOT Found) DO
   IF CurrentPointer^.DataField.SSN = SSNumber THEN
     Found:= TRUE
  ELSE
   CurrentPointer:= CurrentPointer^.NextField;
END;

{ ----------------------- Procedure BuildList ------------------------ }
PROCEDURE BuildList(VAR FirstPointer:ListPointer;
          DataItem:DataRecord);
{ This procedure builds the liked list, or adds nodes to it.}
{Note: The FirstPointer is passed using the VAR keyword as it will be
updated by this procedure. }

VAR
 ToolPointer:ListPointer;
BEGIN
 NEW(ToolPointer);
 ToolPointer^.DataField := DataItem;
 ToolPointer^.NextField := FirstPointer;
FirstPointer := ToolPointer
END;
{ ----------------------- Procedure ReadList ------------------------- }
PROCEDURE ReadList(FirstPointer:ListPointer);
{ This procedure reads and displays the contents of the list. }

VAR
 CurrentPointer:ListPointer;

BEGIN
 CurrentPointer:= FirstPointer;
 WHILE CurrentPointer <> NIL DO
  BEGIN
   WITH CurrentPointer^.DataField DO
    BEGIN
     WRITE(ID:7);
     WRITE(Name:22);
     WRITE(Position:22);
     WRITE(SSN:13);
     WRITELN(' $',Rate:0:2)
    END;
    CurrentPointer:= CurrentPointer^.NextField
  END;
  WRITELN
END;
{ ----------------------- Procedure GetData -------------------------- }
PROCEDURE GetData(VAR FirstPointer:ListPointer);
{ This procedure receives the employee data from the keyboard, and
passes the record information to the procedure "BuildList" to be added to the
linked list. }
VAR
  Item:DataRecord;

BEGIN
  WRITELN('Please enter the record information,',
                  ' when finished hit ENTER.');
{ Read the first data item }
  WITH Item DO
      BEGIN
       WRITE('ID: ');        READLN(ID);
       WRITE('Name: ');          READLN(Name);
       WRITE('Position: ');    READLN(Position);
       WRITE('SSN: ');        READLN(SSN);
       WRITE('Rate: ');        READLN(Rate);
       WRITE(Separator)
      END;
    BuildList(FirstPointer, Item);
END;

{ --------------------- Procedure DisplayItAll ----------------------- }
PROCEDURE DisplayItAll(FirstPointer:ListPointer);
{ This procedures displays the headers of the fields in the proper
format and calls the procedure "ReadList" to display the contents of
the list. }

BEGIN
 WRITELN(Separator);
 WRITELN('The contents of the list: ');
 WRITELN('ID':7, 'Name':22, 'Position':22, 'SSN':13,
     'Rate':7);
 WRITELN;
 ReadList(FirstPointer);
 WRITE('Hit any key to continue...');
 READLN
END;

{ ----------------------- Procedure DisplayRec ----------------------- }
PROCEDURE DisplayRec(FirstPointer:ListPointer);
{ This procedure displays the information for a specific employee. It
calls the procedure "SearchList" to search the list using the Social
Security Number of the employee. If found, the information  is displayed,
otherwise a message "not found" is issued. }

VAR
  CurrentPointer :ListPointer;
  SSNumber       :SSNstring;
  Found        :BOOLEAN;

BEGIN
 Found := FALSE;
 WRITELN(Separator);
 WRITE('Enter the SSN for the employee:'); READLN(SSNumber);
 SearchList(FirstPointer, CurrentPointer, SSNumber, Found);
 IF NOT Found THEN
    WRITELN('SSN: ', SSNumber, ' Not Found')
 ELSE
  WITH CurrentPointer^.DataField DO
   BEGIN
    WRITELN('ID: ',ID);
    WRITELN('Name: ',Name);
    WRITELN('Position: ', Position);
    WRITELN('Social Security Number: ',SSN);
    WRITELN('Hourly Rate: ',Rate:2:2)
   END;
 WRITE('Hit any key to continue...');
 READLN
END;

{ ------------------------ Procedure SaveList ------------------------ }
PROCEDURE SaveList(FirstPointer:ListPointer;
                  VAR MyListFile: EmpFile);
{This procedure saves the data fields in the linked list to a file of the
type RECORD. }

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;

{ ------------------------ Procedure ReadFile ------------------------ }
PROCEDURE ReadFile(VAR FirstPointer:ListPointer;
                VAR MyListFile: EmpFile);
{This procedure reads data from the file "emplist.bin" and adds the
data to the linked list. }

VAR
 Item :DataRecord;

BEGIN
 ASSIGN(MyListFile, FileName);
 RESET(MyListFile);
 WHILE NOT EOF (MyListFile) DO
  BEGIN
   READ(MyListFile, Item);
   BuildList(FirstPointer, Item);
  END;
 CLOSE(MyListFile)
END;

{ ------------------------- Procedure Menu --------------------------- }
PROCEDURE Menu;

VAR
  Option:INTEGER;
BEGIN
  WRITELN(Header);
  WRITELN('1. Add records to the list.');
  WRITELN('2. Display the whole list.');
  WRITELN('3. Display an employee record.');
  WRITELN('4. Add records from file.');
  WRITELN('5. Save the list to a file.');
  WRITELN('6. Exit.');
  WRITELN(Separator);
  WRITE('Make a choice and press a number: ');
  READLN(Option);
  CASE Option OF
   1: GetData(FirstPointer);
   2: DisplayItAll(FirstPointer);
   3: DisplayRec(FirstPointer);
   4: ReadFile(FirstPointer, MyListFile);
   5: SaveList(FirstPointer, MyListFile);
   6: Exit
  END;
  Menu
END;

{ --------------------------- Main Program --------------------------- }

BEGIN
{ Initialize an empty list. }
  FirstPointer:= NIL;
  menu
END.


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.