circularArrayList-inl.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // circularArrayList-inl.h: An inline implementation file for the templated
  2. // CircularArrayList class.
  3. #include <stdexcept>
  4. /*Constructor - initializes the size and starting headPos to 0,
  5. * allocates space for an array of size 10
  6. */
  7. template <typename T>
  8. CircularArrayList<T>::CircularArrayList() {
  9. headPos = 0;
  10. size = 0;
  11. capacity = 10; // We've chosen an initial capacity of 10.
  12. values = new T[capacity]; // Allocates an array of 10 items on the heap.
  13. }
  14. /*Destructor - deallocates array from heap
  15. */
  16. template <typename T>
  17. CircularArrayList<T>::~CircularArrayList() {
  18. delete [] values;
  19. }
  20. /*getSize - returns number of elements stored in the list
  21. */
  22. template <typename T>
  23. int CircularArrayList<T>::getSize() {
  24. return size;
  25. }
  26. /*isEmpty - returns true iff (if and only if) there are no items in the list
  27. */
  28. template <typename T>
  29. bool CircularArrayList<T>::isEmpty() {
  30. return size == 0;
  31. }
  32. /*peekHead - returns item stored at the beginning of the list
  33. */
  34. template <typename T>
  35. T CircularArrayList<T>::peekHead() {
  36. if (isEmpty()) {
  37. throw std::runtime_error("Attempted to peekHead on an empty list.");
  38. }
  39. return values[headPos];
  40. }
  41. /*peekTail - returns last item stored in the list
  42. */
  43. template <typename T>
  44. T CircularArrayList<T>::peekTail() {
  45. if (isEmpty()) {
  46. throw std::runtime_error("Attempted to peekTail on an empty list.");
  47. }
  48. return values[(headPos+size-1)%capacity];
  49. }
  50. /*get - returns the item at position i in the list
  51. * @param i - an integer representing the position of the item relative to the
  52. * beginning of the list
  53. */
  54. template <typename T>
  55. T CircularArrayList<T>::get(int i) {
  56. if (i < 0 || i >= size) {
  57. throw std::runtime_error("Attempted to get out of bounds.");
  58. }
  59. return values[(headPos+i)%capacity];
  60. }
  61. /*insertAtHead - adds an item to the beginning of the list
  62. * @param value - the item to add the list
  63. */
  64. template <typename T>
  65. void CircularArrayList<T>::insertAtHead(T value) {
  66. if (size == capacity) {
  67. expandCapacity();
  68. }
  69. headPos = (headPos+capacity-1) % capacity; // Avoids mod of negative #.
  70. values[headPos] = value; // Copies the value to the new first position.
  71. ++size;
  72. }
  73. /*insertAtTail - adds an item to the end of the list
  74. * @param value - the item to add the list
  75. */
  76. template <typename T>
  77. void CircularArrayList<T>::insertAtTail(T value) {
  78. if (size == capacity) {
  79. expandCapacity();
  80. }
  81. values[(headPos+size)%capacity] = value;
  82. ++size;
  83. }
  84. /*removeHead - removes and returns the first item in the list
  85. * @return the value of the item removed from the list
  86. */
  87. template <typename T>
  88. T CircularArrayList<T>::removeHead() {
  89. if (isEmpty()) {
  90. throw std::runtime_error("Attempted to removeHead on an empty list.");
  91. }
  92. int oldHeadPos = headPos; // Store position of value to return
  93. headPos = (headPos+1)%capacity; // Resets head to new position
  94. --size;
  95. return values[oldHeadPos];
  96. }
  97. /*removeTail - removes and returns the last item in the list
  98. * @return: the value of the item removed from the list
  99. */
  100. template <typename T>
  101. T CircularArrayList<T>::removeTail() {
  102. if (isEmpty()) {
  103. throw std::runtime_error("Attempted to removeTail on an empty list.");
  104. }
  105. --size;
  106. return values[(headPos+size)%capacity];
  107. }
  108. /*expandCapacity - private method for doubling the capacity of values
  109. */
  110. template <typename T>
  111. void CircularArrayList<T>::expandCapacity() {
  112. int newCapacity = 2*capacity;
  113. T* newArray = new T[newCapacity];
  114. for (int i = 0; i < capacity; ++i) {
  115. newArray[i] = values[(headPos + i) % capacity];
  116. }
  117. delete [] values;
  118. values = newArray;
  119. headPos = 0;
  120. capacity = newCapacity;
  121. }