Click Here!
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


Example: A Linked List Demo

In 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:

• “FirstPointer” which points to the first node
• “GetData” to accept data from the keyboard
• “BuildList” to create and add nodes to the list
• “ReadList” to read the contents of the list
• “DisplayInfo” to display the list on the screen
{ ------------------------- 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:

1.  The VAR keyword is used in the procedure “BuildList,” as it updates the direction of the “FirstPointer” with the statement
         FirstPointer:= ToolPointer;

The procedure “GetData” does not update the “FirstPointer” explicitly, but passes it to the procedure “BuildList,” therefore, the VAR keyword still has to be used.
2.  Note that the empty list is initialized only once in the main program:
         FirstPointer:= NIL;

This means that you can keep adding items to the same list if you choose option “1” more than once. The list is reinitialized only if you exit and start over. If you like to initialize an empty list each time you choose option “1,” then move the statement “FirstPointer:= NIL” to the “GetData” procedure.
3.  Although three pointers were used in the program, the procedure NEW is used only with the “ToolPointer.” This procedure is only needed to allocate memory when nodes are created.


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.