home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / LISTS30.ZIP / LISTS30.DOC < prev   
Encoding:
Text File  |  1988-08-29  |  21.2 KB  |  641 lines

  1.   Documentation for LISTS.PAS (ver 3.0)
  2.   Written by Mark Addleman [72777, 740] for Software Applications of Wichita
  3.  
  4.   *** NOTE: This program is distributed under the ShareWare concept.  As
  5.   such, you are granted a limited evaluation period after which continued use
  6.   requires registration in accordance with the Agreement contained in
  7.   License.TXT.  A licensing fee of $15 and a signed Agreement should be sent
  8.   to the address below:
  9.  
  10.   Please send your contributions to
  11.                         Software Applications of Wichita
  12.                               2204 Winstead Circle
  13.                             Wichita, KS  67226-1122
  14.  
  15.   This document was created using Sprint:  The Professional Word Processor
  16.   and whatever printer you are using.
  17.   Updates
  18.  
  19.   Any updates which are made available will be placed on the Compuserve
  20.   Borland Forum.
  21.  
  22.  
  23.   Purpose
  24.  
  25.   LISTS is designed to relieve you of the messy details of creating and
  26.   maintaining linked lists.  Features include the ability to add, insert, and
  27.   delete items from lists.  Moving (forward and backward) through lists is
  28.   implemented, along with the capability to rearrange the order of the list.
  29.  
  30.  
  31.  
  32.   Changes from the previous version
  33.  
  34.   LISTS version 3 abounds with changes from the version 2.  Most notable
  35.   changes include a different analogy use in LISTS.  Now, a list is made up
  36.   of ENTRIES.  Each ENTRY contains an ITEM.  It is the ITEM that the
  37.   programmer is primarily interested in.  Another major change is that very
  38.   few of the routines explicitly require the list name as an argument.  For
  39.   example, to refer to the next-to-the-last entry in a list:
  40.        Entry:=PrevEntry(LastEntry(List));
  41.   The change is that PrevEntry doesn't require List as an argument.  The
  42.   advantage is that less code is required, greater memory efficiency, and
  43.   speed since the program no longer must push the name of the list onto the
  44.   stack.
  45.  
  46.   WARNING:  LISTS uses a local variable to make this possible.  Therefore,
  47.   any statement using LISTS routines must somewhere contain the name of the
  48.   list.  For example, the following code is ***** no longer valid:
  49.        Var
  50.           E       :   EntryPtr;
  51.           List    :   ListRec;
  52.  
  53.        Begin
  54.           Entry:=FirstEntry(List);
  55.           MoveToEntry(E);
  56.        End.
  57.  
  58.   The compiler will not generate an error because technically it is correct.
  59.   The problem is that LISTS will be unable to communicate with the variable
  60.   List if it had to.  The Entry function is provided to perform the same
  61.   function as the program above, but in a legal manner:
  62.        Function Entry(List:ListRec; Location:EntryPtr):EntryPtr;
  63.  
  64.   So the above program would look like this:
  65.        Var
  66.           E       :   EntryPtr;
  67.           List    :   ListRec;
  68.  
  69.        Begin
  70.           E:=FirstEntry(List);
  71.           MoveToEntry(Entry(List,E));
  72.        End.
  73.  
  74.  
  75.   Another enhancement to LISTS is the ability to access a list completely at
  76.   random using the EntryNumAbs and EntryNumRel routines.  These routines
  77.   access the absolute entry on the list (1st, 2nd, 3rd, etc) and relative
  78.   entry on the list (back one, back two, forward three, forward four, etc)
  79.   respectively.  Also, GetItem and PutItem are available for accessing the
  80.   item in the entry or changing it.
  81.   ListRec (type declaration)
  82.   ---------------------------------------------------------------------------
  83.   ListRec is the backbone of the entire LISTS package.  This is the
  84.   programmer's connection with the linked list.  The following is its type
  85.   definition in the LISTS.TPU:
  86.        ListRec   =    Record
  87.                            OK   :    Boolean;
  88.                            {Set to FALSE if an error occurs}
  89.  
  90.                            F_Entry   :    EntryPtr;
  91.                            {Points to the first entry in the list}
  92.  
  93.                            L_Entry   :    EntryPtr;
  94.                            {Points to the last entry in the list}
  95.  
  96.                            C_Entry   :    EntryPtr;
  97.                            {Points to the entry currently being referenced}
  98.                       End;
  99.  
  100.   EntryPtr is a pointer to the record EntryRec which contains the information
  101.   concerning an entry on the list.
  102.        EntryRec  =    Record
  103.                            P_Entry   :    EntryPtr;
  104.                            {Points to the previous entry}
  105.  
  106.                            N_Entry   :    EntryPtr;
  107.                            {Points to the next entry}
  108.  
  109.                            Size      :    Word;
  110.                            {Size in bytes of the item in this entry}
  111.  
  112.                            Item      :    Byte
  113.                            {The item in this entry}
  114.                       End;
  115.  
  116.   Item is only 1 byte in length.  The reason that Item is only a byte is that
  117.   Item actually becomes part of the Entry data structure.  Whenever an item
  118.   is added to the list, the size is taken as an argument and LISTS allocates
  119.   enough memory for the EntryRec plus the size of the item.  The Item is then
  120.   copied in memory to the end of the entry using Turbo's Move procedure.  If
  121.   this isn't clear, refer to the GetItem and PutItem procedures in the source
  122.   code.
  123.  
  124.  
  125.  
  126.   InitList(Var List:ListRec)
  127.   ---------------------------------------------------------------------------
  128.   InitList must be called for each list before using.  InitList sets all
  129.   pointers to NIL and prepares the list for processing.
  130.  
  131.        Program DemoList;
  132.        Uses Lists;
  133.  
  134.        Var
  135.             MyList              :    ListRec;
  136.  
  137.        Begin
  138.             InitList(MyList);
  139.        End.
  140.  
  141.  
  142.  
  143.   AddEntry(Var List:ListRec; Var Item; Size:Word);
  144.   ---------------------------------------------------------------------------
  145.   AddEntry is used for tacking entries onto the end of List.  AddEntry also
  146.   takes an Item and its Size (using the SizeOf procedure usually) as
  147.   arguments and places them in the entry.
  148.  
  149.   AddEntry places the new entry at the end of List.
  150.   AddEntry does not move the current entry pointer.
  151.  
  152.        Program DemoList;
  153.        Uses Lists;
  154.  
  155.        Var
  156.             MyList              :    ListRec;
  157.             I                   :    Byte;
  158.  
  159.        Begin
  160.             InitList(MyList);
  161.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  162.        End.
  163.  
  164.   This program creates the following list:
  165.        1    2    3    4    5    6    7    8    9    10
  166.        ^
  167.        Current entry
  168.  
  169.  
  170.  
  171.   InsertEntry(Location:EntryPtr; Var Item; Size:Word)
  172.   ---------------------------------------------------------------------------
  173.   InsertEntry is another procedure for placing an entry in a list.  Unlike
  174.   AddEntry, however, InsertEntry places the entry immediately before the
  175.   entry specified in Location.
  176.  
  177.   InsertEntry does not move the current entry pointer.
  178.  
  179.   For example:
  180.        Program DemoList;
  181.        Uses Lists;
  182.  
  183.        Var
  184.             MyList              :    ListRec;
  185.             I                   :    Byte;
  186.  
  187.        Begin
  188.             InitList(MyList);
  189.             For I:=1e To 10 Do AddEntry(MyList, I, SizeOf(I));
  190.  
  191.             I:=76;
  192.             InsertEntry(LastEntry(MyList), I, SizeOf(I));
  193.        End.
  194.  
  195.   This program would create the following list:
  196.        1    2    3    4    5    6    7    8    9    76   10
  197.        ^
  198.        Current entry
  199.  
  200.   NOTE: InsertEntry does not take the name of the list as an argument (MyList
  201.   in the above example).  It is the programmer's responsibility to ensure
  202.   that somewhere in the InsertEntry expression the name of the list is
  203.   referenced.
  204.  
  205.  
  206.  
  207.   DeleteEntry(Location:EntryPtr)
  208.   ---------------------------------------------------------------------------
  209.   DeleteEntry deletes the entry in a list at Location.   DeleteEntry moves the current entry pointer in only one circumstance:  when
  210.   the entry to be deleted is the entry currently being referenced.  In this
  211.   case, the current entry is moved to the next entry.  However, if the
  212.   current entry is the last entry and it is to be deleted, the current entry
  213.   will point to the new last entry.
  214.  
  215.   NOTE: DeleteEntry does not take the name of the list as an argument (MyList
  216.   in the above example).  It is the programmer's responsibility to ensure
  217.   that somewhere in the InsertEntry expression the name of the list is
  218.   referenced.
  219.  
  220.        Program DemoList;
  221.        Uses Lists;
  222.  
  223.        Var
  224.             MyList              :    ListRec;
  225.             I                   :    Byte;
  226.  
  227.        Begin
  228.             InitList(MyList);
  229.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  230.  
  231.             DeleteEntry(LastEntry(MyList));
  232.        End.
  233.  
  234.   This program produces the following list:
  235.        1    2    3    4    5    6    7    8    9
  236.        ^
  237.        Current entry
  238.  
  239.   NOTE: DeleteEntry does not take the name of the list as an argument (MyList
  240.   in the above example).  It is the programmer's responsibility to ensure
  241.   that somewhere in the DeleteEntry expression the name of the list is
  242.   referenced.
  243.  
  244.  
  245.  
  246.  
  247.   DeleteList(Var List:ListRec)
  248.   ---------------------------------------------------------------------------
  249.   DeleteList removes the entire List from memory and initializes List for
  250.   use.
  251.  
  252.  
  253.  
  254.   CurrentEntry(List:ListRec):Pointer
  255.   ---------------------------------------------------------------------------
  256.   CurrentEntry returns the a pointer to the entry currently being referenced
  257.   in List.
  258.  
  259.        Program DemoList;
  260.        Uses Lists;
  261.  
  262.        Var
  263.             MyList              :    ListRec;
  264.             I                   :    Byte;
  265.  
  266.        Begin
  267.             InitList(MyList);
  268.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  269.  
  270.             I:=76;
  271.             InsertEntry(CurrentEntry(MyList), I, SizeOf(I));
  272.        End.
  273.  
  274.   This program would produce the following list:        76   1    2    3    4    5    6    7    8    9    10
  275.             ^
  276.             Current entry
  277.  
  278.  
  279.  
  280.   FirstEntry(Var List:ListRec):Pointer;
  281.   ---------------------------------------------------------------------------
  282.   FirstEntry returns the location of the first entry in List.
  283.  
  284.        Program DemoList;
  285.        Uses Lists;
  286.  
  287.        Var
  288.             MyList              :    ListRec;
  289.             I                   :    Byte;
  290.  
  291.        Begin
  292.             InitList(MyList);
  293.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  294.  
  295.             I:=76;
  296.             InsertEntry(FirstEntry(MyList), I, SizeOf(I));
  297.        End.
  298.  
  299.   This program would produce the following list:
  300.        76   1    2    3    4    5    6    7    8    9    10
  301.             ^
  302.             Current entry
  303.  
  304.  
  305.  
  306.   LastEntry(Var List:ListRec)
  307.   ---------------------------------------------------------------------------
  308.   LastEntry returns the location of the last entry in List.
  309.  
  310.        Program DemoList;
  311.        Uses Lists;
  312.  
  313.        Var
  314.             MyList              :    ListRec;
  315.             I                   :    Byte;
  316.  
  317.        Begin
  318.             InitList(MyList);
  319.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  320.  
  321.             I:=76;
  322.             InsertEntry(LastEntry(MyList), I, SizeOf(I));
  323.        End.
  324.  
  325.   This program would produce the following list:
  326.        1    2    3    4    5    6    7    8    9    76   10
  327.        ^
  328.        Current entry
  329.  
  330.  
  331.  
  332.   NextEntry(Location:EntryPtr):EntryPtr
  333.   ---------------------------------------------------------------------------
  334.   NextEntry returns the location of the entry immediately after Location.
  335.   Normally location is either CurrentEntry or FirstEntry.  If LastEntry is
  336.   used as an argument, a NIL is returned.
  337.  
  338.        Program DemoList;
  339.        Uses Lists;        Var
  340.             MyList              :    ListRec;
  341.             I                   :    Byte;
  342.  
  343.        Begin
  344.             InitList(MyList);
  345.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  346.  
  347.             I:=76;
  348.             InsertEntry(NextEntry(FirstEntry(MyList)), I, SizeOf(I));
  349.        End.
  350.  
  351.   This program would produce the following list:
  352.        1    2    3    4    5    6    7    8    9    76   10
  353.        ^
  354.        Current entry
  355.  
  356.   NOTE: NextEntry does not take the name of the list as an argument (MyList
  357.   in the above example).  It is the programmer's responsibility to ensure
  358.   that somewhere in the NextEntry expression the name of the list is
  359.   referenced.
  360.  
  361.  
  362.  
  363.   PrevEntry(Location:EntryPtr):EntryPtr
  364.   ---------------------------------------------------------------------------
  365.   PrevEntry returns the entry immediately preceding Location.  Normally,
  366.   PrevEntry takes CurrentEntry or LastEntry as arguments.  If FirstEntry is
  367.   used, a NIL pointer is returned.
  368.        Program DemoList;
  369.        Uses Lists;
  370.  
  371.        Var
  372.             MyList              :    ListRec;
  373.             I                   :    Byte;
  374.  
  375.        Begin
  376.             InitList(MyList);
  377.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  378.  
  379.             I:=76;
  380.             InsertEntry(PrevEntry(LastEntry(MyList)), I, SizeOf(I));
  381.        End.
  382.  
  383.   This program would procedure the following list:
  384.        1    2    3    4    5    6    7    8    76   9    10
  385.        ^
  386.        Current entry
  387.  
  388.   NOTE: NextEntry does not take the name of the list as an argument (MyList
  389.   in the above example).  It is the programmer's responsibility to ensure
  390.   that somewhere in the NextEntry expression the name of the list is
  391.   referenced.
  392.  
  393.  
  394.  
  395.   MoveToEntry(Location:EntryPtr)
  396.   ---------------------------------------------------------------------------
  397.   MoveToEntry moves the current entry in a list to Location.
  398.  
  399.   MoveToEntry sets OK to FALSE if an attempt is made to move before the
  400.   FirstEntry or after the LastEntry.
  401.        Program DemoList;
  402.        Uses Lists;
  403.  
  404.        Var             MyList              :    ListRec;
  405.             I                   :    Byte;
  406.  
  407.        Begin
  408.             InitList(MyList);
  409.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  410.  
  411.             MoveToEntry(PrevEntry(LastEntry(MyList)));
  412.        End.
  413.  
  414.   This program would produce the following list:
  415.        1    2    3    4    5    6    7    8    9    10
  416.                                                ^
  417.                                                Current entry
  418.  
  419.   NOTE: MoveToEntry does not take the name of the list as an argument (MyList
  420.   in the above example).  It is the programmer's responsibility to ensure
  421.   that somewhere in the MoveToEntry expression the name of the list is
  422.   referenced.
  423.  
  424.  
  425.  
  426.   EntryNumAbs(Var List:ListRec; N:LongInt)
  427.   ---------------------------------------------------------------------------
  428.   EntryNumAbs returns the Nth entry in List.
  429.  
  430.   If N is zero or less, the current entry is returned.
  431.  
  432.        Program DemoList;
  433.        Uses Lists;
  434.  
  435.        Var
  436.             MyList              :    ListRec;
  437.             I                   :    Byte;
  438.  
  439.        Begin
  440.             InitList(MyList);
  441.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  442.  
  443.             MoveToEntry(EntryNumAbs(MyList, 3)));
  444.        End.
  445.  
  446.   This program would produce the following list:
  447.        1    2    3    4    5    6    7    8    9    10
  448.                  ^
  449.                  Current entry
  450.  
  451.  
  452.  
  453.   EntryNumRel(Location:EntryPtr; N:LongInt):EntryPtr;
  454.   ---------------------------------------------------------------------------
  455.   EntryNumRel returns the entry N positions relative to Location.  If N is
  456.   negative, EntryNumRel returns the Nth entry preceding Location.  If N is
  457.   positive, EntryNumRel returns the Nth entry after Location.
  458.  
  459.  
  460.        Program DemoList;
  461.        Uses Lists;
  462.  
  463.        Var
  464.             MyList              :    ListRec;
  465.             I                   :    Byte;
  466.  
  467.        Begin
  468.             InitList(MyList);
  469.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));             MoveToEntry(EntryNumRel(NextEntry(FirstEntry(MyList), 2)));
  470.        End.
  471.  
  472.   This program produces the following list:
  473.        1    2    3    4    5    6    7    8    9    10
  474.                       ^
  475.                       Current entry
  476.  
  477.  
  478.  
  479.   Item(Location:EntryPtr):Pointer
  480.   ---------------------------------------------------------------------------
  481.   Item returns a pointer to the Item in the Entry at Location.
  482.  
  483.        Program DemoList;
  484.        Uses Lists;
  485.  
  486.        Var
  487.             MyList              :    ListRec;
  488.             I                   :    Byte;
  489.  
  490.        Begin
  491.             InitList(MyList);
  492.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  493.  
  494.             MoveToEntry(FirstEntry(MyList));
  495.             While MyList.OK Do Begin
  496.                  Write(Word(Item(CurrentEntry(MyList))^),' ');
  497.                  MoveToEntry(NextEntry(CurrentEntry(MyList)));
  498.             End;
  499.        End.
  500.  
  501.   This program would produce the following output:
  502.        1    2    3    4    5    6    7    8    9    10
  503.  
  504.   NOTE: Item does not take the name of the list as an argument (MyList in the
  505.   above example).  It is the programmer's responsibility to ensure that
  506.   somewhere in the Item expression the name of the list is referenced.
  507.  
  508.  
  509.  
  510.   Entry(Var List:ListRec; Location:EntryPtr):EntryPtr
  511.   ---------------------------------------------------------------------------
  512.   The purpose of Entry is to return the a pointer to the location of
  513.   Location.  Entry is needed because in some cases, it is the only way for
  514.   LISTS to know what list is being used.
  515.  
  516.        Program DemoList;
  517.        Uses Lists;
  518.  
  519.        Var
  520.             MyList              :    ListRec;
  521.             I                   :    Byte;
  522.             E                   :    EntryPtr;
  523.  
  524.        Begin
  525.             InitList(MyList);
  526.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  527.  
  528.             E:=PrevEntry(PrevEntry(LastEntry(MyList)));
  529.             MoveToEntry(Entry(MyList, E));
  530.        End.
  531.   This program would produce the following list:
  532.        1    2    3    4    5    6    7    8    9    10
  533.                                           ^
  534.                                           Current entry
  535.  
  536.  
  537.  
  538.   GetItem(Location:EntryPtr; Var _Item);
  539.   ---------------------------------------------------------------------------
  540.   GetItem copies the Item at Location to _Item.  The size is automatically
  541.   taken care of.
  542.  
  543.        Program DemoList;
  544.        Uses Lists;
  545.  
  546.        Var
  547.             MyList              :    ListRec;
  548.             I                   :    Byte;
  549.             E                   :    EntryPtr;
  550.  
  551.        Begin
  552.             InitList(MyList);
  553.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  554.  
  555.             MoveToEntry(LastEntry(MyList));
  556.             While MyList.OK Do Begin
  557.                  GetItem(CurrentEntry(MyList),I);
  558.                  Write(I,' ');
  559.                  MoveToEntry(PrevEntry(CurrentEntry(MyList)));
  560.             End;
  561.        End.
  562.  
  563.   This program would produce the following output:
  564.        10   9    8    7    6    5    4    3    2    1
  565.  
  566.   NOTE: Item does not take the name of the list as an argument (MyList in the
  567.   above example).  It is the programmer's responsibility to ensure that
  568.   somewhere in the Item expression the name of the list is referenced.
  569.  
  570.  
  571.  
  572.   PutItem(Location:EntryPtr; Var _Item)
  573.   ---------------------------------------------------------------------------
  574.   PutItem replaces the item at Location with _Item.  PutItem does not take
  575.   Size as an argument, _Item MUST be the same size as the previous Item.  The
  576.   only way to change a size is to first DeleteEntry and then InsertEntry.
  577.  
  578.        Program DemoList;
  579.        Uses Lists;
  580.  
  581.        Var
  582.             MyList              :    ListRec;
  583.             I                   :    Byte;
  584.  
  585.        Begin
  586.             InitList(MyList);
  587.             For I:=1 To 10 Do AddEntry(MyList, I, SizeOf(I));
  588.  
  589.             I:=76;
  590.             PutItem(LastEntry(MyList), I);
  591.        End.
  592.  
  593.   This program would produce the following list:
  594.        1    2    3    4    5    6    7    8    9    76
  595.        ^
  596.        Current entry   The following procedures are implemented for the purpose of simulating a
  597.   stack or queue.  Push and Pop are intended for Last In First Out routines
  598.   whereas Queue and DeQueue designed for a First In Last Out buffer.  These
  599.   four routines can be intermixed to produce Last In Last Out and First In
  600.   First Out buffers.
  601.  
  602.  
  603.  
  604.   InitStack(Var Stack:ListRec)
  605.   ---------------------------------------------------------------------------
  606.   InitStack does exactly what InitList does.  It is provided for clarity of
  607.   code.
  608.  
  609.  
  610.  
  611.   Push(Var Stack:ListRec; Var Item; Size:Word)
  612.   ---------------------------------------------------------------------------
  613.   Push places Item at the top of Stack.
  614.  
  615.  
  616.  
  617.   Pop(Var Stack:ListRec; Var Item)
  618.   ---------------------------------------------------------------------------
  619.   Pop removes the top item on Stack and places it in Item.
  620.  
  621.  
  622.  
  623.   Queue(Var Stack:ListRec; Var Item; Size:Word)
  624.   ---------------------------------------------------------------------------
  625.   Queue places Item at the bottom of Stack.
  626.  
  627.  
  628.  
  629.   DeQueue(Var Stack:ListRec; Var Item)
  630.   ---------------------------------------------------------------------------
  631.   DeQueue removes the bottom item on Stack and places it in Item.
  632.  
  633.  
  634.  
  635.  
  636.                       ***** I hope you find LISTS useful.
  637.                 Again, if you have anything to ask let me know.
  638.  
  639.                                  Mark Addleman
  640.                                  [72777, 740]
  641.