#ifndef LIST_T #define LIST_T #include #include #include template struct ListNodeT { T data; std::shared_ptr> next; }; template class ListT{ public: ListT() = default; ~ListT() = default; ListT(const ListT & other) = delete; ListT & operator =(const ListT & other) = delete; // probably not the best semantics for insert, but it will do. // If the list is empty, insert at the front. // If the current pointer is null insert at the front // othewise insert after current. void Insert(T data){ // the auto is prefered here. auto newNode = std::make_shared>(); newNode->data = data; size++; if (head == nullptr) { head = newNode; } else if (current == nullptr) { newNode->next = head; head = newNode; } else { newNode->next = current->next; current->next = newNode; } } // if current is not null, delete what current is pointing to. bool Delete() { std::shared_ptr> tmp; bool success{false}; if (head == nullptr or current == nullptr) { return false; } if ( current == head ) { current = nullptr; head = head->next; success = true; size--; } else { for(tmp = head; tmp!= nullptr and tmp->next != current; tmp=tmp->next); if (tmp != nullptr) { tmp->next = current->next; current = nullptr; success = true; size--; } } return success; } T Get() const { if (current == nullptr) { throw std::range_error{"Bad Pointer"}; } return current->data; } int Size() const { return size; } void Home(){ current = head; } void Next() { if (current != nullptr) { current = current ->next; } } bool Last() { return current == nullptr; } private: std::shared_ptr> head; std::shared_ptr> current; int size{0}; }; #endif