"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/llist.h" (11 Jul 2020, 4442 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "llist.h" see the Fossies "Dox" file reference documentation.

    1 //
    2 //  Copyright (c) 1994, 1995, 2006 by Mike Romberg ( mike.romberg@noaa.gov )
    3 //
    4 //  This file may be distributed under terms of the GPL
    5 //
    6 
    7 #ifndef __LLIST_H__
    8 #define __LLIST_H__
    9 
   10 #include <stdio.h>
   11 
   12 #define MAXCURR 4    //  number of 'current' pointers
   13 
   14 class LList {
   15 public:
   16   //    The first constructor is used for unordered lists ( stacks, queques ).
   17   //  The second constructor is used for ordered lists. It's
   18   //  parameter is a pointer to the function to be used for ordering the
   19   //  list.  This function must behave in the following way:
   20   //        int Cmp_Fun ( void *data, void *key )
   21   //
   22   //        *data is a pointer to the type of information
   23   //        to be stored in the list.
   24   //
   25   //        *key is a pointer to the location within the data
   26   //        which will be used to order the list.  In some
   27   //        cases these two pointers will be of the same
   28   //        type.
   29   //
   30   //        function returns :
   31   //
   32   //        0.........*data = *key
   33   //        > 0.......*data > *key
   34   //        < 0.......*data < *key
   35 
   36   LList( void );                    //  for unordered lists
   37   LList( int ( *cmp_fun )( void *data, void *key ) );   //  for ordered lists
   38 
   39   virtual ~LList( void );  //  frees nodes but not data
   40 
   41   //    Stack like functions.
   42   //  pop returns NULL if the list is empty.
   43   //  push returns 1 on sucess and 0 upon failure.
   44   int push( void *data );
   45   void *pop( void );
   46 
   47   //    Queue like functions
   48   //  dequeue returns NULL if the list is empty.
   49   //  enqueue returns 1 on sucess and 0 upon failure.
   50   int enqueue( void *data ) { return( push( data ) ); }
   51   void *dequeue( void );
   52 
   53   //    Ordered list functions
   54   //  for insert *key points to some part of *data used for sorting.
   55   //  for find and removematch *key points to the "search" key.
   56   //  - insert returns 1 on sucess and 0 on failure
   57   //  - find and removematch return a pointer to the data stored in the list if
   58   //    sucessful and NULL if not.
   59   int insert( void *data, void *key );
   60   void *find( void *key );
   61   void *removematch( void *key );
   62 
   63   //    Misc. llist functions
   64   int putontop( void *data );  //  oposite of push
   65   void remove( void *data );   //  removes *data from the list if it's there
   66   void *findn( int n ); //  returns nth element if found or NULL if not
   67   void *operator[](int n)  //  returns nth element if found or NULL if not
   68     { return findn(n); }
   69   int index( void *data );     //  returns the index of *data or 0 if not there
   70 
   71   //    Sets the a pointer for the linked list to the nth element in
   72   //  the list. This function and the three that follow are intended to
   73   //  be used for a stepwise search of a linked list.  This function sets
   74   //  curr_ to the nth element in the list.  incc sets the same pointer
   75   //  to the next element in the list.  decc sets it to the previous
   76   //  element in the list.  findc returns a pointer to the element
   77   //  curr_ is "currently" pointing to. If n is not valid for the list
   78   //  curr_ is set to NULL.
   79   void setc( int n, int which = 0 );
   80   void incc( int which = 0 );
   81   void decc( int which = 0 );
   82   void *findc( int which = 0 );
   83 
   84   //    This function will save a linked list to the file pointed to
   85   //  by *fp.  The file should be binary.  n is saved first and then
   86   //  each item on the list is saved.  size is the number of bytes of
   87   //  each item on the list.  The list will remain intact after this
   88   //  function is called.
   89   void save( int size, FILE *fp );
   90 
   91   //    This function reads a linked list from the file pointed to
   92   //  by *fp ( previously saved by the above function ).  Space is
   93   //  allocated for each item, and it is placed into the list in it's
   94   //  old position.  size is the number of bytes each item occupies.
   95   //  This function will return 1 upon sucess and 0 upon failure.
   96   int restore( int size, FILE *fp );
   97 
   98   //    This function will remaove all of the elements in the linked
   99   //  list pointed to by L and free the memory each element occupied.
  100   void kill( void );
  101 
  102   int n( void ) const { return( n_ ); }     //  number of elements in the list
  103 protected:
  104 
  105   class LNode {
  106   public:
  107     LNode( void *data = NULL );
  108 
  109     void *data_;
  110     LNode *next_;
  111     LNode *prev_;
  112   };
  113 
  114   int n_;           //  number of nodes in the list
  115   LNode *top_, *btm_;           //  pointers to various nodes
  116   LNode *curr_[MAXCURR];
  117 
  118   //  a comparison function for ordered lists
  119   int ( *cmp_fun_ )( void *data, void *key );
  120   LNode *findnode( void *key );
  121   void *deletenode( LNode *ptr );
  122   LNode *findnnode( int i );
  123 private:
  124 };
  125 
  126 #endif