Hey guys,
I think I know what the answer to this is going to be, but I should ask, maybe somebody will see something I am not seeng that would make this much easier.
I have implemented the architecture for a linked list container. As anyone that has ever taken a class in data structures will know, this basically consists of two classes. Mine is somewhat different from the norm in that the node class does not contain any actual place to hold data, it's meant to be inherited by another class.
This is the node class
and this is the control classCode:template <typename ELEMENT> class ListNode { public: ListNode(){}; ListNode(const ListNode<ELEMENT> &source); const ListNode<ELEMENT>& operator=(const ListNode<ELEMENT> &source); virtual ~ListNode(){}; private: ListNode<ELEMENT> *Next; ListNode<ELEMENT> *Previous; };
(ignore the base class here, it's only there so I can put the bulk of the code into the cpp file rather than the h file [since as we all know, you cannot put a template class code into the cpp file]).Code:template <typename ELEMENT> class LinkedList : public LinkedListBase { public: LinkedList(){}; LinkedList(const LinkedList<ELEMENT> &source); const LinkedList<ELEMENT>& operator=(const LinkedList<ELEMENT> &source); virtual ~LinkedList(){}; void push_back(const <ELEMENT> &Node); void push_front(const <ELEMENT> &Node); //and so on private: ListNode<ELEMENT> Root; };
The usage of these classes is like such. First, you define your class and inherit the ListNode class:
Code:class SomeDataClass : public ListNode<SomeDataClass> { int SomeInt; float SomeFloat; char SomeString[10]; };
Then you instance this class like so:
LinkedList<SomeDataClass> MyListOfData;
Okay? All that works just fine. But, you may see the problem already, given the name of this thread.
We can't use this method with built in data types (like int, float, double, and so forth), because there is no way for them to inherit ListNode. You can declare them:
LinkedList<int> MyListOfIntData;
And that will give you the functions in LinkedList, but it will be missing the Next/Previous pointers that are central to the concept of a linked list
Possible solutions:
1) we put the data right into the ListNode class, simply adding ELEMENT* Data to the definition. Then you would no longer need to inherit this class. However, I dislike that idea, because it almost *requires* dynamic memory allocation (we have a pointer, but no actual place to put the data).
2) We simply disallow built in types. If you want a linked list, you must define the data you want in a class. This is a limitation, but it may not be as harsh as you first think. If you need a list of built in types, what you probably want is an array rather than a linked list anyway. But still, it's a limit.
3) We could specialize the template class, like such:
The problem with that approach is that we'd need a specialized template for EACH base data type (int, float, double, char, bool, and so forth). Not impossible to do, but kind of a PITA. But that WOULD give us the ability to have linked lists based on the built in data types.Code:template <float> class ListNode public: //mostly as before private: ListNode<float> *Next; ListNode<float> *Previous; float Data; };
Now, I've done specialized template classes maybe twice in all my career, so maybe there is a better way to do that that I'm not previously aware of. If anyone knows, I'm all ears.
Assuming not, which option would be better?



Reply With Quote


