ccmny

MyList

Jun 4th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5.  
  6. namespace DataTypes
  7. {
  8.    
  9.  
  10.     class MyList<T> : IEnumerable<T>
  11.     {
  12.         public delegate void ProcessDelegate(T item);
  13.  
  14.         Node first;
  15.         Node last;
  16.  
  17.         int n = 0;
  18.         public int N
  19.         {
  20.             get { return n; }
  21.         }
  22.  
  23.         private class Node
  24.         {
  25.             public T item;
  26.             public Node previous;
  27.             public Node next;
  28.         }
  29.  
  30.         public bool IsEmpty { get { return first == null; } }
  31.        
  32.         public void Add(T item)
  33.         {
  34.             Node oldLast = last;
  35.             last = new Node();
  36.             last.item = item;
  37.             last.previous = oldLast;
  38.             last.next = null;
  39.  
  40.             if (IsEmpty)
  41.                 first = last;
  42.             else
  43.                 oldLast.next = last;
  44.             n++;            
  45.         }
  46.  
  47.         public T Get(int index)
  48.         {
  49.             int i = 0;
  50.             foreach (T item in this)
  51.             {
  52.                 if (i == index)
  53.                 {
  54.                     return item;
  55.                 }
  56.                 i++;
  57.             }
  58.             return default(T);
  59.         }
  60.  
  61.         public void Set(int index, T itemIn)
  62.         {
  63.             int i = 0;
  64.             Node current = first;
  65.  
  66.             while (current != null)
  67.             {
  68.                 if (i == index)
  69.                 {
  70.                     current.item = itemIn;
  71.                     break;
  72.                 }
  73.                 current = current.next;
  74.                 i++;
  75.             }
  76.         }
  77.  
  78.         public int IndexOf(T item)
  79.         {
  80.             int i = 0;
  81.             foreach (T it in this)
  82.             {
  83.                 if (item.Equals(it))
  84.                 {
  85.                     return i;
  86.                 }
  87.                 i++;
  88.             }
  89.             return -1;
  90.         }
  91.  
  92.         public void Remove(T item)
  93.         {
  94.             if (this.IsEmpty) return;
  95.             for (Node node = first; node != null; node = node.next)
  96.             {
  97.                 if (item.Equals(node.item))
  98.                 {
  99.                     if (node.next != null && node.previous != null)
  100.                     {
  101.                         node.previous.next = node.next;
  102.                         node.next.previous = node.previous;
  103.                     }
  104.                     else
  105.                     {
  106.                         if (node.next == null)
  107.                         {
  108.                             node.previous.next = null;
  109.                             last = node.previous;
  110.                         }
  111.                         else
  112.                         {
  113.                             node.next.previous = null;
  114.                             first = node.next;
  115.                         }
  116.                     }
  117.                     break;
  118.  
  119.                 }
  120.             }
  121.             n--;
  122.         }
  123.  
  124.         public void AddRange(MyList<T> that)
  125.         {
  126.             if (!that.IsEmpty && !this.IsEmpty)
  127.             {
  128.                 this.last.next = that.first;
  129.                 that.first.previous = this.last;
  130.                 this.last = that.last;
  131.             }
  132.             else if (this.IsEmpty && !that.IsEmpty)
  133.             {
  134.                 this.first = that.first;
  135.             }            
  136.             n += that.n;
  137.         }
  138.  
  139.         public static MyList<T> operator + (MyList<T> listIn1, MyList<T> listIn2)
  140.         {
  141.             MyList<T> listOut = new MyList<T>();
  142.             foreach (T item in listIn1)
  143.             {
  144.                 listOut.Add(item);
  145.             }
  146.             foreach (T item in listIn2)
  147.             {
  148.                 listOut.Add(item);
  149.             }
  150.             return listOut;
  151.         }
  152.  
  153.         public static MyList<T> operator +  (MyList<T> listIn, T item)
  154.         {
  155.             listIn.Add(item);
  156.             return listIn;
  157.         }
  158.  
  159.         public T this[int index]
  160.         {
  161.             get { return this.Get(index); }
  162.             set { this.Set(index, value); }
  163.         }
  164.  
  165.         public void each(ProcessDelegate function)
  166.         {
  167.             foreach (T item in this)
  168.             {
  169.                 function(item);
  170.             }
  171.         }
  172.  
  173.         public IEnumerator<T> GetEnumerator()
  174.         {
  175.             Node current = first;
  176.  
  177.             while (current != null)
  178.             {
  179.                 yield return current.item;
  180.                 current = current.next;
  181.             }
  182.         }
  183.  
  184.         IEnumerator IEnumerable.GetEnumerator()
  185.         {
  186.             return GetEnumerator();
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment