|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
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:
Deleting Nodes from ListsTo delete a node from a linked list, you need to declare three pointers:
The algorithm to delete a node depends on its relative position in the link. There are two cases to consider:
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);
|
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. |