Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename ElemT>
- class ListFw
- {
- class Node
- {
- ElemT data;
- Node* next;
- public:
- Node(const ElemT& dataP, Node* nextP = nullptr) : data{ dataP }, next{ nextP }{}
- friend ListFw;
- template <typename ElemT>
- friend std::ostream& operator<<(std::ostream& out, const ListFw<ElemT>& list);
- template <typename ElemT>
- friend ListFw<ElemT> operator+(const ListFw<ElemT>& lhs, const ListFw<ElemT>& rhs);
- template <typename ElemT>
- friend ListFw<ElemT> operator*(const ListFw<ElemT>& lhs, const ListFw<ElemT>& rhs);
- };
- Node* head;
- Node* tail;
- size_t size;
- Node* getNode(size_t idx);
- public:
- ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
- ListFw(const ListFw& listP) : head{ nullptr }, tail{ nullptr }, size{ 0 }
- {
- for (auto curr{ listP.head }; curr; curr = curr->next)
- {
- push_back(curr->data);
- }
- std::cout << "Copy-shmopy!\n";
- };
- ListFw(ListFw&& listP) : head{ listP.head }, tail{ listP.tail }, size{ listP.size }
- {
- listP.head = nullptr;
- listP.tail = nullptr;
- listP.size = 0;
- std::cout << "I liket to mov'it, mov'it! :[]\n";
- };
- ListFw(const std::initializer_list<ElemT> initList) : head{ nullptr }, tail{ nullptr }, size{ 0 }
- {
- for (const auto& elem : initList)
- {
- push_back(elem);
- }
- };
- bool empty()const { return size == 0; }
- size_t get_size()const { return size; }
- ElemT& front() { return head->data; }
- const ElemT& front()const { return head->data; }
- ElemT& back() { return tail->data; }
- const ElemT& back()const { return tail->data; }
- void push_front(const ElemT& data);
- void push_back(const ElemT& data);
- bool pop_front();
- bool pop_back();
- void insert_at(size_t idx, const ElemT& data);
- bool delete_at(size_t idx);
- void print()
- {
- if (!head) { std::cout << "[empty]"; }
- else
- {
- for (auto curr{ head }; curr; curr = curr->next)
- {
- std::cout << curr->data << ' ';
- }
- }
- std::cout << '\n';
- }
- void clear();
- ~ListFw();
- template <typename ElemT>
- friend std::ostream& operator<<(std::ostream& out, const ListFw<ElemT>& list);
- template <typename ElemT>
- friend ListFw<ElemT> operator+(const ListFw<ElemT>& lhs, const ListFw<ElemT>& rhs)
- {
- ListFw<ElemT> res;
- for (auto curr{ lhs.head }; curr; curr = curr->next)
- {
- res.push_back(curr->data);
- }
- for (auto curr{ rhs.head }; curr; curr = curr->next)
- {
- res.push_back(curr->data);
- }
- return res;
- };
- template <typename ElemT>
- friend ListFw<ElemT> operator*(const ListFw<ElemT>& lhs, const ListFw<ElemT>& rhs)
- {
- ListFw<ElemT> res;
- for (auto currL{ lhs.head }; currL; currL = currL->next)
- {
- for (auto currR{ rhs.head }; currR; currR = currR->next)
- {
- if (currL->data == currR->data)
- {
- res.push_back(currL->data);
- break;
- }
- }
- }
- return res;
- };
- };
- template <typename ElemT>
- std::ostream& operator<<(std::ostream& out, const ListFw<ElemT>& list)
- {
- if (!list.head) { out << "[empty]"; }
- else
- {
- for (auto curr{ list.head }; curr; curr = curr->next)
- {
- out << curr->data << ' ';
- }
- }
- return out;
- }
- template <typename ElemT>
- void ListFw<ElemT>::push_front(const ElemT& data)
- {
- head = new Node{ data, head };
- if (!tail) { tail = head; }
- ++size;
- }
- template <typename ElemT>
- void ListFw<ElemT>::push_back(const ElemT& data)
- {
- if (!head)
- {
- head = new Node{ data, head };
- tail = head;
- }
- else
- {
- tail->next = new Node{ data, nullptr };
- tail = tail->next;
- }
- ++size;
- }
- template <typename ElemT>
- bool ListFw<ElemT>::pop_front()
- {
- if (head)
- {
- Node* oldHead{ head };
- head = head->next;
- delete oldHead;
- --size;
- if (!size) { tail = nullptr; }
- return true;
- }
- return false;
- }
- template <typename ElemT>
- bool ListFw<ElemT>::pop_back()
- {
- if (tail)
- {
- if (tail == head)
- {
- delete tail;
- head = nullptr;
- tail = nullptr;
- size = 0;
- return true;
- }
- Node* curr{ head };
- for (; !(curr->next == tail); curr = curr->next);
- delete tail;
- curr->next = nullptr;
- tail = curr;
- --size;
- return true;
- }
- return false;
- }
- template <typename ElemT>
- void ListFw<ElemT>::clear()
- {
- while (head)
- {
- std::cout << "Clear for: " << front() << '\n';
- pop_front();
- }
- }
- template <typename ElemT>
- ListFw<ElemT>::~ListFw()
- {
- while (tail)
- {
- //std::cout << "Destruct for: " << back() << '\n';
- pop_back();
- }
- }
- template <typename ElemT>
- void printListState(const ListFw<ElemT>& l)
- {
- std::cout << "Size: " << l.get_size() << '\n';
- std::cout << "Front: " << l.front() << '\n';
- std::cout << "Back: " << l.back() << '\n';
- }
- template <typename ElemT>
- typename ListFw<ElemT>::Node* ListFw<ElemT>::getNode(size_t idx)
- {
- Node* curr{ nullptr };
- size_t currIdx;
- if (idx >= 0 and idx < size)
- {
- for (curr = head, currIdx = 0; currIdx < idx; curr = curr->next, ++currIdx);
- }
- return curr;
- }
- template <typename ElemT>
- void ListFw<ElemT>::insert_at(size_t idx, const ElemT& data)
- {
- Node* prev{ getNode(idx - 1) };
- Node* curr{ prev ? prev->next : nullptr };
- if (!curr or curr == head) { push_front(data); return; }
- else
- {
- prev->next = new Node{ data, curr };
- }
- ++size;
- }
- template <typename ElemT>
- bool ListFw<ElemT>::delete_at(size_t idx)
- {
- if (idx == 0) { pop_front(); return true; }
- Node* prev{ getNode(idx - 1) };
- Node* curr{ prev ? prev->next : nullptr };
- if (curr)
- {
- if (curr == head) { head = curr->next; }
- else
- {
- if (curr->next) { prev->next = curr->next; }
- else { tail = prev; prev->next = nullptr; }
- }
- --size;
- delete curr;
- return true;
- }
- return false;
- }
- int main()
- {
- ListFw<int> a;
- a.push_back(1);
- a.push_back(2);
- a.push_back(3);
- a.push_back(4);
- std::cout << "a -> " << a << '\n';
- ListFw<int> b{ a };
- std::cout << "b -> " << b << '\n';
- ListFw<int> c;
- c.push_back(5);
- c.push_back(6);
- c.push_back(7);
- c.push_back(8);
- std::cout << "c -> " << c << '\n';
- ListFw<int> d{ b + c };
- std::cout << "d -> " << d << '\n';
- ListFw<int> e{ 1,3,5,7,3,5,7,1 };
- ListFw<int> f{ 2,1,4,6,7,1,6,7 };
- std::cout << "e -> " << e << '\n';
- std::cout << "f -> " << f << '\n';
- ListFw<int> g{ e * f };
- std::cout << "g -> " << g << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement