Hello, i need to write a template with Nodes containing data with 2 data structures : a map and a minimum heap, both got the same nodes in it and every 2 same nodes are connected. the problem is that i need the heap to know the node fields for the heapify for example, and i don't know what's the right way to do so, friends? public fields in Node? writing the Node inside the heap? using getters and setters? thanks all for your help.
+1
A:
Well, a linked list might be laid out like this:
namespace my_namespace
{
namespace detail
{
template <class T>
struct Node
{
T value;
Node* previous;
Node* next;
//constructors and other things that might help
};
}
template <class T>
class LinkedList
{
private:
detail::Node<T>* head;
public:
//all it does
};
}
There is no particular reason to hide the Node struct from the user or the LinkedList class (putting it into a detail namespace should be more than enough): LinkedList needs it and the Node itself is pretty much useless to the user. All encapsulation is up to LinkedList to achieve: it just shouldn't give out it's head (or any other Node*
).
UncleBens
2010-09-19 11:13:59
Thanks. it was helpful.
Roy Gavrielov
2010-09-19 11:25:30
You should only put class Node in the detail namespace thou.
Viktor Sehr
2010-09-19 11:33:15
@Victor: Indeed, fixed.
UncleBens
2010-09-19 12:54:03