home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / LSTS20.ZIP / LISTS20.DOC < prev   
Encoding:
Text File  |  1988-07-16  |  12.9 KB  |  487 lines

  1. Documentation for Lists.Pas (ver 2.0)
  2. Written by Mark Addleman [72777, 740]
  3.  
  4. Released into public domain
  5. July 16, 1988
  6.  
  7. Please distribute in complete form (as you received it).
  8.  
  9. Any comments, suggestions, criticisms, or anything, please send to let me know.
  10.  
  11.  
  12. The idea of a generic set of routines for a linked list came to me in a vision.  
  13. After working many long hours writing and rewriting essentially the same code 
  14. for a set of linked lists, I decided that it was time to sit down and write one 
  15. set of routines that I could use with them all (that the that it was 3:00am 
  16. helped a lot too).  I hope that these routines help your programming as much as 
  17. they have helped mine.
  18.  
  19. Lists ver 2.0 extends on the previous versions in many ways.  It includes many 
  20. new routines and subtle changes to others.  If you previously used Lists, 
  21. first, I thank you, and second I suggest you re-read this documentation for the 
  22. differences between the two versions.  
  23.  
  24. One major difference is that no external code is required for accessing the 
  25. itself.  AddToList (which was renamed to AddItem) and InsertInList (renamed to 
  26. InsertItem) now take a Size parameter as an argument.  This Size parameter is 
  27. the size of the item and it allows the item itself to be incorporated in the 
  28. list.  If this doesn't make sense, here is an example:
  29.      Program DemoList;
  30.      Uses Lists;
  31.  
  32.      Var
  33.         MyList   :  ListRec;
  34.         I        :  Byte;
  35.  
  36.      Begin
  37.         InitList(MyList);
  38.  
  39.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I);
  40.      End.
  41. No longer must you worry about the pointers and getting memory that you did in 
  42. previous versions.
  43.  
  44. Other differences include:
  45.      The following procedures have been renamed:
  46.           ListOK         becomes OK
  47.           InsertInList   becomes InsertItem
  48.           AddToList      becomes AddItem
  49.      The MoveTo______ routines have been replaced with MoveToItem
  50.      The ______ItemPtr routines have been replaced with Item
  51.      Routines for accessing a stack have been implemented
  52. ListRec   (type declaration)
  53. ---------------------------------------
  54. ListRec is the backbone of the entire package.  This is the programmer's 
  55. connection with the linked list.  It consists of 4 fields,  FirstItem (which 
  56. points to the first item in the list), LastItem (points to the last item in the 
  57. list), Item (points to the current item) and OK boolean variable which lets you 
  58. know of if errors occur while accessing the list).
  59.  
  60. To use a list in your program, you simply declare a variable as ListRec:
  61.  
  62.      Program DemoList;
  63.      Uses Lists;
  64.  
  65.      Var
  66.         MyList               :   ListRec;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. InitList(Var List:ListRec);
  74. ---------------------------------------
  75. InitList is a procedure that must be called before using any list.  InitList 
  76. sets all points to NIL and prepares the list for use.
  77.  
  78.      Program DemoList;
  79.      Uses Lists;
  80.  
  81.      Var
  82.         MyList               :   ListRec;
  83.  
  84.      Begin
  85.         InitList(MyList);
  86.      End.
  87.  
  88.  
  89.  
  90.  
  91.  
  92. AddItem(Var List:ListRec; 
  93.         Var Item; 
  94.         Size:Word);
  95. ---------------------------------------
  96. AddItem is a procedure used for putting things on the list.  The first 
  97. parameter of AddItem is the List you want to append an Item to.  The second 
  98. parameter is the Item you want to add.  And third is the size of the Item to be 
  99. added.
  100.  
  101. AddItem placed Item at the end of the list.
  102. AddItem does not move the current item pointer.
  103.  
  104.      Program DemoList;
  105.      Uses Lists;
  106.  
  107.      Var
  108.         MyList               :   ListRec;
  109.         I                    :   Byte;
  110.  
  111.      Begin
  112.         InitList(MyList);
  113.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  114.      End.
  115.  
  116. This program creates the following list:
  117.    1  2  3  4  5  6  7  8  9  10
  118.    ^
  119.    Current item
  120.  
  121.  
  122.  
  123.  
  124.  
  125. InsertItem(Var List:ListRec;
  126.            Var Item;
  127.            Size:Word;
  128.            Location:Pointer);
  129. ---------------------------------------
  130. InsertItem is another procedure for placing an item on the list.  Unlike 
  131. AddItem, InsertItem places the item immediately before the item specified in 
  132. Location.  For example:
  133.      Program DemoList;
  134.      Uses Lists;
  135.  
  136.      Var
  137.         MyList               :   ListRec;
  138.         I                    :   Byte;
  139.  
  140.      Begin
  141.         InitList(MyList);
  142.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  143.  
  144.         I:=76;
  145.         InsertItem(MyList, I, SizeOf(I), LastItem(Stack));
  146.      End.
  147.  
  148. This program would create the following list:
  149.    1  2  3  4  5  6  7  8  9  76  10
  150.    ^
  151.    Current item
  152.  
  153. See CurrentItem, FirstItem, LastItem, PrevItem, and NextItem
  154. InsertItem does not move the current item pointer.
  155. DeleteItem(Var List:ListRec;
  156.            Location:Pointer);
  157. ---------------------------------------
  158. DeleteItem deletes the item in List at Location.  For example:
  159.      Program DemoList;
  160.      Uses Lists;
  161.  
  162.      Var
  163.         MyList               :   ListRec;
  164.         I                    :   Byte;
  165.  
  166.      Begin
  167.         InitList(MyList);
  168.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  169.  
  170.         DeleteItem, I, SizeOf(I), LastItem(Stack));
  171.      End.
  172.  
  173. This program produces the following list:
  174.    1  2  3  4  5  6  7  8  9
  175.    ^
  176.    Current item
  177.  
  178. See CurrentItem, FirstItem, LastItem, NextItem, PrevItem
  179. DeleteItem does not move the current item pointer.
  180.  
  181.  
  182.  
  183.  
  184.  
  185. DeleteList(Var List:ListRec)
  186. ---------------------------------------
  187. DeleteList removes the entire List from memory and initializes the List for use 
  188. again.
  189.  
  190.  
  191.  
  192.  
  193.  
  194. CurrentItem(List:ListRec):Pointer;
  195. ---------------------------------------
  196. CurrentItem returns the location of the item currently pointed to in List.
  197.  
  198.      Program DemoList;
  199.      Uses Lists;
  200.  
  201.      Var
  202.         MyList               :   ListRec;
  203.         I                    :   Byte;
  204.  
  205.      Begin
  206.         InitList(MyList);
  207.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  208.  
  209.         I:=76;
  210.         InsertItem(MyList, I, SizeOf(I), CurrentItem(MyList));
  211.      End.
  212. This program would produce the following list:
  213.    76   1   2   3   4   5   6   7   8   9   10
  214.         ^
  215.         Current item
  216. FirstItem(List:ListRec):Pointer;
  217. ---------------------------------------
  218. FirstItem returns the location of the first item in List.
  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 AddItem(MyList, I, SizeOf(I));
  230.  
  231.         I:=76;
  232.         InsertItem(MyList, I, SizeOf(I), FirstItem;
  233.      End.
  234. This program would produce the following list:
  235.    76   1   2   3   4   5   6   7   8   9   76   10
  236.         ^
  237.         Current item
  238.  
  239.  
  240.  
  241.  
  242. LastItem(List:ListRec):Pointer;
  243. ---------------------------------------
  244. LastItem returns the location of the last item in List.
  245.  
  246.      Program DemoList;
  247.      Uses Lists;
  248.  
  249.      Var
  250.         MyList               :   ListRec;
  251.         I                    :   Byte;
  252.  
  253.      Begin
  254.         InitList(MyList);
  255.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  256.  
  257.         I:=76;
  258.         InsertItem(MyList, I, SizeOf(I), CurrentItem(MyList));
  259.      End.
  260. This program would produce the following list:
  261.    1   2   3   4   5   6   7   8   9   76   10
  262.    ^
  263.    Current item
  264. NextItem(Item:Pointer):Pointer
  265. ---------------------------------------
  266. NextItem takes CurrentItem or FirstItem as an argument and returns the 
  267. following item.  For example:
  268.  
  269.      Program DemoList;
  270.      Uses Lists;
  271.  
  272.      Var
  273.         MyList               :   ListRec;
  274.         I                    :   Byte;
  275.  
  276.      Begin
  277.         InitList(MyList);
  278.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  279.  
  280.         I:=76;
  281.         InsertItem(MyList, I, SizeOf(I), NextItem(FirstItem(MyList)));
  282.      End.
  283.  
  284. This program would produce the following list:
  285.    1   76   2   3   4   5   6   7   8   9   10
  286.    ^
  287.    Current item
  288.  
  289.  
  290.  
  291.  
  292.  
  293. PrevItem(Item:Pointer):Pointer
  294. ---------------------------------------
  295. PrevItem takes either CurrentItem or LastItem as an argument and returns the 
  296. preceding argument.
  297.  
  298.      Program DemoList;
  299.      Uses Lists;
  300.  
  301.      Var
  302.         MyList               :   ListRec;
  303.         I                    :   Byte;
  304.  
  305.      Begin
  306.         InitList(MyList);
  307.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  308.         I:=76;
  309.         InsertItem(MyList, I, SizeOf(I), NextItem(LastItem(MyList)));
  310.      End.
  311.  
  312. This program would produce the following list:
  313.    1   2   3   4   5   6   7   8   9   76   10
  314.    ^
  315.    Current item
  316. MoveToItem(Var List:ListRec;
  317.            Location:Pointer);
  318. ---------------------------------------
  319. MoveToItem the current item in List to Location.  Location can be CurrentItem, 
  320. FirstItem, or LastItem in any combination with NextItem or PrevItem.  
  321. MoveToItem will set OK to false if Location is outside of List.
  322.  
  323. For example to move the current item forward through a list until the end is 
  324. reached:
  325.  
  326.      Program DemoList;
  327.      Uses Lists;
  328.  
  329.      Var
  330.         MyList               :   ListRec;
  331.         I                    :   Byte;
  332.  
  333.      Begin
  334.         InitList(MyList);
  335.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  336.  
  337.         MoveToItem(MyList, FirstItem(MyList));
  338.         While MyList.OK Do Begin
  339.            MoveToItem(MyList, NextItem(CurrentItem(MyList));
  340.         End;
  341.      End.
  342.  
  343. This program moves backward through the list:
  344.  
  345.      Program DemoList;
  346.      Uses Lists;
  347.  
  348.      Var
  349.         MyList               :   ListRec;
  350.         I                    :   Byte;
  351.  
  352.      Begin
  353.         InitList(MyList);
  354.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  355.  
  356.         MoveToItem(MyList, LastItem(MyList));
  357.         While MyList.OK Do Begin
  358.            MoveToItem(MyList, PrevItem(CurrentItem(MyList));
  359.         End;
  360.      End.
  361.  
  362.  
  363. MoveToItem(PrevItem(LastItem(MyList))) would move the current item to the 
  364. second to the last item on the list, Where 
  365. MoveToItem(NextItem(FirstItem(MyList))) would move the current item to the 
  366. second item on the list.
  367. GetItem(Var List:ListRec;
  368.         Location:Pointer;
  369.         Var Item)
  370. ---------------------------------------
  371. GetItem is a procedure for reading an item off of the list.  The item read is 
  372. specified in Location and the item is put into Item.
  373.  
  374. For example:
  375.      Program DemoList;
  376.      Uses Lists;
  377.  
  378.      Var
  379.         MyList               :   ListRec;
  380.         I                    :   Byte;
  381.  
  382.      Begin
  383.         InitList(MyList);
  384.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  385.  
  386.         MoveToItem(MyList, FirstItem(MyList));
  387.         While MyList.OK Do Begin
  388.            GetItem(MyList, I, CurrentItem(MyList));
  389.            Write(I,' ');
  390.            MoveToItem(MyList, NextItem(CurrentItem(MyList));
  391.         End;
  392.      End.
  393.  
  394. This program would produce the following output:
  395.    1 2 3 4 5 6 7 8 9 10 
  396.  
  397.  
  398.  
  399.  
  400.  
  401. Item(List:ListRec;
  402.      Location:Pointer):Pointer
  403. ----------------------------------------
  404. Item is a function that returns the location of any item in the list.  It's 
  405. purpose is similar to GetItem, only implemented as a function.
  406.  
  407. For example:
  408.      Program DemoList;
  409.      Uses Lists;
  410.  
  411.      Var
  412.         MyList               :   ListRec;
  413.         I                    :   Byte;
  414.  
  415.      Begin
  416.         InitList(MyList);
  417.         For I:=1 To 10 Do AddItem(MyList, I, SizeOf(I));
  418.  
  419.         MoveToItem(MyList, FirstItem(MyList));
  420.         While MyList.OK Do Begin
  421.            I:=Byte(Item(MyList, CurrentItem(MyList))^);
  422.            Write(I,' ');
  423.            MoveToItem(MyList, NextItem(CurrentItem(MyList));
  424.         End;
  425.      End.
  426.  
  427. This program would produce the following output:
  428.    1 2 3 4 5 6 7 8 9 10
  429. The following procedures are implemented for the purpose of simulating a stack 
  430. or queue.  Push and Pop are implemented for a Last In First Out buffer.  Queue 
  431. and DeQueue are implemented for a First In Last Out buffer.  They can be 
  432. intermixed to produce Last In Last Out and First In First Out.
  433.  
  434.  
  435.  
  436.  
  437.  
  438. InitStack(Var Stack:ListRec)
  439. ---------------------------------------
  440. InitStack does exactly what InitList does.  It is provided for clarity.
  441.  
  442.  
  443.  
  444.  
  445.  
  446. Push(Var Stack:ListRec; 
  447.      Var Item; 
  448.      Size:Word)
  449. ---------------------------------------
  450. Push calls AddItem to place Item at the end of the list.
  451.  
  452.  
  453.  
  454.  
  455.  
  456. Pop(Var Stack:ListRec;
  457.     Var Item);
  458. ---------------------------------------
  459. Pop removes the last item the Stack and places it in Item.
  460.  
  461.  
  462.  
  463.  
  464.  
  465. Queue(Var Stack:ListRec;
  466.       Var Item;
  467.       Size:Word)
  468. ---------------------------------------
  469. Queue calls InsertItem to place Item at the beginning of the list
  470.  
  471.  
  472.  
  473.  
  474.  
  475. DeQueue(Var Stack:ListRec;
  476.         Var Item)
  477. ---------------------------------------
  478. DeQueue removes the first item on Stack and places it in Item.
  479.  
  480.  
  481.  
  482.  
  483. Again, any questions, comments, criticisms, anything, please let me know.
  484.  
  485. Mark Addleman
  486. [72777, 740]
  487.