home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / LISTS12.ZIP / LISTS.DOC next >
Encoding:
Text File  |  1993-01-04  |  11.5 KB  |  418 lines

  1. Documentation for Lists.Pas (ver 1.0)
  2. Written by Mark Addleman [72777,740]
  3.  
  4.      This  set  of routines is released into  the  public  domain 
  5. under the concept of NameWare.  The NameWare ideal is simple, the 
  6. user  has full privileges to this software and can use it  anyway 
  7. he/she  feels  like.  The user also has the right  to  distribute 
  8. this software to whomever he/she feels like; as long as it is  in 
  9. its  original  form.  This means the author has  worked  hard  on 
  10. these routines and would really like some credit for his work.
  11.  
  12.                                    Thanks,
  13.                                    Mark Addleman
  14.  
  15.  
  16.  
  17.      The idea of a generic set of routines for a linked list came
  18. to  me  in a vision.  After working many long hours  writing  and
  19. rewriting essentially the same routines for a set of linked lists
  20. that  I  had to use, I decided that it was time to sit  down  and
  21. write one set of routines that I could use for them all (I'm sure
  22. the  fact that it was 3:00am didn't hurt much, either).   I  hope
  23. that these will help you as much as they have simplified my life.
  24. Suggestions are welcome.
  25.  
  26.  
  27.  
  28. ListRec  (type declaration)
  29. ---------------------------------------
  30. ListRec  is  the  backbone of the entire package.   This  is  the 
  31. programmer's  connection with the linked list.  It consists of  4 
  32. fields,  FirstItem (which points to the first item in the  list), 
  33. LastItem  (points to the last item in the list), Item (points  to 
  34. the  item  currently  being referenced), and  ListOK  (a  boolean 
  35. variable  which lets you know of any errors in using  the  list).  
  36. To  use  a  list  in your program, you  would  simply  declare  a 
  37. variable as ListRec:
  38.  
  39.      Program DemoList;
  40.      Uses Lists;
  41.  
  42.      Var
  43.         MyList               :   ListRec;
  44.  
  45.  
  46.  
  47. InitList(Var List:ListRec)
  48. ---------------------------------------
  49. InitList  is a procedure that must be called prior to  using  any 
  50. list.  InitList will set all pointers to NIL and prepare the list 
  51. for use.
  52.  
  53.      Program DemoList;
  54.      Uses List;
  55.  
  56.      Var
  57.         MyList               :   ListRec;
  58.  
  59.      Begin
  60.         InitList(MyList);
  61.      End.
  62.  
  63.  
  64.  
  65. AddToList(NewItem:Pointer; 
  66.           Var List:ListRec);
  67. ---------------------------------------
  68. AddToList  is one of two procedures to use for putting things  on 
  69. your list.  AddToList first takes the argument of a pointer which 
  70. should point to the item you want to add.  The second argument is 
  71. the list that you want to add it.  AddToList will stick this item 
  72. at the end of the list, thus the most recent addition to the list 
  73. will be the last; second most recent, second to list and so on.  
  74. AddToList does not move the current item pointer.
  75. It  is  important  to note that the items  in  ListRec  are  only 
  76. pointers  to  the actual items on the list, therefore  each  item 
  77. that  you  want  to place on the list  must  have  an  associated 
  78. pointer variable.
  79.  
  80.      Program DemoList;
  81.      Uses List;
  82.  
  83.      Var
  84.         MyList               :   ListRec;
  85.         Item                 :   ^Byte;
  86.         I                    :   Byte;
  87.  
  88.      Begin
  89.         InitList(MyList);
  90.         
  91.         For I:=1 To 10 Do Begin
  92.            New(Item);
  93.            Item^:=I;
  94.  
  95.            AddToList(Item, List);
  96.         End;
  97.      End.
  98.  
  99. The  above  program  would  produce  a  list  of  10  numbers  in 
  100. consecutive order.
  101.  
  102.  
  103.  
  104. InsertInList(NewItem:Pointer;
  105.              Var List:ListRec);
  106. ---------------------------------------
  107. InsertInList is the second method of placing an item on the list.  
  108. Unlike   AddToList,   InsertInList  will  place  the   new   item 
  109. immediately preceding the current item on the list.
  110.  
  111. InsertInList also does not move the current item pointer.
  112.  
  113.      Program DemoList;
  114.      Uses List;
  115.  
  116.      Var
  117.         MyList               :   ListRec;
  118.         Item                 :   ^Byte;
  119.         I                    :   Byte;
  120.  
  121.      Begin
  122.         InitList(MyList);
  123.         
  124.         For I:=1 To 10 Do Begin
  125.            New(Item);
  126.            Item^:=I;
  127.  
  128.            AddToList(Item, MyList);
  129.         End;
  130.  
  131.         New(Item);
  132.         Item^:=0;
  133.         InsertInList(Item, MyList);
  134.      End.
  135.  
  136. The above program would produce the following list:
  137.      0  1  2  3  4  5  6  7  8  9  10
  138.  
  139.  
  140.  
  141. DeleteItemFromList(Var List:ListRec);
  142. ---------------------------------------
  143. DeleteItemFromList  does just that.  It deletes the item that  is 
  144. currently  being  pointed to.  The rest of the list is  moved  up 
  145. one.  For example, the following list:
  146.      0  1  2  3  4  5
  147.           ^
  148.           Currently pointed to
  149.  
  150. After DeleteItemFromList, the list would look like:
  151.      0   1  3  4  5
  152.             ^
  153.             Currently pointed to
  154.  
  155.  
  156.  
  157. NextItemPtr(List:ListRec):Pointer
  158. ---------------------------------------
  159. NextItemPtr is a function that returns the a pointer to the  next 
  160. item  in  the list.  If there isn't a next item (already  at  the 
  161. last item on the list), NextItemPtr will return NIL.
  162.  
  163. For example, if you had the list:
  164.      0  1  2  3  4  5
  165.               ^
  166.               Currently pointed to
  167.  
  168. NextItemPtr  would  return the pointer to 4.   However,  in  this 
  169. instance, NextItemPtr would return NIL.
  170.      0  1  2  3  4  5
  171.                     ^
  172.                     Currently pointed to
  173.  
  174.  
  175.  
  176. PrevItemPtr(List:ListRec):Pointer
  177. ---------------------------------------
  178. PrevItemPtr  returns a pointer to the previous item on the  list.  
  179. It  works just like NextItemPtr, except it returns NIL  when  you 
  180. are at the first item of the list.
  181.  
  182.  
  183.  
  184. LastItemPtr(List:ListRec):Pointer
  185. ---------------------------------------
  186. LastItemPtr returns a pointer to the last item on the list.
  187.  
  188.  
  189.  
  190. FirstItemPtr(List:ListRec):Pointer
  191. ---------------------------------------
  192. FirstItemPtr returns a pointer to the first item on the list (big 
  193. surprise, right?).
  194.  
  195.  
  196.  
  197. CurrentItemPtr(List:ListRec):Pointer
  198. ---------------------------------------
  199. CurrentItemPtr returns a pointer to (Think quickly....) the  item 
  200. that  is  currently being pointed to.  Type  Casting  (see  Turbo 
  201. Pascal  manual) is of the greatest help when using any  of  these 
  202. functions.     As    NextItemPtr,    PrevItemPtr,    LastItemPtr, 
  203. FirstItemPtr,  and CurrentItemPtr return a generic pointer  type, 
  204. you can type cast it into any type you wish.
  205.  
  206. For example:
  207.      Program DemoList;
  208.      Uses List;
  209.  
  210.      Var
  211.         MyList               :   ListRec;
  212.         Item                 :   ^Byte;
  213.         I                    :   Byte;
  214.  
  215.      Begin
  216.         InitList(MyList);
  217.         
  218.         For I:=1 To 10 Do Begin
  219.            New(Item);
  220.            Item^:=I;
  221.  
  222.            AddToList(Item, MyList);
  223.         End;
  224.  
  225.         New(Item);
  226.         Item^:=0;
  227.         InsertInList(Item, MyList);
  228.  
  229.         Writeln(Byte(CurrentItemPtr(MyList)^));
  230.      End.
  231.  
  232. This  program  would  output  '0'.   Notice  that  CurrentItemPtr 
  233. returns  the address of the item and CurrentItemPtr^ is the  form 
  234. used in the type cast.
  235.  
  236.  
  237.  
  238. MoveToNextItem(Var List:ListRec)
  239. ---------------------------------------
  240. This  procedure  does  exactly what it is called,  it  moves  the 
  241. current  item to the next item on the list.  The field ListOK  is 
  242. set to false when the end of the list is reached.
  243.  
  244.      Program DemoList;
  245.      Uses List;
  246.  
  247.      Var
  248.         MyList               :   ListRec;
  249.         Item                 :   ^Byte;
  250.         I                    :   Byte;
  251.  
  252.      Begin
  253.         InitList(MyList);
  254.         
  255.         For I:=1 To 10 Do Begin
  256.            New(Item);
  257.            Item^:=I;
  258.  
  259.            AddToList(Item, MyList);
  260.         End;
  261.  
  262.         New(Item);
  263.         Item^:=0;
  264.         InsertInList(Item, MyList);
  265.  
  266.         While MyList.ListOK Do Begin
  267.            Write(Byte(CurrentItemPtr(MyList)^));
  268.            Write(' ');
  269.            MoveToNextItem(MyList);
  270.         End;
  271.      End.
  272.  
  273. This program will display the output
  274.      0 1 2 3 4 5 6 7 8 9 10
  275.  
  276.  
  277.  
  278. MoveToPrevItem(Var List:ListRec)
  279. ---------------------------------------
  280. This  procedure is exactly like MoveToNextItem, except  opposite.  
  281. MoveToPrevItem will move the current item pointer to the previous 
  282. item and will set ListOK to false if the beginning is reached.
  283.  
  284.  
  285.  
  286. MoveToFirstItem(Var List:ListRec)
  287. ---------------------------------------
  288. This  procedure moves the current item pointer to the first  item 
  289. in the list.  It will set ListOK to false if the list is empty.
  290.  
  291.  
  292.  
  293. MoveToLastItem(Var List:ListRec)
  294. ---------------------------------------
  295. MoveToLastItem  will  move the current item pointer to  the  last 
  296. item  in  the list.  It will set ListOK to false if the  list  is 
  297. empty.
  298.  
  299.  
  300.  
  301. MoveToItem(Item:Pointer; Var List:ListRec)
  302. ---------------------------------------
  303. MoveToItem will move List's current item to Item.  ListOK will be 
  304. set to false if Item isn't in List.
  305.  
  306.      Program DemoList;
  307.      Uses List;
  308.  
  309.      Var
  310.         MyList               :   ListRec;
  311.         Item, 5thItem        :   ^Byte;
  312.         I                    :   Byte;
  313.  
  314.      Begin
  315.         InitList(MyList);
  316.         
  317.         For I:=1 To 10 Do Begin
  318.            New(Item);
  319.            Item^:=I;
  320.  
  321.            If I=5 Then 5thItem:=Item;
  322.  
  323.            AddToList(Item, MyList);
  324.         End;
  325.  
  326.         MoveToItem(5thItem, MyList);
  327.      End.
  328.  
  329. This  program  will create a list of 10 consecutive  numbers  and 
  330. then move the current pointer in MyList to the 5th item (in  this 
  331. case number 5).  
  332.  
  333. I  am not sure why I included this procedure in the  package.   I 
  334. used it once, but I can't remember why.  If you have some use for 
  335. it, please let me know.
  336.  
  337.  
  338.  
  339. ItemInList(Item:Pointer; 
  340.            List:ListRec):Boolean
  341. ---------------------------------------
  342. This function returns True if Item is in List and False if it  is 
  343. not.   Again,  if you have any use for the routine, I'd  like  to 
  344. know what it is.
  345.  
  346.  
  347.  
  348. DisposeOfList(Var List:ListRec);
  349. ---------------------------------------
  350. When you're all done playing with your lists, you need to get rid 
  351. of it.  This routine does just that.  It reclaims all memory used 
  352. by the list and calls InitList to reset the list.
  353.  
  354. It  is important to note that DisposeOfList only reclaims  memory 
  355. used by the List, not by the items in the list.  For example:
  356.  
  357.      Program DemoList;
  358.      Uses List;
  359.  
  360.      Var
  361.         MyList               :   ListRec;
  362.         Item                 :   ^Byte;
  363.         I                    :   Byte;
  364.  
  365.      Begin
  366.         InitList(MyList);
  367.         
  368.         For I:=1 To 10 Do Begin
  369.            New(Item);
  370.            Item^:=I;
  371.  
  372.            AddToList(Item, MyList);
  373.         End;
  374.  
  375.         DisposeOfList(MyList);
  376.      End.
  377.  
  378. This   program  won't  reclaim  the  memory  set  aside  by   the 
  379. 'New(Item)' statement.  The method for reclaiming all memory used 
  380. by a list and its Items is as follows:
  381.  
  382.      Program DemoList;
  383.      Uses List;
  384.  
  385.      Var
  386.         MyList               :   ListRec;
  387.         Item                 :   ^Byte;
  388.         I                    :   Byte;
  389.  
  390.      Begin
  391.         InitList(MyList);
  392.         
  393.         For I:=1 To 10 Do Begin
  394.            New(Item);
  395.            Item^:=I;
  396.  
  397.            AddToList(Item, MyList);
  398.         End;
  399.  
  400.         MoveToFirstItem(MyList);
  401.         While MyList.ListOK Do Begin
  402.            Item:=CurrentItemPtr(MyList);
  403.            Dispose(Item); {The argument to Dispose must be a variable}
  404.            MoveToNextItem(MyList);
  405.         End;
  406.  
  407.         DisposeOfList(MyList);
  408.      End.
  409.  
  410.  
  411.  
  412.  
  413. Again,  I  hope  these routines are helpful.   If  you  have  any 
  414. comments or suggestions please contact me through CompuServe.
  415.  
  416. Mark Addleman
  417. [72777,740]
  418.