C/C++

Помогите составить програму на с++

1. Создать класс Stack, который будет реализовывать принцип работы структуры данных стек. Размер стека не должен быть ограничен (используйте динамический массив).

2. Реализовать для любого типа данных на выбор (int, float, double, string). Должны быть как минимум методы pop(), чтобы достать из стека верхний элемент, и push(), чтобы добавить новый элемент.

3. Протестировать свою структуру.

4. При желании можно выполнить усложненную версию задания и реализовать стек при помощи шаблонов (необходимо самим ознакомиться с материалом).
#include <iostream>
using namespace std;
template<typename Type> struct Node {
Node() : prev(nullptr), next(nullptr) {}
Type value;
Node* prev;
Node* next;
};
template<typename Type>
class Stack {
public:
using size_type = size_t;
Stack(const Stack<Type>&) = delete;
Stack(Stack<Type>&&) = delete;
Stack<Type> operator=(const Stack<Type>&) = delete;
Stack<Type> operator=(Stack<Type>&&) = delete;
Stack() : head_(nullptr), tail_(nullptr), length_(0) {}
Stack(initializer_list<Type>& list) : head_(nullptr), tail_(nullptr), length_(0) {
for (auto& value : list) push(value);
}
Stack(initializer_list<Type> list) : head_(nullptr), tail_(nullptr), length_(0) {
for (auto& value : list) push(value);
}
~Stack() {
if (tail_ != nullptr) {
while (!empty()) pop();
}
}
bool empty()const noexcept {
return tail_ == nullptr;
}
void push(const Type& value) {
Node<Type>* ptr = new Node<Type>;
if (empty()) {
head_ = tail_ = ptr;
tail_->value = value;
}
else {
tail_->next = ptr;
ptr->prev = tail_;
tail_ = ptr;
tail_->value = value;
}
++length_;
}
void pop() {
if (tail_ == head_) {
delete tail_;
tail_ = head_ = nullptr;
}
else {
Node<Type>* tmp = tail_->prev;
tail_->prev = tail_->next = nullptr;
delete tail_;
tail_ = tmp;
}
--length_;
}
Type& top() {
return tail_->value;
}
size_type size()const {
return length_;
}
private:
Node<Type>* head_;
Node<Type>* tail_;
size_type length_;
friend bool operator==(const Stack<Type>& a, const Stack<Type>& b) {
if (a.length_ == b.length_) {
auto ahead = a.head_;
auto bhead = b.head_;
const auto end = a.tail_->next;
while (ahead != end) {
if (ahead->value != bhead->value) return false;
ahead = ahead->next;
bhead = bhead->next;
}
return true;
}
return false;
}
friend bool operator!=(const Stack<Type>& a, const Stack<Type>& b) {
return !(a == b);
}
friend bool operator<(const Stack<Type>& a, const Stack<Type>& b) {
const auto length = a.length_ < b.length_ ? a.length_ : b.length_;
auto ahead = a.head_;
auto bhead = b.head_;
for (auto i = 0U; i < length; ++i) {
if (ahead->value >= bhead->value) return false;
ahead = ahead->next;
bhead = bhead->next;
}
return a.length_ < b.length_;
}
friend bool operator<=(const Stack<Type>& a, const Stack<Type>& b) {
return a < b || a == b;
}
friend bool operator>(const Stack<Type>& a, const Stack<Type>& b) {
return !(a <= b);
}
friend bool operator>=(const Stack<Type>& a, const Stack<Type>& b) {
return a > b || a == b;
}
};
Иван Осадчий
Иван Осадчий
60 298
Лучший ответ
Ну кидай свой код, а мы будем комментировать и критиковать