Exemplo n.º 1
0
 public T this[int index] {
     get {
         if (index < 0 || index > this.Count)
         {
             throw new IndexOutOfRangeException("Index is out of range");
         }
         MyCustomCollectionValues <T> pointer = head;
         int size = 0;
         while (index != size++)
         {
             pointer = pointer.next;
         }
         return(pointer.value);
     }
     set {
         if (index < 0 || index > this.Count)
         {
             throw new IndexOutOfRangeException("Index is out of range");
         }
         MyCustomCollectionValues <T> pointer = head;
         int size = 0;
         while (index != size++)
         {
             pointer = pointer.next;
         }
         pointer.value = value;
     }
 }
Exemplo n.º 2
0
 public void Next()
 {
     if (this.current == null || this.current.next == null)
     {
         throw new IndexOutOfRangeException("Index is out of range");
     }
     current = current.next;
 }
Exemplo n.º 3
0
        public void Add(T item)
        {
            MyCustomCollectionValues <T> newItem = new MyCustomCollectionValues <T>();

            newItem.next  = null;
            newItem.value = item;
            if (head == null)
            {
                head    = new MyCustomCollectionValues <T>();
                current = new MyCustomCollectionValues <T>();
                head    = newItem;
            }
            this.current.next = newItem;
            this.current      = newItem;
        }
Exemplo n.º 4
0
        public void Remove(T item)
        {
            bool flagSucceed = false;

            if (!isEmpty())
            {
                MyCustomCollectionValues <T> pointer = head;
                if (item.Equals(pointer.value))
                {
                    head = pointer.next;
                    if (pointer == current)
                    {
                        current = pointer.next;
                    }
                    flagSucceed = true;
                }
                else
                {
                    while (pointer.next != null && flagSucceed == false)
                    {
                        if (item.Equals(pointer.next.value))
                        {
                            pointer.next = pointer.next.next;
                            if (pointer.next == current)
                            {
                                current = pointer.next.next;
                            }
                            flagSucceed = true;
                        }
                        else
                        {
                            pointer = pointer.next;
                        }
                    }
                }
                if (flagSucceed == false)
                {
                    throw new Exception("There is no such an item in collection!");
                }
            }
            else
            {
                throw new ArgumentNullException("Collection is Empty!");
            }
        }
Exemplo n.º 5
0
        public T RemoveCurrent()
        {
            MyCustomCollectionValues <T> saveItem = current;
            MyCustomCollectionValues <T> pointer  = head;

            if (pointer == current)
            {
                head = current = pointer.next;
            }
            else
            {
                while (pointer != null)
                {
                    if (pointer.next == current)
                    {
                        pointer.next = current.next;
                        current      = pointer;
                        break;
                    }
                    pointer = pointer.next;
                }
            }
            return(saveItem.value);
        }
Exemplo n.º 6
0
 public void Reset()
 {
     this.current = head;
 }
Exemplo n.º 7
0
 public MyCustomCollection()
 {
     head    = null;
     current = null;
 }