Created
March 7, 2023 07:31
-
-
Save iarash84/665be6450f372f106f91b5340b3d6ef4 to your computer and use it in GitHub Desktop.
stack template example
This file contains 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
template<typename T> | |
class Stack { | |
private: | |
T* data; | |
int size; | |
int capacity; | |
public: | |
Stack(int capacity) : capacity(capacity), size(0) { | |
data = new T[capacity]; | |
} | |
~Stack() { | |
delete[] data; | |
} | |
void push(T value) { | |
if (size < capacity) { | |
data[size++] = value; | |
} | |
} | |
T pop() { | |
if (size > 0) { | |
return data[--size]; | |
} | |
else { | |
throw std::runtime_error("Stack is empty"); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment