Advertisement
Guest User

List.h

a guest
Jan 2nd, 2011
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. const int maxlist=1000;
  2. enum Error_code
  3. {
  4.     success,
  5.     overflow,
  6.     underflow,
  7.     range_error
  8. };
  9. template <class List_entry>
  10. class List
  11. {
  12.     public:
  13.     //List();
  14.  
  15.     List():count(0)
  16.     {}
  17.  
  18.     bool empty()
  19.     {
  20.     return count==0;
  21.     }
  22.     bool full()
  23.     {
  24.     return count==maxlist;
  25.     }
  26.     int size()
  27.     {
  28.         return count;
  29.     }
  30.     Error_code insert(int position, const List_entry &x)
  31.     {
  32.     if(full()) {
  33.     return overflow;}
  34.     if(position<0 || position>count)
  35.     {
  36.         return range_error;
  37.     }
  38.     for(int i=count-1;i>=position;i--)
  39.     {
  40.     entry[i+1]=entry[i];
  41.     }
  42.     entry[position]=x;
  43.     count++;
  44.     return success;
  45.     }
  46.     Error_code retrieve(int position, List_entry &x)const
  47.     {
  48.     if(empty()) return underflow;
  49.     if(position<0 || position>count) return range_error;
  50.     x=entry[position];
  51.     return success;
  52.     }
  53.  
  54.     Error_code replace(int position, const List_entry &x)
  55.     {
  56.     if(empty()) return underflow;
  57.     if(position<0 || position>count) return range_error;
  58.     entry[position]=x;
  59.     return success;
  60.     }
  61.     Error_code remove(int position, List_entry &x)
  62.     {
  63.     if(empty()) return underflow;
  64.     if(position>0 || position>=count) return range_error;
  65.     for(int i=position;i<=count-1;i++)
  66.     entry[i+1]=entry[i];
  67.     count--;
  68.     return success;
  69.     }
  70.  
  71.     void traverse(void (*visit)(List_entry &x))//*visit is the pointer to function which performs operation on list
  72.     {
  73.     for(int i=0;i<count;i++)
  74.     (*visit)(entry[i]);
  75.     }
  76.  
  77.     protected:
  78.     int count;
  79.     List_entry entry[maxlist];
  80. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement