Kylin/DataStructure/LinkedStack.h
2023-12-27 10:29:16 +08:00

38 lines
983 B
C++

#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H
#include "LinkedList.h"
#include "Stack.h"
#include "utility"
#include <initializer_list>
namespace Kylin {
template <typename T>
class LinkedStack : public Stack<T> {
public:
LinkedStack() = default;
LinkedStack(LinkedStack &&other) : m_list(std::move(other.m_list)) {}
LinkedStack(std::initializer_list<T> init) {
for (auto &value : init) push(value);
}
virtual void push(const T &value) { m_list.insert(0, value); }
T pop() final {
if (m_list.size() <= 0) THROW_EXCEPTION(InvalidOperationException, "There is no element in stack...");
auto value = m_list[0];
m_list.removeAt(0);
return value;
}
virtual T &top() { return m_list[0]; }
virtual void clear() { m_list.clear(); }
virtual size_t size() const { return m_list.size(); }
private:
LinkedList<T> m_list;
};
} // namespace Kylin
#endif // LINKEDSTACK_H