I Have been Searching the Internet For A while Now And could Not Find A Good Answer.
My Question Is Can We Make A Link List Of Which`s node Only Contains One Data Element e.g int float etc etc.... but the link list can contain different node types.... like this where [] is one node
template <class Head, class Tail>
class Node{
public:
Head data;
Tail* next;
};
The type of your list will then have to be Node<int, Node<float, Node<char, void> > >. I'm sure you can see how that would be a problem.
Another way would be
1 2 3 4 5
class Node{
public:
void* data;
Node* next;
};
But now you don't have a way to tell what points to what type.
An improvement would be to add a member std::type_info tp;. Then when inserting elements, you'd pass their typeid and when retrieving you'd use a function
1 2 3 4 5 6 7
template<class T>
bool getData( Node* n, T& result ){
if( n == 0 || typeid(T) != n->tp ) returnfalse;
T* ptr = (T*)n->data;
result = *ptr;
returntrue;
}
Note that either way this is hardly ever the right thing to do. If there is some problem you're trying to solve with this, post it here and we'll try to find a better solution.
void valuesetter()
{
cout<<" Enter Data the type of Data You Want To Enter "<<endl;
cout<<" 1.Int 2.Float 3.Char : ";
int input;
cin>>input;
cout<<endl;
if(input==1)
{
cout<<" Enter The Data You Want To Put ";
cin>>input;
i=new int;
*i=input;
}
if(input==2)
{
double a;
cout<<" Enter The Data You Want To Put ";
cin>>a;
f=new double;
*f=a;
}
if(input==3)
{
char a;
cout<<" Enter The Data You Want To Put ";
cin>>a;
c=new char;
*c=a;
}
}
Of course a templated linked list is possible. Here's a basic outline of one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
template< typename T >
struct node {
T data;
node<T> * next;
};
template< typename T >
class List {
public:
List();
~List();
private:
node<T> * first;
node<T> * last;
int size;
};
And then each function and constructor within the class would need to be templated.
1 2 3 4
template< typename T >
List<T>::List() {
// Constructor
}
The only problem I've run into is that if you try to write the constructor/function definitions in a separate CPP file, the template will cause problems unless you include lines like this at the bottom:
The only problem with that is that you will only be able to create a List object of those types, limiting the types of objects you can store to what you template at the bottom of your CPP file.
The only solution I've found to this is to put the class function/constructor definitions within the header file with the class declaration. It is also a good idea to enclose everything in a namespace.
@packetpirate: Your templated list is for single-type lists. Once 'T' is assigned a true type 'Tx', *first and *last will also be defined as Node<Tx>, which is not the point. hamsterman's solution avoids this by providing another level of abstraction.
void makelist()
{
void *temp=first;
int choice;
cout<<"Which Type of Data You Want To Enter "<<endl;
cout<<" 1.Int 2.float 3.char : ";
cin>>choice;
if(choice==1)
{
if(temp==NULL)
{
Node<int> *temp2 = new Node<int>;
strcpy(type,"int");
cout<<"Enter int Data : ";
cin>>temp2->data;
first=temp2;
last=temp2;
}