Guest User

ListArray.h

a guest
Feb 4th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #if !defined (LISTARRAY_H)
  2. #define LISTARRAY_H
  3.  
  4. #include "ListArrayIterator.h"
  5.  
  6. template < class T >
  7. class ListArray
  8. {
  9.    private:
  10.       int max_size;
  11.       T** items;
  12.       int sz;
  13.  
  14.       void arrayResize(int new_max_size);
  15.  
  16.    public:
  17.       ListArray();
  18.       ~ListArray();
  19.  
  20.       bool isEmpty();
  21.       int size();
  22.       void removeAll();
  23.       T* get(int index);
  24.       void add(int index, T* item);
  25.       void add(T* item);
  26.       void remove(int index);
  27.       void set(int index, T* item);
  28.       ListArrayIterator<T>* iterator();
  29.       T** toArray();
  30. };
  31.  
  32. template < class T >
  33. ListArray<T>::ListArray()
  34. {
  35.    max_size = 10;
  36.    items = new T*[max_size];
  37.    sz = 0;
  38. }
  39.  
  40. template < class T >
  41. ListArray<T>::~ListArray()
  42. {
  43.    delete[] items;  //the items themselves are not deleted
  44. }
  45.  
  46. template < class T >
  47. bool ListArray<T>::isEmpty()
  48. {
  49.    return (sz == 0);
  50. }
  51.  
  52. template < class T >
  53. int ListArray<T>::size()
  54. {
  55.    return sz;
  56. }
  57.  
  58. template < class T >  //1-based
  59. T* ListArray<T>::get(int index)
  60. {
  61.    T* item = NULL;
  62.  
  63.    if (index >= 1 && index <= sz)
  64.    {  
  65.       item = items[index - 1];
  66.    }
  67.  
  68.    return item;
  69. }
  70.  
  71. template < class T >
  72. void ListArray<T>::add(T* item)
  73. {
  74.    add(sz + 1, item);  //add the item to the end of the array list
  75. }
  76.  
  77. template < class T >
  78. void ListArray<T>::add(int index, T* item)
  79. {
  80.    if (index < 1 || index > sz + 1)
  81.    {
  82.       return;
  83.    }
  84.  
  85.    //need more room in the array list
  86.    if (sz == max_size)
  87.    {
  88.       arrayResize(2*max_size);
  89.    }  
  90.  
  91.    for (int i = sz; i >= index; i--)
  92.    {
  93.       items[i] = items[i - 1];
  94.    }
  95.  
  96.    items[index - 1] = item;
  97.    sz++;
  98. }
  99.  
  100. template < class T >
  101. void ListArray<T>::remove(int index)
  102. {
  103.    if (index < 1 || index > sz)
  104.    {
  105.       return;
  106.    }
  107.  
  108.    for (int i = index; i < sz; i++)
  109.    {
  110.       items[i - 1] = items[i];
  111.    }
  112.  
  113.    items[sz - 1] = NULL;
  114.    sz--;
  115. }
  116.  
  117. template < class T >
  118. ListArrayIterator<T>* ListArray<T>::iterator()
  119. {
  120.    ListArrayIterator<T>* iter = new ListArrayIterator<T>(items, sz);
  121.    return iter;
  122. }
  123.  
  124. template < class T >
  125. void ListArray<T>::set(int index, T* item)
  126. {
  127.    //could use other methods already written, but this is more efficient
  128.    if (index >= 1 && index <= sz)
  129.    {
  130.       items[index - 1] = item;  //overwrite contents at that location
  131.    }
  132. }
  133.  
  134. template < class T >
  135. void ListArray<T>::arrayResize(int new_max_size)
  136. {
  137.    max_size = new_max_size;
  138.    T** temp = new T*[max_size];
  139.  
  140.    for (int i = 0; i < sz; i++)
  141.    {
  142.       temp[i] = items[i];
  143.    }
  144.  
  145.    delete[] items;
  146.    items = temp;
  147. }
  148.  
  149. template < class T >
  150. void ListArray<T>::removeAll()
  151. {
  152.    delete[] items;
  153.  
  154.    max_size = 10;
  155.    items = new T*[max_size];
  156.    sz = 0;
  157. }
  158.  
  159. template < class T >
  160. T** ListArray<T>::toArray()
  161. {
  162.    int num_items = size();
  163.    T** to_array = new T*[num_items];
  164.    for (int i = 0; i < num_items; i++)
  165.    {
  166.       to_array[i] = items[i];
  167.    }
  168.    return to_array;
  169. }
  170.  
  171. #endif
Advertisement
Add Comment
Please, Sign In to add comment