Got something to write about? Check out our Article Builder.

View SAMPLES\LISTTEST.PAS

Portable ISO Standard Pascal in C, version 3.8

Submitted By: WEBMASTER
Rating: starstarstarstar (Rate It)


(*$c+*)
PROGRAM listtest;
TYPE List_ptr = ^Node;
     Node = RECORD
             Number : Integer;
             Next : List_ptr
            END;
VAR First, Current : List_ptr;
           i : Integer;
           Marker : ^Integer;
BEGIN
 { NOTE: UPPERCASE IS NOT NECESSARY; I JUST LIKE TO USE IT. }
 { --------------------------------------------------------- }
 { In this system, whole blocks of memory are allocated }
 { and deallocated by MARK(^Integer) and RELEASE(^Integer). }
 { MARK() saves the heap state as an Integer pointer BEFORE }
 { allocation using NEW() begins, and RELEASE() restores the }
 { heap to that state after the memory allocated by NEW() is }
 { no longer needed. Note that MARK() and RELEASE() do not }
 { have to appear in the same procedure. All that is needed }
 { is for them to have the same Integer pointer parameter. }
 { ---------------------------------------------------------- }
 { Save the heap address in Marker: }
 MARK(Marker);
 { Initialize the list and its pointers: }
 NEW(First); First^.Next := NIL;
 Current := First;
 FOR i := 1 TO 30 DO
  BEGIN
   { Add each number to the list; then add a node: }
   Current^.Number := i;
   NEW(Current^.Next);
   Current := Current^.Next;
   Current^.Next := NIL
  END;
 Current := First;
 WHILE Current^.Next <> NIL DO
  BEGIN
   { Write out the node value: }
   WRITE('Current Number: ', Current^.Number : 2,
        '; Current^.Next ','is ');
   First := Current^.Next;
   IF Current^.Next <> NIL THEN WRITE('not ');
   WRITE('nil. Current^.Next^.Next is ');
   IF Current^.Next^.Next <> NIL THEN WRITE('not ');
   WRITELN('nil.');
   { USE OF RELEASE() BELOW MAKES THIS INSTANCE }
   { OF DISPOSE() UNNECESSARY, BUT NOT WRONG: }
   DISPOSE(Current);
   Current := First
  END;
 { RETURN ALL OF THE NEW() NODES TO THE HEAP IN ONE }
 { OPERATION. DISPOSE() ABOVE HAS ALREADY DONE THIS }
 { WORK ONE NODE AT A TIME. IN THIS EXAMPLE, EITHER }
 { DISPOSE() OR RELEASE() IS REDUNDANT, BUT NOT BOTH: }
 RELEASE(Marker)
END.

corner
© 1996-2008. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin.
bootstrapLabs Logo A bootstrapLabs project.