|
| 1 | +#ifndef DSLISTSTACK_H |
| 2 | +#define DSLISTSTACK_H |
| 3 | + |
| 4 | +#include "dsdoublyll.h" |
| 5 | + |
| 6 | + |
| 7 | +template <class T> |
| 8 | +class DSListStack |
| 9 | +{ |
| 10 | +public: |
| 11 | + /** |
| 12 | + * @brief DSStack - default constructor |
| 13 | + */ |
| 14 | + DSListStack(); |
| 15 | + /** |
| 16 | + * @brief DSStack - copy constructor |
| 17 | + * @param other - reference to target stack |
| 18 | + */ |
| 19 | + DSListStack(const DSListStack<T>& other); |
| 20 | + |
| 21 | + /** |
| 22 | + * @brief ~DSStack - destructor |
| 23 | + */ |
| 24 | + ~DSListStack(); |
| 25 | + |
| 26 | + /** |
| 27 | + * @brief Getter - numIndexes |
| 28 | + */ |
| 29 | + int size(); |
| 30 | + |
| 31 | + /** |
| 32 | + * @brief push - adds an element to the top of the stack |
| 33 | + * @param element - element to add to stack |
| 34 | + */ |
| 35 | + void push(T element); |
| 36 | + |
| 37 | + /** |
| 38 | + * @brief pop - removes the top element of the stack |
| 39 | + */ |
| 40 | + void pop(); |
| 41 | + |
| 42 | + /** |
| 43 | + * @brief peek - returns the top element of the stack |
| 44 | + * @return top element of the stack |
| 45 | + */ |
| 46 | + T peek(); |
| 47 | + |
| 48 | + /** |
| 49 | + * @brief operator = : Sets this stack equal to the passed in stack |
| 50 | + * @param other - reference to target stack |
| 51 | + * @return reference to this stack |
| 52 | + */ |
| 53 | + DSListStack<T>& operator=(const DSListStack<T>& other); |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief operator + : returns a stack combining this and the given stack |
| 57 | + * @param other - reference to given stack |
| 58 | + * @return combined stack of this and given stack |
| 59 | + */ |
| 60 | + DSListStack<T> operator+(const DSListStack<T>& other) const; |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief operator += : combines this stack with the given stack |
| 64 | + * @param other - reference to the given stack |
| 65 | + * @return this stack, after it has been combined with the given |
| 66 | + */ |
| 67 | + DSListStack<T>& operator+=(const DSListStack<T>& other); |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief operator == : compares this stack against another stack |
| 71 | + * @param other - reference to target stack |
| 72 | + * @return true if the stacks have equal values |
| 73 | + */ |
| 74 | + bool operator==(const DSListStack<T>& other) const; |
| 75 | + |
| 76 | +private: |
| 77 | + DSDoublyLL<T> data; |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief DSStack - default constructor |
| 82 | + */ |
| 83 | +template <class T> |
| 84 | +DSListStack<T>::DSListStack(){} |
| 85 | +/** |
| 86 | + * @brief DSStack - copy constructor |
| 87 | + * @param other - reference to target stack |
| 88 | + */ |
| 89 | +template <class T> |
| 90 | +DSListStack<T>::DSListStack(const DSListStack<T>& other){ |
| 91 | + data = other->data; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * @brief ~DSStack - destructor |
| 96 | + */ |
| 97 | +template <class T> |
| 98 | +DSListStack<T>::~DSListStack(){} |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief Getter - numIndexes |
| 102 | + */ |
| 103 | +template <class T> |
| 104 | +int DSListStack<T>::size(){ |
| 105 | + return data.size(); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * @brief push - adds an element to the top of the stack |
| 110 | + * @param element - element to add to stack |
| 111 | + */ |
| 112 | +template <class T> |
| 113 | +void DSListStack<T>::push(T element){ |
| 114 | + data.pushBack(element); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief pop - removes the top element of the stack |
| 119 | + */ |
| 120 | +template <class T> |
| 121 | +void DSListStack<T>::pop(){ |
| 122 | + data.popBack(); |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief peek - returns the top element of the stack |
| 127 | + * @return top element of the stack |
| 128 | + */ |
| 129 | +template <class T> |
| 130 | +T DSListStack<T>::peek(){ |
| 131 | + return data[size() - 1]; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief operator = : Sets this stack equal to the passed in stack |
| 136 | + * @param other - reference to target stack |
| 137 | + * @return reference to this stack |
| 138 | + */ |
| 139 | +template <class T> |
| 140 | +DSListStack<T>& DSListStack<T>::operator=(const DSListStack<T>& other){ |
| 141 | + data = other.data; |
| 142 | + |
| 143 | + return *this; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * @brief operator + : returns a stack combining this and the given stack |
| 148 | + * @param other - reference to given stack |
| 149 | + * @return combined stack of this and given stack |
| 150 | + */ |
| 151 | +template <class T> |
| 152 | +DSListStack<T> DSListStack<T>::operator+(const DSListStack<T>& other) const{ |
| 153 | + DSListStack<T> result; |
| 154 | + result.data = data + other.data; |
| 155 | + |
| 156 | + return result; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * @brief operator += : combines this stack with the given stack |
| 161 | + * @param other - reference to the given stack |
| 162 | + * @return this stack, after it has been combined with the given |
| 163 | + */ |
| 164 | +template <class T> |
| 165 | +DSListStack<T>& DSListStack<T>::operator+=(const DSListStack<T>& other){ |
| 166 | + data += other.data; |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +/** |
| 171 | + * @brief operator == : compares this stack against another stack |
| 172 | + * @param other - reference to target stack |
| 173 | + * @return true if the stacks have equal values |
| 174 | + */ |
| 175 | +template <class T> |
| 176 | +bool DSListStack<T>::operator==(const DSListStack<T>& other) const{ |
| 177 | + return data == other.data; |
| 178 | +} |
| 179 | + |
| 180 | +#endif // DSLISTSTACK_H |
0 commit comments