Skip to content

Instantly share code, notes, and snippets.

@iarash84
Created March 7, 2023 07:31
Show Gist options
  • Save iarash84/665be6450f372f106f91b5340b3d6ef4 to your computer and use it in GitHub Desktop.
Save iarash84/665be6450f372f106f91b5340b3d6ef4 to your computer and use it in GitHub Desktop.
stack template example
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