home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / OSProject / p5 / List.h < prev    next >
Text File  |  2007-09-19  |  2KB  |  52 lines

  1. header List
  2.  
  3.   uses System
  4.  
  5.   class List [T: Listable]
  6.  
  7.     -- This class is used to implement a singly-linked list of elements.
  8.     -- Each element has a "next" pointer and this class maintains pointer
  9.     -- to the first element ("head") and to the last element ("last").
  10.     --
  11.     -- Elements may be added to the list at either end (using "AddToFront"
  12.     -- and "AddToEnd"), but may only be removed from the front using
  13.     -- "Remove".
  14.     --
  15.     -- The method "ApplyToEach" can be used to apply a single function
  16.     -- to each element in the list, one by one.  The argument to
  17.     -- "ApplyToEach" is the function to be applied to each element.
  18.     --
  19.     -- The elements in the list may be of any class T, as long as it
  20.     -- has been made a subclass of "Listable", which ensures that the
  21.     -- objects will have a "next" pointer and a "key".
  22.     --
  23.     -- This class can also be used to implement a sorted list where
  24.     -- each element has an associated integer key.  For sorted lists,
  25.     -- the elements should be added using "SortedInsert" (not "AddToEnd"
  26.     -- or "AddToFront").  The elements may only be removed from the front
  27.     -- but there is a provision for obtaining the key of the element
  28.     -- just removed as well as the element itself, using "SortedRemove".
  29.  
  30.     superclass Object
  31.     fields
  32.       first: ptr to T
  33.       last: ptr to T
  34.     methods
  35.       AddToFront (p: ptr to T)
  36.       AddToEnd (p: ptr to T)
  37.       Remove () returns ptr to T
  38.       IsEmpty () returns bool
  39.       ApplyToEach (f: ptr to function (ptr to T))
  40.       SortedInsert (p: ptr to T, k: int)
  41.       SortedRemove (whereToStoreItsKey: ptr to int) returns ptr to T
  42.   endClass
  43.  
  44.   class Listable
  45.     superclass Object
  46.     fields
  47.       next: ptr to Listable
  48.       key: int
  49.   endClass
  50.  
  51. endHeader
  52.