Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class Staque
- {
- private:
- class Node
- {
- public:
- using NodePointer = Node*;
- NodePointer next;
- NodePointer prev;
- int data;
- Node(int data, NodePointer next = nullptr, NodePointer prev = nullptr)
- :data(data), next(next), prev(prev)
- {
- }
- friend std::ostream& operator <<(std::ostream& output, const Node& object)
- {
- return output << object.data;
- }
- };
- using NodePointer = Node*;
- NodePointer first = nullptr;
- NodePointer last = nullptr;
- void push_back(int value)
- {
- NodePointer new_node = new Node(value);
- if (this->is_empty())
- {
- this->first = this->last = new_node;
- return;
- }
- new_node->prev = this->last;
- new_node->prev->next = new_node;
- this->last = new_node;
- }
- void push_front(int value)
- {
- NodePointer new_node = new Node(value);
- if (this->is_empty())
- {
- this->first = this->last = new_node;
- }
- new_node->next = this->first;
- this->first->prev = new_node;
- this->first = new_node;
- }
- public:
- Staque() = default;
- ~Staque()
- {
- this->clear();
- }
- Staque(const Staque& lvalue)
- {
- this->operator=(lvalue);
- }
- void clear()
- {
- NodePointer current_node = this->first;
- NodePointer next_node = nullptr;
- while (current_node)
- {
- next_node = current_node->next;
- delete current_node;
- current_node = next_node;
- }
- this->first = this->last = nullptr;
- }
- bool is_empty() const { return this->first == nullptr; }
- void push(int value)
- {
- if (value % 2 == 1)
- {
- this->push_back(value);
- }
- else
- {
- this->push_front(value);
- }
- }
- int pop()
- {
- if (this->is_empty())
- return INT_MIN;
- NodePointer deleted_node = this->last;
- int deleted_data = deleted_node->data;
- if (this->last == this->first)
- {
- delete this->last, this->first;
- this->last = this->first = nullptr;
- return deleted_data;
- }
- this->last = this->last->prev;
- delete deleted_node;
- this->last->next = nullptr;
- return deleted_data;
- }
- int pop_front()
- {
- if (this->is_empty())
- return INT_MIN;
- NodePointer deleted_node = this->first;
- int deleted_data = deleted_node->data;
- if (this->first == this->last)
- {
- delete this->last, this->first;
- this->last = this->first = nullptr;
- return deleted_data;
- }
- this->first = this->first->next;
- delete deleted_node;
- this->first->prev = nullptr;
- return deleted_data;
- }
- int top() { return this->first->data; }
- int bottom() { return this->last->data; }
- std::ostream& display(std::ostream& output) const
- {
- NodePointer current_node = this->first;
- if (this->is_empty())
- return output << "[]" << std::endl;
- if (this->first == this->last)
- return output << "[ " << *current_node << " ]";
- output << "[ ";
- while (current_node->next)
- {
- output << *current_node << ", ";
- current_node = current_node->next;
- }
- output << *this->last;
- output << " ]";
- return output;
- }
- void operator =(const Staque& lvalue)
- {
- if (this != &lvalue)
- {
- if (not lvalue.is_empty())
- {
- this->~Staque();
- this->first = new Node(lvalue.first->data);
- NodePointer current_node = this->first;
- NodePointer other_node = lvalue.first;
- while (other_node->next)
- {
- current_node->next = new Node(other_node->next->data);
- current_node->next->prev = current_node;
- current_node = current_node->next;
- other_node = other_node->next;
- }
- this->last = current_node;
- }
- }
- }
- friend std::ostream& operator <<(std::ostream& output,const Staque& object)
- {
- return object.display(output);
- }
- };
- int main()
- {
- std::cout << "n Staque" << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- Staque n;
- for (int i = 1; i <= 9; i++)
- {
- if (i == 5 or i == 7)
- continue;
- n.push(i);
- }
- std::cout << n << std::endl;
- std::cout << "Deletd: " << n.pop_front() << std::endl;
- std::cout << "Deletd: " << n.pop_front() << std::endl;
- std::cout << "Deletd: " << n.pop() << std::endl;
- std::cout << n << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << std::endl;
- std::cout << "m Staque" << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- Staque m = n;
- std::cout << "Called copy constructor for m variable copied from (n)" << std::endl;
- std::cout << "m = " << m << " | n = " << n << std::endl;
- std::cout << "Deletd: " << m.pop() << std::endl;
- std::cout << m << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- std::cout << std::endl;
- std::cout << "nc Staque" << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- Staque nc;
- nc = m;
- std::cout << nc << std::endl;
- while (not nc.is_empty())
- {
- if (nc.top() % 2 == 0)
- std::cout << "deleted: " << nc.pop_front() << std::endl;
- else
- std::cout << "deleted: " << nc.pop() << std::endl;
- }
- std::cout << nc << std::endl;
- std::cout << "push milion element in nc" << std::endl;
- for (int i = 0; i < 1000000; i++)
- nc.push(i);
- std::cout << "clear nc" << std::endl;
- nc.clear();
- std::cout << nc << std::endl;
- std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement