Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- 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(std::initializer_list<Type>& list) : head_(nullptr), tail_(nullptr), length_(0) {
- for (auto& value : list) push(value);
- }
- Stack(std::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;
- }
- };
- int main() {
- Stack<int> stack({ 1, 2, 3, 4, 5, 6, 7, 8, 9 });
- std::cout << "Stack size: " << stack.size() << '\n';
- if (!stack.empty()) std::cout << "Top: " << stack.top() << '\n';
- if (!stack.empty()) stack.top() = 15;
- if (!stack.empty()) std::cout << "Top: " << stack.top() << '\n';
- if (!stack.empty()) stack.pop();
- if (!stack.empty()) std::cout << "Top: " << stack.top() << '\n';
- while (!stack.empty()) stack.pop();
- Stack<int> a({ 1, 2, 3, 4, 5 });
- Stack<int> b({ 1, 2, 3, 4, 5 });
- std::cout << "a == b " << std::boolalpha << (a == b) << '\n';
- std::cout << "a < b " << std::boolalpha << (a < b) << '\n';
- std::cout << "a <= b " << std::boolalpha << (a <= b) << '\n';
- std::cout << "a >= b " << std::boolalpha << (a >= b) << '\n';
- std::cout << "a > b " << std::boolalpha << (a > b) << '\n';
- std::cout << "a != b " << std::boolalpha << (a != b) << '\n';
- std::cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement