Guest User

ListArrayIterator.h

a guest
Feb 4th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #if !defined (NULL)
  2. #define NULL 0
  3. #endif
  4.  
  5. #if !defined (LISTARRAYITERATOR_H)
  6. #define LISTARRAYITERATOR_H
  7.  
  8. template < class T >
  9. class ListArrayIterator
  10. {
  11.    private:
  12.       int index;
  13.       int sz;
  14.       T** items;
  15.  
  16.    public:
  17.       ListArrayIterator(T** items, int size);
  18.       ~ListArrayIterator();
  19.       bool hasNext();
  20.       T* next();
  21. };
  22.  
  23. template < class T >
  24. ListArrayIterator<T>::ListArrayIterator(T** itms, int size)
  25. {
  26.    items = new T*[size];
  27.    for (int i = 0; i < size; i++)
  28.    {
  29.       items[i] = itms[i];  //snapshot of the data
  30.    }
  31.  
  32.    index = 1;
  33.    sz = size;
  34. }
  35.  
  36. template < class T >
  37. ListArrayIterator<T>::~ListArrayIterator()
  38. {
  39.    delete[] items;
  40. }
  41.  
  42. template < class T >
  43. bool ListArrayIterator<T>::hasNext()
  44. {
  45.    return (index <= sz);
  46. }
  47.  
  48. template < class T >
  49. T* ListArrayIterator<T>::next()
  50. {
  51.    T* item = NULL;
  52.  
  53.    if (hasNext())
  54.    {
  55.       item = items[index - 1];
  56.       index++;
  57.    }
  58.  
  59.    return item;
  60. }
  61.  
  62. #endif
Advertisement
Add Comment
Please, Sign In to add comment