home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 04 / list.cpp < prev    next >
C/C++ Source or Header  |  1992-02-05  |  2KB  |  101 lines

  1. // list.cpp RHS 8/2/91
  2.  
  3. #include<stdio.h>
  4. #include"list.h"
  5.  
  6. List::List(void)
  7.     {
  8.     lastnode = end = start = current = NULL;
  9.     NumElements = lastndx = 0;
  10.     }
  11.  
  12. List::~List(void)
  13.     {
  14.     if(start)
  15.         {
  16.         NODE *temp;
  17.  
  18.         for(current = start; current; )
  19.             {
  20.             temp = current->next;
  21.             delete current->object;
  22.             delete current;
  23.             current = temp;
  24.             }
  25.         }
  26.     }
  27.  
  28.  
  29. int List::Add(void *object)
  30.     {
  31.     NODE *temp = new NODE;
  32.     if(!temp)
  33.         return 1;
  34.     temp->object = object;
  35.     temp->next = NULL;
  36.     if(!start)
  37.         lastnode = start = end = current = temp;
  38.     else
  39.         {
  40.         end->next = temp;
  41.         current = end = temp;
  42.         }
  43.     NumElements++;
  44.     return 0;
  45.     }
  46.  
  47. void *List::Current(void)
  48.     {
  49.     return current->object;
  50.     }
  51.  
  52. int List::Num(void)
  53.     {
  54.     return NumElements;
  55.     }
  56.  
  57. void *List::operator[](int index)
  58.     {
  59.     if(index >= NumElements)
  60.         return NULL;
  61.  
  62.     int i = 0;
  63.     NODE *temp = start;
  64.  
  65.     if(lastndx <= index)
  66.         {
  67.         i = lastndx;
  68.         temp = lastnode;
  69.         }
  70.  
  71.     for( ; i < index; i++)
  72.         temp = temp->next;
  73.     lastndx = i;
  74.     lastnode = temp;
  75.     return temp->object;
  76.     }
  77.  
  78. void List::Dump(int index)
  79.     {
  80.     if(index >= NumElements)
  81.         return;
  82.  
  83.     int i = 0;
  84.     NODE *temp = start;
  85.  
  86.     if(lastndx <= index)
  87.         {
  88.         i = lastndx;
  89.         temp = lastnode;
  90.         }
  91.  
  92.     for( ; i < index; i++)
  93.         temp = temp->next;
  94.     lastndx = i;
  95.     lastnode = temp;
  96.  
  97.     printf("(index= %d) NODE @ %p: object=%p next=%p\n",
  98.         index,temp,temp->object,temp->next);
  99.     }
  100.  
  101.