Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <list>
- using namespace std;
- class Node
- {
- public:
- int data = NULL;
- Node* next = nullptr;
- Node* prev = nullptr;
- Node() = default;
- Node(int data)
- : data(data)
- {
- }
- };
- class Babajua
- {
- public:
- Node* first = nullptr;
- Node* last = nullptr;
- unsigned actual_size = 0;
- ~Babajua()
- {
- Node* i_next;
- for (Node* i = first; i != nullptr; i)
- {
- i_next = i->next;
- delete i;
- i = i_next;
- }
- first = nullptr;
- }
- Node* operator [](int index)
- {
- if (index < 0 || index > this->actual_size)
- throw IndexOutOfRange();
- Node* c = this->first;
- c = this->first;
- for (int i = 0; i <= index; i++)
- {
- c = c->next;
- }
- return c;
- }
- bool is_empty() const { return this->first == nullptr; }
- void push_back(int data)
- {
- this->actual_size++;
- if (this->is_empty())
- {
- this->first = this->last = new Node(data);
- return;
- }
- Node* previously_last = this->last;
- this->last = new Node(data);
- previously_last->next = this->last;
- }
- void pop_back()
- {
- if (this->is_empty())
- throw Empty();
- Node* tmp = this->last;
- this->last = this->operator[](--this->actual_size);
- this->last->next = nullptr;
- delete tmp;
- }
- void push_front(int value)
- {
- }
- void pop_front()
- {
- }
- /*friend ostream& operator << (ostream& output, Babajua& object)
- {
- return output << object.first->data;
- }*/
- class IndexOutOfRange : public exception
- {
- const char* what() throw()
- {
- return "Index out of range";
- }
- };
- class Empty : public exception
- {
- const char* what() throw()
- {
- return "List is empty";
- }
- };
- };
- class Stack : private Babajua
- {
- private:
- using Babajua::actual_size;
- using Babajua::first;
- using Babajua::last;
- public:
- using Babajua::pop_back;
- using Babajua::push_back;
- using Babajua::is_empty;
- };
- class Queue : private Babajua
- {
- private:
- using Babajua::actual_size;
- using Babajua::first;
- using Babajua::last;
- public:
- using Babajua::pop_back;
- using Babajua::is_empty;
- void enqueue(int value)
- {
- Babajua::push_front(value);
- }
- int First() { return this->first->data; }
- };
- ostream& operator << (ostream& output, Babajua& object)
- {
- return output << object.first->data;
- }
- int main() {
- Queue i;
- i.enqueue(25);
- cout << i.First();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement