Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename ElemT>
- class ListFw
- {
- class Node
- {
- public:
- ElemT data;
- Node* next;
- Node(const ElemT& dataP, Node* nextP = nullptr)
- :data{ dataP }, next{ nextP }{};
- };
- Node* head;
- Node* tail;
- size_t size;
- public:
- ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
- ElemT& front() { return head->data; };
- const ElemT& front()const { return head->data; };
- ElemT& back() { return tail->data; };
- const ElemT& back()const { return tail->data; };
- const size_t getSize() const { return size; };
- bool empty() const { return size == 0; };
- void push_back(const ElemT& data);
- void push_front(const ElemT& data);
- bool pop_back();
- bool pop_front();
- void clear();
- ListFw clone();
- ListFw operator+(ListFw& object);
- ListFw operator*(ListFw& object);
- void operator=(ListFw& object)
- {
- clear();
- for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
- push_front(curr->data);
- }
- }
- void operator=(ListFw&& object)
- {
- clear();
- size=object.size;
- head=object.head;
- tail=object.tail;
- object.head=nullptr;
- object.tail=nullptr;
- }
- ListFw(ListFw& object)
- {
- size=object.size;
- for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
- push_front(curr->data);
- }
- }
- ~ListFw();
- };
- template <typename ElemT>
- void ListFw<ElemT>::push_back(const ElemT& data)
- {
- if (!head)
- {
- head = new Node{ data };
- tail = head;
- }
- else
- {
- tail->next = new Node{ data };
- tail = tail->next;
- }
- ++size;
- }
- template <typename ElemT>
- void ListFw<ElemT>::push_front(const ElemT& data)
- {
- head = new Node{ data, head };
- ++size;
- if (!tail)
- {
- tail = head;
- }
- }
- template <typename ElemT>
- bool ListFw<ElemT>::pop_front()
- {
- if (head)
- {
- Node* tmp{ head };
- head = head->next;
- delete tmp;
- --size;
- return true;
- }
- return false;
- }
- template <typename ElemT>
- bool ListFw<ElemT>::pop_back()
- {
- if (tail)
- {
- if (tail == head)
- {
- delete tail;
- size = 0;
- tail = nullptr;
- head = nullptr;
- return true;
- }
- //Node* curr{ head };
- //while (curr)
- for (Node* curr{ head }; curr; curr = curr->next)
- {
- if (curr->next == tail)
- {
- delete tail;
- curr->next = nullptr;
- tail = curr;
- --size;
- return true;
- }
- //curr = curr->next;
- }
- }
- return false;
- }
- template <typename ElemT>
- void ListFw<ElemT>::clear()
- {
- while (tail)
- {
- std::cout << "Clear for: " << back() << '\n';
- pop_back();
- }
- }
- template <typename ElemT>
- ListFw<ElemT>::~ListFw()
- {
- while (head)
- {
- std::cout << "Destruct for: " << front() << '\n';
- pop_front();
- }
- }
- template<class ElemT>
- ListFw<ElemT> ListFw<ElemT>::clone()
- {
- ListFw objectT;
- objectT.size = size;
- for (Node* curr{ head }; curr!= nullptr; curr = curr->next) {
- objectT.push_back(curr->data);
- }
- objectT.size = size;
- return objectT;
- }
- template<class ElemT>
- ListFw<ElemT> ListFw<ElemT>::operator+(ListFw<ElemT>& object)
- {
- ListFw objectTest;
- objectTest = clone();
- for (Node* curr{object.head}; curr!=nullptr; curr=curr->next) {
- objectTest.push_back(curr->data);
- }
- size+=object.size;
- return objectTest;
- }
- template<class ElemT>
- ListFw<ElemT> ListFw<ElemT>::operator*(ListFw<ElemT>& object)
- {
- ListFw objectNew;
- for (Node* currObject{object.head},curr{head}; currObject!=nullptr and curr!=nullptr; currObject=currObject->next,curr=curr->next) {
- if (curr->data == currObject->data)
- {
- objectNew.push_front(curr->data);
- }
- }
- return objectNew;
- }
- int main()
- {
- /*ListFw<int> l;
- std::cout << "Size: " << l.getSize() << '\n';
- l.push_back(42);
- l.push_back(33);
- std::cout << "Size: " << l.getSize() << '\n';
- std::cout << "Front: " << l.front() << '\n';
- std::cout << "Back: " << l.back() << '\n';
- l.pop_front();
- l.pop_back();
- std::cout << "Size: " << l.getSize() << '\n';
- l.push_back(1);
- l.push_back(2);
- l.push_back(3);
- l.push_back(4);
- std::cout << "Size: " << l.getSize() << '\n';
- std::cout << "Front: " << l.front() << '\n';
- std::cout << "Back: " << l.back() << '\n';
- l.clear();
- std::cout << "Size: " << l.getSize() << '\n';
- l.push_front(1);
- l.push_front(2);
- l.push_front(3);
- l.push_front(4);
- std::cout << "Size: " << l.getSize() << '\n';
- std::cout << "Front: " << l.front() << '\n';
- std::cout << "Back: " << l.back() << '\n';*/
- ListFw<int> test1;
- test1.push_front(1);
- test1.push_back(2);
- ListFw<int> test2;
- test2.push_front(3);
- test2.push_back(4);
- ListFw<int> test3;
- test3 = test2.clone(); //If you want to check code, uncomment from 235 to 237
- //std::cout << "Size: " << test3.getSize() << '\n';
- //std::cout << "Front: " << test3.front() << '\n';
- //std::cout << "Back: " << test3.back() << '\n';
- ListFw<int> test4; //If you want to check code, uncomment from 235 to 237
- test4=test1+test2;
- //std::cout << "Size: " << test4.getSize() << '\n';
- //std::cout << "Front: " << test4.front() << '\n';
- //std::cout << "Back: " << test4.back() << '\n';
- ListFw<int> test5; //Empty be after actions
- test5=test1*test2;
- std::cout << "Size: " << test5.getSize() << '\n';
- std::cout << "Front: " << test5.front() << '\n';
- std::cout << "Back: " << test5.back() << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement