home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07117b < prev    next >
Text File  |  1990-06-19  |  3KB  |  127 lines

  1. /* Listing 3*/
  2. /* --------------------- List.H------------------ */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define LIST_CLASS  unsigned (*at_top)(struct list*), \
  7.              (*at_end)(struct list*), \
  8.              (*is_empty)(struct list*), \
  9.              (*find)(struct list *, ...); \
  10.     void     (*prev)(struct list*), \
  11.              (*next)(struct list *), \
  12.              (*top)(struct list *), \
  13.              (*seek)(struct list *, long, int), \
  14.              (*end)(struct list *), \
  15.              (*display)(struct list*), \
  16.              (*add_member)(struct list*, void *), \
  17.              (*replace_member)(struct list *, void *), \
  18.     void     *(*current)(struct list *); \
  19.     long     (*total_members)(struct list *), \
  20.              (*tell)(struct list *);
  21.  
  22. typedef struct list {
  23.     LIST_CLASS
  24. } LIST;
  25.  
  26. LIST *new_list();
  27. destroy_list(LIST *);
  28.  
  29. #define TRUE 1
  30. #define FALSE 0
  31.  
  32. /* ----------------------  LIST.C ----------------*/
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include "list.h"
  37.  
  38. static void not_valid() {
  39.    fprintf(stderr,"Operation is not valid for this list\n");
  40.    getch();
  41. }
  42.  
  43. static unsigned is_empty(LIST *this) {
  44.     return( this->total_members(this) == 0L);
  45. }
  46.  
  47. static void seek(LIST *this, long where, int start) {
  48.     long count;
  49.  
  50.     switch(start) {
  51.         case SEEK_SET:
  52.             this->top(this);
  53.             for (count = 0; count < where; ++count) {
  54.                 if ( this->at_end(this) )
  55.                     break;
  56.                 this->next(this);
  57.             }
  58.         break;
  59.         case SEEK_CUR:
  60.             if (where > 0) {
  61.                 for (count = 0; count < where; ++count) {
  62.                     if ( this->at_end(this) )
  63.                         break;
  64.                     this->next(this);
  65.                 }
  66.             }
  67.             else {
  68.                 for(count = 0; count > where; ++count) {
  69.                     if ( this->at_top(this) )
  70.                        break;
  71.                     this->prev(this);
  72.                 }
  73.             }
  74.     break;
  75.     case SEEK_END:
  76.         this->end(this);
  77.         for(count = 0; count > where; ++count) {
  78.             if ( this->at_top(this) )
  79.                    break;
  80.             this->prev(this);
  81.         }
  82.     break;
  83.     }
  84. }
  85.  
  86. static long total_members(LIST *this)
  87. {
  88.     long thisone, count;
  89.     thisone = this->tell(this); this->top(this);
  90.     count = 0;
  91.     do {
  92.         if ( ! this->at_end(this) ) {
  93.             ++count;
  94.             this->next(this);
  95.         }
  96.     } while( ! this->at_end(this) );
  97.     this->seek(this,thisone,SEEK_SET);
  98.     return(count);
  99. }
  100.  
  101. LIST *new_list() {
  102. LIST *this;
  103.  
  104. /* Allocate Memory for this Object */
  105. this = calloc(1,sizeof(LIST));
  106. if (this == NULL)
  107.    return(NULL);
  108.  
  109. /* Assign Methods */
  110. this->at_top = not_valid;   this->at_end = not_valid;
  111. this->is_empty = is_empty;  this->find = not_valid;
  112. this->prev = not_valid;     this->next = not_valid;
  113. this->seek = seek;          this->top = not_valid;
  114. this->end = not_valid;      this->display = not_valid;
  115. this->replace_member = not_valid;
  116. this->add_member = not_valid;
  117. this->current = not_valid;
  118. this->total_members = total_members;
  119. this->tell = not_valid;
  120.  
  121. return(this);
  122. }
  123.  
  124. destroy_list(LIST *this) {
  125.     free(this);
  126. }
  127.