0

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.

Roy Gavrielov
  • 607
  • 4
  • 10
  • 22
  • 1
    Uhm, dude? Same as [how to implement minheap using template](http://stackoverflow.com/questions/3745194/how-to-implement-minheap-using-template)? – Konrad Rudolph Sep 19 '10 at 11:08
  • Does this answer your question? [how to implement minheap using template](https://stackoverflow.com/questions/3745194/how-to-implement-minheap-using-template) – trincot Nov 22 '21 at 20:52

1 Answers1

1

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
  • 40,819
  • 6
  • 57
  • 90