constructor and destructor
This commit is contained in:
@@ -1,8 +1,51 @@
|
|||||||
#ifndef STACK_H
|
#ifndef STACK_H
|
||||||
#define STACK_H
|
#define STACK_H
|
||||||
|
|
||||||
class Stack {
|
#include <cstdint>
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
|
#define DEFAULT_STACK_SIZE 4
|
||||||
|
|
||||||
|
enum class StackErr {
|
||||||
|
ok,
|
||||||
|
bad_alloc,
|
||||||
|
empty,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Stack {
|
||||||
|
private:
|
||||||
|
uint64_t len;
|
||||||
|
uint64_t cap;
|
||||||
|
T *data;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Stack();
|
||||||
|
~Stack();
|
||||||
|
|
||||||
|
std::expected<T, StackErr> pop();
|
||||||
|
|
||||||
|
std::expected<T, StackErr> peek();
|
||||||
|
|
||||||
|
StackErr push(T val);
|
||||||
|
|
||||||
|
void print();
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Stack<T>::Stack() {
|
||||||
|
Stack<T> new_stack;
|
||||||
|
new_stack.data = new T[DEFAULT_STACK_SIZE];
|
||||||
|
new_stack.cap = 0;
|
||||||
|
new_stack.len = 0;
|
||||||
|
return new_stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Stack<T>::~Stack() {
|
||||||
|
delete[] this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif // !ST
|
#endif // !ST
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "stack"
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user