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


Sample run:

A sample of the file “emplist.bin” is included on the companion disk. When you run the program, you may start with loading records from the file by choosing option 4, then display the list using option 2. In the following sample run, the user input is italicized and bolded for clarity.

------------- Main Menu --------------
1. Add records to the list.
2. Display the whole list.
3. Display an employee record.
4. Add records from file.
5. Save the list to a file.
6. Exit.
--------------------------------------
Make a choice and press a number: 4 -----> At this point, the list is loaded
into memory.
------------- Main Menu --------------
1. Add records to the list.
2. Display the whole list.
3. Display an employee record.
4. Add records from file.
5. Save the list to a file.
6. Exit.
--------------------------------------
Make a choice and press a number:  2
--------------------------------------
The contents of the list:
  ID         Name           Position          SSN    Rate

  456      Mark Poche     Staff Assistant  999-99-9999 $23.00
  345   Deanna Bedford          Secretary I  444-44-4444 $12.55
  123  John Martin Smith     Sales Manager     111-11-1111 $22.50
  234    James Strahan   Sales Representative   222-22-2222 $11.50
  987    Charles Berlin      President 333-33-3333 $60.50

Hit any key to continue...

For your convenience, the social security numbers in the file are made easy to remember when you search for a certain employee. Here is an example:

------------- Main Menu --------------
1. Add records to the list.
2. Display the whole list.
3. Display an employee record.
4. Add records from file.
5. Save the list to a file.
6. Exit.
--------------------------------------
Make a choice and press a number: 3
--------------------------------------
Enter the SSN for the employee:  111-11-1111
ID: 123
Name: John Martin Smith
Position: Sales Manager
Social Security Number: 111-11-1111
Hourly Rate: 22.50

Hit any key to continue...
------------- Main Menu --------------
1. Add records to the list.
2. Display the whole list.
3. Display an employee record.
4. Add records from file.
5. Save the list to a file.
6. Exit.
--------------------------------------
Make a choice and press a number: 6

Notice the following points in the search procedure:

1.  The call to the search procedure takes the form:
         SearchList(FirstPointer, CurrentPointer, SSNumber, Found);

where the “SSNumber” is the social security number to be matched with the field “SSN.”
2.  Both the “CurrentPointer” and the flag “Found” are passed using the keyword VAR, because their values are expected to change after the search process.
PROCEDURE SearchList(FirstPointer:ListPointer;
       VAR CurrentPointer:ListPointer;
       SSNumber:SSNstring;
       VAR Found:BOOLEAN);

Drill 11-4

Add a procedure to the previous program in order to incorporate the “Update record” option in your menu. To update a record, search for it, accept the new information from the keyboard, and write the record to the data field in the current node. Remember to update the menu options as well by adding the option “Update a record.”

Deleting Nodes from Lists

To delete a node from a linked list, you need to declare three pointers:

• “FirstPointer” which points to the first node
• “CurrentPointer” which points to the current node
• “PreviousPointer” which points to the previous node

The algorithm to delete a node depends on its relative position in the link. There are two cases to consider:

A.  If the node is the first node in the list: The procedure to delete the node in this case is simple, and requires only the two pointers “FirstPointer” and “CurrentPointer”:
1.  Set the “CurrentPointer” to point to the node to be deleted (the first node).
2.  Set the “FirstPointer” to point to the second node in the list:
         FirstPointer:= FirstPointer^.NextField;
3.  Dispose the “CurrentPointer”:
DISPOSE(CurrentPointer);

B.  If the node has a predecessor: This is the case in which you need the third pointer that points to the previous node. The following is the algorithm to delete the node:
1.  Set the “CurrentPointer” to point to the node to be deleted.
2.  Set the “PreviousPointer” to point to the successor of the current node:
         PreviousPointer^.NextField:= CurrentPointer^.NextField;
3.  Dispose the “CurrentPointer”:
DISPOSE(CurrentPointer);

The previous steps imply that the algorithm of the search procedure must be changed so that the “PreviousPointer” follows the “CurrentPointer” step by step through the list.

This is the new version of the procedure “SearchList”:

{ ----------------------- Procedure SearchList ----------------------- }
PROCEDURE SearchList(FirstPointer:ListPointer;
          VAR CurrentPointer:ListPointer;
          VAR PreviousPointer:ListPointer;
          SSNumber:SSNstring;
          VAR Found:BOOLEAN);

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

The procedure header is changed to:

PROCEDURE SearchList(FirstPointer :ListPointer;
          VAR CurrentPointer :ListPointer;
             VAR PreviousPointer :ListPointer;
          SSNumber :SSNstring;
          VAR Found :BOOLEAN);


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.