home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / exec_library / lists / buildlist.e
Text File  |  1977-12-31  |  3KB  |  96 lines

  1. -> buildlist.e - Example which uses an application-specific Exec list
  2.  
  3. MODULE 'amigalib/lists',
  4.        'exec/lists',
  5.        'exec/nodes'
  6.  
  7. CONST DATASIZE=62
  8.  
  9. OBJECT nameNode
  10.   ln:ln               -> System Node structure
  11.   data[DATASIZE]:ARRAY  -> Node-specific data
  12. ENDOBJECT
  13.  
  14. CONST NAMENODE_ID=100  -> The type of 'nameNode'
  15.  
  16. PROC main() HANDLE
  17.   DEF nameList=NIL:PTR TO lh  -> Note that a mlh would also work
  18.   NEW nameList
  19.   newList(nameList)  -> Important: prepare header for use
  20.  
  21.   addName(nameList, 'Name7');  addName(nameList, 'Name6')
  22.   addName(nameList, 'Name5');  addName(nameList, 'Name4')
  23.   addName(nameList, 'Name2');  addName(nameList, 'Name0')
  24.  
  25.   addName(nameList, 'Name7');  addName(nameList, 'Name5')
  26.   addName(nameList, 'Name3');  addName(nameList, 'Name1')
  27.  
  28.   displayName(nameList, 'Name5')
  29.   displayNameList(nameList)
  30.  
  31. EXCEPT DO
  32.   IF nameList
  33.     -> E-Note: none of this is necessary, since the program is terminating
  34.     ->         and the memory will be freed automatically
  35.     freeNameNodes(nameList)
  36.     END nameList  -> Free list header
  37.   ENDIF
  38.   SELECT exception
  39.   CASE "MEM"; WriteF('Error: Out of memory\n')
  40.   ENDSELECT
  41. ENDPROC
  42.  
  43. -> Allocate a NameNode structure, copy the given name into the structure, then
  44. -> add it the specified list.  This example does not provide an error return
  45. -> for the out of memory condition.
  46. -> E-Note: ...instead it raises an exception which is handled by the caller
  47. PROC addName(list, name)
  48.   DEF namenode:PTR TO nameNode
  49.   NEW namenode
  50.   -> E-Note: copy *safely* to namenode.data
  51.   AstrCopy(namenode.data, name, DATASIZE)
  52.   namenode.ln.name:=namenode.data
  53.   namenode.ln.type:=NAMENODE_ID
  54.   namenode.ln.pri:=0
  55.   AddHead(list, namenode)
  56. ENDPROC
  57.  
  58. -> Free the entire list, including the header.  The header is not updated as
  59. -> the list is freed.  This function demonstrates how to avoid referencing
  60. -> freed memory when deallocating nodes.
  61. PROC freeNameNodes(list:PTR TO lh)
  62.   DEF worknode:PTR TO nameNode, nextnode
  63.   worknode:=list.head  -> First node
  64.   WHILE nextnode:=worknode.ln.succ
  65.     END worknode
  66.     worknode:=nextnode
  67.   ENDWHILE
  68. ENDPROC
  69.  
  70. -> Print the names of each node in a list.
  71. PROC displayNameList(list:PTR TO lh)
  72.   DEF node:PTR TO ln
  73.   IF list.tailpred=list
  74.     WriteF('List is empty.\n')
  75.   ELSE
  76.     node:=list.head
  77.     WHILE node.succ
  78.       WriteF('$\h -> \s\n', node, node.name)
  79.       node:=node.succ
  80.     ENDWHILE
  81.   ENDIF
  82. ENDPROC
  83.  
  84. -> Print the location of all nodes with a specified name.
  85. PROC displayName(list, name)
  86.   DEF node:PTR TO ln
  87.   IF node:=FindName(list,name)
  88.     WHILE node
  89.       WriteF('Found a \s at location $\h\n', node.name, node)
  90.       node:=FindName(node, name)
  91.     ENDWHILE
  92.   ELSE
  93.     WriteF('No node with name \s found.\n', name)
  94.   ENDIF
  95. ENDPROC
  96.