linked list - C++ Is it possible to create a private link between objects? -
as far can understand, linked list can implemented outsider class. because class can't have member varable of it's own type , node list need type. problem is, if link intented used specific class. if, link class created outside, available created standalone object.
it's okay if link class/struct pure link object because can used linking object type. but, in case need link has functionallity related object, public availability of pointless. , think it's better created private.
let's take @ this declaration:
#include <unordered_map> using namespace std; template<class t> class node { public: node(); node(const t& item, node<t>* ptrnext = null); t data; // access next node node<t>* nextnode(); // list modification methods void insertafter(node<t>* p); node<t>* deleteafter(); node<t> * getnode(const t& item, node<t>* nextptr = null); private: node<t> * next; unordered_map<string, t*> nodelist; };
the unordred_map<string,t*>
member can have meaning object. so, pointless node
class available outside.
is possible? or maybe bad idea add non-generic funtionallity link class?
class { a* m_pnext = nullptr; public: inline a* next() { return m_pnext; } inline void set_next(a*ptr) { m_pnext = ptr; } } template <class type> class linkedlist { type *m_pfirst = nullptr; public: void add(type * ptr) { if ( nullptr == m_pfirst ) { m_pfirst = ptr; } else { type * n = m_pfirst, p = m_pfirst->next; while (nullptr != p) { n = p; p = n->next(); } n->set_next(ptr); } } };
plenty of room improvement, of course. i'll let exercise mind.
Comments
Post a Comment