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);
- };
- 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>
- friend std::ostream& operator<<(std::ostream& out, const ListFw<ElemT>& list);
- };
- 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;
- }
- template <typename ElemT>
- class Queue
- {
- ListFw<ElemT> storage;
- size_t capacity; // if capUnlimit unlimited elements in queue
- static const uint8_t capUnlimit{ 0 };
- public:
- Queue(size_t capacityP = capUnlimit) : capacity{ capacityP }{}
- size_t getSize()const { return storage.get_size(); }
- size_t getCapacity()const { return capacity; }
- Queue& setCapacity(size_t capacityP)
- {
- while (storage.get_size() > capacityP) { storage.pop_back(); } // remove extra elements if neded
- capacity = capacityP;
- return *this;
- }
- bool empty()const { return storage.empty(); }
- bool full()const { return capacity == capUnlimit ? false : storage.get_size() == capacity; }
- ElemT& front() { return storage.front(); }
- const ElemT& front()const { return storage.front(); }
- bool push(const ElemT& elem)
- {
- if (!full())
- {
- storage.push_back(elem);
- return true;
- }
- return false;
- }
- bool pop()
- {
- if (!empty())
- {
- storage.pop_front();
- return true;
- }
- return false;
- }
- template <typename ElemT>
- friend std::ostream& operator<<(std::ostream& out, const Queue<ElemT> list);
- };
- template <typename ElemT>
- std::ostream& operator<<(std::ostream& out, const Queue<ElemT> list)
- {
- return out << list.storage;
- }
- int main()
- {
- Queue<int> queue{10};
- std::cout << queue << '\n';
- std::cout << "Enqueue...\n";
- for (size_t i{ 0 }; !queue.full(); ++i)
- {
- queue.push(i);
- std::cout << i << '\n';
- }
- std::cout << "\nFront of queue is: " << queue.front() << "\n\n";
- std::cout << "Size of queue is: " << queue.getSize() << "\n\n";
- queue.setCapacity(5);
- std::cout << "\nFront of queue is: " << queue.front() << "\n\n";
- std::cout << "Size of queue is: " << queue.getSize() << "\n\n";
- std::cout << "Dequeue...\n";
- for (; !queue.empty(); queue.pop())
- {
- std::cout << queue.front() << '\n';
- }
- queue.pop();
- std::cout << queue << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement