circularArrayList.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef CIRCULARARRAYLIST_H_
  2. #define CIRCULARARRAYLIST_H_
  3. #include "list.h"
  4. /**
  5. * An CircularArrayList implements the pure virtual List interface.
  6. *
  7. * It stores a list as a circular array of items, with the first item
  8. * in the list being in array position headPos, the second item in
  9. * position (headPos+1)%capacity, the third item in position
  10. * (headPos+2)%capacity, and so on.
  11. */
  12. template <typename T>
  13. class CircularArrayList : public List<T> {
  14. private:
  15. int headPos; // Array position of the first item in the list.
  16. int size; // Number of items currently stored in this list.
  17. int capacity; // Current size (including empty slots) of our array.
  18. T* values; // The array that stores the items in the list.
  19. public:
  20. CircularArrayList();
  21. ~CircularArrayList();
  22. int getSize(); // Get number of items in this list.
  23. bool isEmpty(); // True iff list contains no items.
  24. T peekHead(); // Returns item at front of list.
  25. T peekTail(); // Returns item at back of list.
  26. T get(int i); // Returns the ith item in the list.
  27. void insertAtHead(T value); // Prepends item to front of list.
  28. void insertAtTail(T value); // Appends item to back of list.
  29. T removeHead(); // Removes and returns front item.
  30. T removeTail(); // Removes and returns back item.
  31. private:
  32. void expandCapacity(); // Expands the array to store more items.
  33. };
  34. #include "circularArrayList-inl.h"
  35. #endif // CIRCULARARRAYLIST_H_