Created
August 21, 2025 04:43
-
-
Save un4ckn0wl3z/5d8dfd5a1b064ef2d2e43f2c967f4319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <exception> | |
template<typename T> | |
class Stack { | |
public: | |
Stack(size_t max_size = 0); | |
~Stack(); | |
void Push(T const& value); | |
T Pop(); | |
bool IsEmpty() const; | |
private: | |
T* m_data; | |
size_t m_sp, m_max_size; | |
}; | |
template<typename T> | |
Stack<T>::Stack(size_t max_size) : m_sp(0) { | |
if (max_size == 0) | |
max_size = 10; | |
m_data = new T[m_max_size = max_size]; | |
} | |
template<typename T> | |
Stack<T>::~Stack() { | |
delete[] m_data; | |
} | |
template<typename T> | |
void Stack<T>::Push(T const& value) { | |
if (m_sp == m_max_size) | |
throw std::exception("stack full"); | |
m_data[m_sp++] = value; | |
} | |
template<typename T> | |
T Stack<T>::Pop() { | |
if (m_sp == 0) | |
throw std::exception("stack empty"); | |
return m_data[--m_sp]; | |
} | |
template<typename T> | |
bool Stack<T>::IsEmpty() const { | |
return m_sp == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment