Advertisement
Guest User

Untitled

a guest
May 27th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // Header file
  2. #ifndef _H_ARRAY
  3. #define _H_ARRAY
  4.  
  5. class Array
  6. {
  7. public:
  8. Array(); // Default Constructor
  9. Array(Array& arr); // Assignment Operator
  10. ~Array(); // Destructor
  11.  
  12. // Constants
  13. const int DEFAULT_SIZE = 10;
  14.  
  15. // Public functions
  16. void pushBack(int value);
  17. int get(int index) const;
  18. void remove(int index);
  19. void removeUnordered(int index);
  20. int getLength() const;
  21. bool isEmpty() const { return (_length == 0); }
  22. void setArrayCapacity(int cap);
  23.  
  24. private:
  25. int* _list; // Holds the array of data
  26. int _length; // Index indicates size and location items can be added to array
  27. int _capacity; // Max number of elements that can be stored in the array
  28.  
  29. // Private Helper Functions
  30. void doubleArraySize();
  31. };
  32.  
  33. #endif
  34.  
  35. // cpp file
  36.  
  37. #include "Array.h"
  38.  
  39. #include <iostream>
  40.  
  41. Array::Array()
  42. {
  43. _list = new int[DEFAULT_SIZE];
  44. _length = 0;
  45. _capacity = DEFAULT_SIZE;
  46. }
  47.  
  48.  
  49. Array::Array(Array& arr)
  50. {
  51. _list = new int[arr._capacity];
  52.  
  53. for (int i = 0; i < arr._length; i++)
  54. {
  55. _list[i] = arr._list[i];
  56. }
  57.  
  58. _capacity = arr._capacity;
  59. _length = arr._length;
  60. }
  61.  
  62.  
  63. Array::~Array()
  64. {
  65. delete[] _list;
  66. }
  67.  
  68.  
  69. void Array::pushBack(int value)
  70. {
  71. if (_length == _capacity)
  72. {
  73. doubleArraySize();
  74. }
  75.  
  76. _list[_length] = value;
  77. _length++;
  78. }
  79.  
  80. int Array::get(int index) const
  81. {
  82. if (index >= 0 && index < _length)
  83. return _list[index];
  84.  
  85. return -1;
  86. }
  87.  
  88. void Array::remove(int index)
  89. {
  90. if (index < 0 || index >= _length)
  91. {
  92. return;
  93. }
  94.  
  95. for (int i = index; i < _length - 1; i++)
  96. {
  97. _list[i] = _list[i + 1];
  98. }
  99.  
  100. _length--;
  101. }
  102.  
  103. void Array::removeUnordered(int index)
  104. {
  105. if (index < 0 || index >= _length)
  106. {
  107. return;
  108. }
  109.  
  110. _list[index] = _list[_length - 1];
  111.  
  112. _length--;
  113. }
  114.  
  115. void Array::setArrayCapacity(int cap)
  116. {
  117. if (cap < 1 || cap == _capacity)
  118. {
  119. return;
  120. }
  121.  
  122. if (cap >= _length)
  123. {
  124. int* temp = new int[cap];
  125.  
  126. for (int i = 0; i < _length; i++)
  127. {
  128. temp[i] = _list[i];
  129. }
  130.  
  131. delete[] _list;
  132. _list = temp;
  133. }
  134. else
  135. {
  136. int* temp = new int[cap];
  137.  
  138. for (int i = 0; i < cap; i++)
  139. {
  140. temp[i] = _list[i];
  141. }
  142.  
  143. delete[] _list;
  144. _list = temp;
  145.  
  146. _length = cap;
  147.  
  148. }
  149.  
  150.  
  151. _capacity = cap;
  152.  
  153. }
  154.  
  155. int Array::getLength() const
  156. {
  157. return _length;
  158. }
  159.  
  160.  
  161.  
  162. void Array::doubleArraySize()
  163. {
  164. int newCapacity = _capacity * 2;
  165.  
  166. int* temp = new int[newCapacity];
  167.  
  168. for (int i = 0; i < _length; i++)
  169. {
  170. temp[i] = _list[i];
  171. }
  172.  
  173. delete[] _list;
  174. _list = temp;
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement