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;
- 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 clear();
- ~ListFw();
- void print()
- {
- for (auto curr{ head }; curr; curr = curr->next)
- {
- std::cout << curr->data << ' ';
- }
- std::cout << '\n';
- }
- //template <typename ElemT>
- //int ListFw<ElemT>::find(const ElemT elem) const {
- // int indx{ 0 };
- // for (Node* curr{ head }; curr->data != elem; curr = curr->next, ++indx) { if (!curr->next) { return -1; } }
- // return indx;
- //}
- int findElement(const ElemT elem)
- {
- auto curr{ head };
- int idx{ 0 };
- for (; curr; curr = curr->next, ++idx)
- {
- if (curr->data == elem)
- {
- return idx;
- }
- }
- return -1;
- }
- };
- 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()
- {
- // BAD! VERY INEFFICIENT!
- 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';
- }
- int getRandom(int start, int end)
- {
- return start + rand() % (end - start);
- }
- int main()
- {
- const int maxElements{ 15 };
- ListFw<int> l;
- for (int i{ 0 }; i < maxElements; l.push_back(getRandom(0,10)),++i);
- l.print();
- int key;
- std::cout << "Enter number to find in list[0..10]\n";
- std::cin >> key;
- int foundIdx{ l.findElement(key) };
- if (foundIdx < 0)
- {
- std::cout << "Number not in List!\n";
- }
- else
- {
- std::cout << "Found at idx: " << foundIdx << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement