mardi 3 novembre 2015

Linked list implementation using Templates in C++

I am new to C++ and have been studying data structures lately. I have created linked lists in the following way:

Class Random{
private:
   struct Node{
          int data;
          Node* next;
         };
} 

But i came across a piece of code that is doing the same thing in the following way:

template<Typename T>
 struct Listnode{
      T data;
      shared_ptr<ListNode<T>> next;
};

I looked it up and found that we use templates when we want to have multiple data types. Like now we can use int, double, float instead of "T". Whereas in the former case we could only use int. However, I dont understand how:

Node* next

is the same as:

shared_ptr<ListNode<T>> next

and how will these be called, I know for the former we use the:

Node->next = new Node;
Node->data = randomdata;

How does it work for the former way. Another thing of the two implementations, which one is better and why? Thanks

Aucun commentaire:

Enregistrer un commentaire