home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / pastrans.zip / SAMPLES / LISTTEST.PAS < prev    next >
Pascal/Delphi Source File  |  1989-10-15  |  2KB  |  58 lines

  1. (*$c+*)
  2. PROGRAM listtest;
  3. TYPE List_ptr = ^Node;
  4.      Node = RECORD
  5.          Number : Integer;
  6.          Next : List_ptr
  7.         END;
  8. VAR First, Current : List_ptr;
  9.        i : Integer;
  10.        Marker : ^Integer;
  11. BEGIN
  12.  { NOTE: UPPERCASE IS NOT NECESSARY; I JUST LIKE TO USE IT. }
  13.  { --------------------------------------------------------- }
  14.  { In this system, whole blocks of memory are allocated }
  15.  { and deallocated by MARK(^Integer) and RELEASE(^Integer). }
  16.  { MARK() saves the heap state as an Integer pointer BEFORE }
  17.  { allocation using NEW() begins, and RELEASE() restores the }
  18.  { heap to that state after the memory allocated by NEW() is }
  19.  { no longer needed. Note that MARK() and RELEASE() do not }
  20.  { have to appear in the same procedure. All that is needed }
  21.  { is for them to have the same Integer pointer parameter. }
  22.  { ---------------------------------------------------------- }
  23.  { Save the heap address in Marker: }
  24.  MARK(Marker);
  25.  { Initialize the list and its pointers: }
  26.  NEW(First); First^.Next := NIL;
  27.  Current := First;
  28.  FOR i := 1 TO 30 DO
  29.   BEGIN
  30.    { Add each number to the list; then add a node: }
  31.    Current^.Number := i;
  32.    NEW(Current^.Next);
  33.    Current := Current^.Next;
  34.    Current^.Next := NIL
  35.   END;
  36.  Current := First;
  37.  WHILE Current^.Next <> NIL DO
  38.   BEGIN
  39.    { Write out the node value: }
  40.    WRITE('Current Number: ', Current^.Number : 2,
  41.     '; Current^.Next ','is ');
  42.    First := Current^.Next;
  43.    IF Current^.Next <> NIL THEN WRITE('not ');
  44.    WRITE('nil. Current^.Next^.Next is ');
  45.    IF Current^.Next^.Next <> NIL THEN WRITE('not ');
  46.    WRITELN('nil.');
  47.    { USE OF RELEASE() BELOW MAKES THIS INSTANCE }
  48.    { OF DISPOSE() UNNECESSARY, BUT NOT WRONG: }
  49.    DISPOSE(Current);
  50.    Current := First
  51.   END;
  52.  { RETURN ALL OF THE NEW() NODES TO THE HEAP IN ONE }
  53.  { OPERATION. DISPOSE() ABOVE HAS ALREADY DONE THIS }
  54.  { WORK ONE NODE AT A TIME. IN THIS EXAMPLE, EITHER }
  55.  { DISPOSE() OR RELEASE() IS REDUNDANT, BUT NOT BOTH: }
  56.  RELEASE(Marker)
  57. END.
  58.