Stack.h 796 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef STACK_H
  2. #define STACK_H
  3. #include "data/List.h"
  4. template<typename T>
  5. class Stack final {
  6. List<T> data;
  7. public:
  8. template<typename... Args>
  9. Stack& push(Args&&... args) {
  10. data.add(std::forward<Args>(args)...);
  11. return *this;
  12. }
  13. void clear() {
  14. data.clear();
  15. }
  16. bool pop() {
  17. if(data.getLength() <= 0) {
  18. return true;
  19. }
  20. data.remove(data.getLength() - 1);
  21. return false;
  22. }
  23. bool isEmpty() const {
  24. return data.getLength() == 0;
  25. }
  26. T& peek() {
  27. return data[data.getLength() - 1];
  28. }
  29. const T& peek() const {
  30. return data[data.getLength() - 1];
  31. }
  32. template<int L>
  33. void toString(StringBuffer<L>& s) const {
  34. s.append(data);
  35. }
  36. };
  37. #endif