added print, need testing
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <expected>
|
#include <expected>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
#include <print>
|
||||||
|
|
||||||
#define DEFAULT_STACK_SIZE 4
|
#define DEFAULT_STACK_SIZE 4
|
||||||
|
|
||||||
@@ -71,6 +72,7 @@ StackErr Stack<T>::push(T value) {
|
|||||||
|
|
||||||
delete[] this->data;
|
delete[] this->data;
|
||||||
this->data = tmp;
|
this->data = tmp;
|
||||||
|
this->cap = new_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->data[this->len] = value;
|
this->data[this->len] = value;
|
||||||
@@ -104,6 +106,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
|
|||||||
|
|
||||||
delete[] this->data;
|
delete[] this->data;
|
||||||
this->data = tmp;
|
this->data = tmp;
|
||||||
|
this->cap = new_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::expected(return_val);
|
return std::expected(return_val);
|
||||||
@@ -119,4 +122,24 @@ std::expected<T, StackErr> Stack<T>::peek() {
|
|||||||
return std::expected(this->data[this->len - 1]);
|
return std::expected(this->data[this->len - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
void Stack<T>::print() {
|
||||||
|
std::println("Stack:");
|
||||||
|
std::println("Length: {}.", this->len);
|
||||||
|
std::println("Capacity: {}.", this->cap);
|
||||||
|
|
||||||
|
std::println("{:^20}", "Datos");
|
||||||
|
std::println("{:^20}", "|");
|
||||||
|
std::println("{:^20}", "v");
|
||||||
|
|
||||||
|
for (int i = 0; i < this->cap; i++) {
|
||||||
|
if (i < this->len) {
|
||||||
|
std::println("{:^20}", this->data[i]);
|
||||||
|
} else {
|
||||||
|
std::println("{:^20}", "NULL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif // !ST
|
#endif // !ST
|
||||||
|
|||||||
Reference in New Issue
Block a user