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;
- };
- Node* head;
- Node* tail;
- size_t size;
- Node* getNode(size_t idx);
- public:
- ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
- 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>
- 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> l;
- std::cout << "Size: " << l.get_size() << '\n';
- l.push_back(42);
- l.push_back(33);
- printListState(l);
- l.pop_front();
- l.pop_back();
- std::cout << "Size: " << l.get_size() << '\n';
- for (int i{ 0 }; i < 5; l.push_front(i++));
- printListState(l);
- l.clear();
- for (int i{ 0 }; i < 5; l.push_back(i++));
- printListState(l);
- l.insert_at(3, 42);
- l.print();
- l.delete_at(3);
- l.print();
- l.clear();
- l.print();
- l.insert_at(42, 33);
- l.print();
- l.delete_at(0);
- l.print();
- l.insert_at(-33, 42);
- l.print();
- l.delete_at(0);
- l.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement