Friday, 12 July 2013

Microsoft .Net Framework implementation of Generics in Custom Linked List C#

What is Generics ?

Generics is the Concept of defining a common Template for all kind of data type. It ensure the Type Safety.
Same class or same Algorithm can be derived for all kind of Data Types . generally it is defined  by "T" character.

Feature and Advancement of Generics are , 
1. It increases the Performance of a application.
2. Reusable code.

Let's we see the Generics concepts implemented in Linked List
LinkedList  is a data structure used to store the data in linear manner. LinkedList Node stores the data internally, Node consists of additional information which will have the address of next item which is added to the linked list.

Data's are iterate from head to the tail using the address of next item ,which is present in the current node
Here a,b,c,d,e,f are the data's .

a is stored in address  1000, Point the next data by there address 2000
is stored in address  2000, Point the next data by there address 3000
is stored in address  3000, Point the next data by there address 4000
is stored in address  4000, Point the next data by there address 5000
is stored in address  5000, Point the next data by there address 6000
is stored in address   6000, Point the next data by there address 7000





So Let we create a Common LinkedListNode class which will support all data type.

Now we create a custom Linked list with one method "Add" and iterate the data through foreach 

class Program
    {
    
    
        public static void Main(string[] args)
        {
            LinkedList<int> list1=new LinkedList<int>();
            list1.Add(2);
            list1.Add(4);
            list1.Add(5);
            foreach(int j in list1)
                Console.WriteLine("Number {0}",j);
        
            LinkedList<string> list2=new LinkedList<string>();
            list2.Add("C#");
            list2.Add("Java");
            list2.Add("Sql");
            foreach(string k in list2)
                Console.WriteLine("String {0} ",k);
                    
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }

output




We see the implementation of Generics in LinkedListNode . Node consists of Value which will store the data and Prev , Next store the address of Previous and Next Value.

      public class LinkedListNode<T>
    {
        private T _value;
        public T Value
        {
            get {return _value;}
        }
      
        private LinkedListNode<T> _next;
        public LinkedListNode
<T> Next
        {
            get{return _next;}
            internal set{_next = value;}
        }
      
        private LinkedListNode
<T> _prev;
        public LinkedListNode<T> Prev
        {          
            get{return _prev;}
            internal set{_prev = value;}
        }
      
        public LinkedListNode(T value)
        {
            this._value = value;  
        }              
    }
   
Now the LinkedList will store the LinkedListNode on addition of each value.This class is implemented simply actually it can be implemented by adding more methods and properties. like Remove,Exists,Length etc.

   public class LinkedList
<T>:IEnumerable<T>
    {
        private LinkedListNode
<T> _first;
        public LinkedListNode<T> First
        {
            get {return _first;}
        }
      
        private LinkedListNode
<T> _last;
        public LinkedListNode<T> Last
        {          
            get {return _last;}
        }
      
        public void Add(T value)
        {
            LinkedListNode
<T> node=new LinkedListNode<T>(value);
            if(_first==null)
            {
                _first = node;  /* Now assign the value to first node */
                _last = _first;
            }
            else
            {
                _last.Next = node;
                _last=node;
            }
        }
      
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
      
        public IEnumerator GetEnumerator
<T>()
        {
            LinkedListNode
<T> cur=_first;
            while(cur!=null)
            {
                yield return cur.Value;
                cur = cur.Next;
            }
        }
    }
     From this article we can see the basic information of custom implementation of Generic LinkedList .