|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: A Linked List DemoIn the following program, you build a linked list which stores names of people, then read it and display its contents. The program contains the following procedures:
{ ------------------------- figure 11-13 ----------------------------- } PROGRAM LinkedListDemo(INPUT, OUTPUT); CONST Header = '------------- Main Menu --------------'; Separator = '--------------------------------'; TYPE DataString = STRING[30]; ListPointer = ^ListRecord; ListRecord = RECORD DataField :DataString; NextField :ListPointer END; VAR FirstPointer:ListPointer; { ------------------------ Procedure BuildList ----------------------- } PROCEDURE BuildList(VAR FirstPointer:ListPointer; DataItem :DataString); {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); VAR CurrentPointer :ListPointer; BEGIN CurrentPointer := FirstPointer; WHILE CurrentPointer <> NIL DO BEGIN WRITELN(CurrentPointer^.DataField); CurrentPointer := CurrentPointer^.NextField END; WRITELN END; { ----------------------- Procedure GetData--------------------------- } PROCEDURE GetData(VAR FirstPointer:ListPointer); {Note: The "FirstPointer" is passed using the VAR keyword as it will be updated when passed to "BuildList" procedure.} VAR Name :DataString; BEGIN WRITELN('Enter the names to be added to the list,', ' when finished hit ENTER.'); { Read the first data item } READLN(Name); { Check for end-of-data } WHILE LENGTH(Name) <> 0 DO BEGIN BuildList(FirstPointer, Name); READLN(Name) END END; { ---------------------- Procedure DisplayInfo ----------------------- } PROCEDURE DisplayInfo(FirstPointer:ListPointer); BEGIN WRITELN(Separator); WRITELN('The contents of the list: '); ReadList(FirstPointer); WRITE('Hit any key to continue...'); READLN END; { ------------------------ Procedure Menu ---------------------------- } PROCEDURE Menu; VAR Option :INTEGER; BEGIN WRITELN(Header); WRITELN('1. Store data in a list.'); WRITELN('2. Display the list.'); WRITELN('3. Exit.'); WRITELN(Separator); WRITE('Make a choice and press a number: '); READLN(Option); CASE Option OF 1: GetData(FirstPointer); 2: DisplayInfo(FirstPointer); 3: Exit END; Menu END; { -------------------------- Main Program ---------------------------- } BEGIN { Initialize an empty list } FirstPointer := NIL; menu END. Sample run: When you run this program and choose to store data in a list (option 1), you are asked to enter some names; when you finish just hit Enter (without writing any text). At this point, the name list is built into memory and may be displayed. Notice that the last name you entered from the keyboard appears first on the screen, because you always insert nodes at the beginning of the list. In this sample run, the data entered by the user are italicized and bolded for clarity. ------------- Main Menu -------------- 1. Store data in a list. 2. Display the list. 3. Exit. -------------------------------------- Make a choice and press a number:1 Enter the names to be add to the list, when finished hit ENTER. John Smith <ENTER> -----> Names entered form the keyboard Jean Murdock <ENTER> -----> Sally Bedford <ENTER> -----> Deanna Loerwold <ENTER> -----> <ENTER> ------------- Main Menu -------------- 1. Store data in a list. 2. Display the list. 3. Exit. -------------------------------------- Make a choice and press a number:2 -------------------------------------- The contents of the list: Deanna Loerwold -----> Notice the sequence of names Sally Bedford -----> Jean Murdock -----> John Smith -----> Hit any key to continue... ------------- Main Menu -------------- 1. Store data in a list. 2. Display the list. 3. Exit. -------------------------------------- Make a choice and press a number: 3 Notice the following in the program:
|
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. |