ccmny

CircularList

Jul 4th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. class CircularList<T> : IEnumerable<T>
  2.     {
  3.         private class Node
  4.         {
  5.             public T item;
  6.             public Node next;
  7.             public Node previous;
  8.         }
  9.  
  10.         Node current;
  11.         int count;
  12.  
  13.         public int Count
  14.         {
  15.             get { return count; }
  16.             set {  }
  17.         }
  18.  
  19.         public CircularList()
  20.         {
  21.  
  22.         }
  23.  
  24.         public void Add(T item)
  25.         {
  26.             if (count == 0)
  27.             {
  28.                 current = new Node();
  29.                 current.item = item;
  30.                 current.next = current;
  31.                 current.previous = current;
  32.                 count++;
  33.                 return;
  34.             }
  35.             Node nodeNew = new Node();
  36.             nodeNew.item = item;
  37.             nodeNew.previous = current.previous;
  38.             nodeNew.next = current;
  39.             current.previous.next = nodeNew;
  40.             current.previous = nodeNew;
  41.             count++;
  42.         }
  43.  
  44.         public T GetCurrent()
  45.         {
  46.             if (count < 1)
  47.             {
  48.                 throw new IndexOutOfRangeException("List is empty.");
  49.             }
  50.             return current.item;
  51.         }
  52.  
  53.         public void Next()
  54.         {
  55.             if (count < 1)
  56.             {
  57.                 throw new IndexOutOfRangeException("List is empty.");
  58.             }
  59.             current = current.next;
  60.         }
  61.  
  62.         public void Previous()
  63.         {
  64.             if (count < 1)
  65.             {
  66.                 throw new IndexOutOfRangeException("List is empty.");
  67.             }
  68.             current = current.previous;
  69.         }
  70.  
  71.         public void Remove()
  72.         {
  73.             if (count < 1)
  74.             {
  75.                 throw new IndexOutOfRangeException("List is empty.");
  76.             }
  77.             current.previous.next = current.next;
  78.             current.next.previous = current.previous;
  79.             current = current.next;
  80.             count--;
  81.         }
  82.  
  83.         public IEnumerator<T> GetEnumerator()
  84.         {
  85.             Node start = this.current;
  86.  
  87.             do
  88.             {
  89.                 yield return current.item;
  90.                 current = current.next;
  91.             }
  92.             while (current != start);
  93.         }
  94.  
  95.         IEnumerator IEnumerable.GetEnumerator()
  96.         {
  97.             return GetEnumerator();
  98.         }
  99.     }
Advertisement
Add Comment
Please, Sign In to add comment