Пример #1
0
        public void Print()
        {
            CustomLinkedListNode <T> temp = _head;

            while (temp != null)
            {
                Console.WriteLine(temp); temp = temp.Next;
            }
        }
Пример #2
0
 public T this[T val]
 {
     get { return(Find(val)); }
     set
     {
         CustomLinkedListNode <T> v = FindNode(val);
         v.Value = value;
     }
 }
Пример #3
0
 public CustomLinkedList(int initialSize)
 {
     _length = 0;
     _head   = null;
     // Add elements to list
     for (int i = 0; i < initialSize; i++)
     {
         AddFirst(new CustomLinkedListNode <T>()); //default(CustomLinkedListNode<T>));
     }
 }
Пример #4
0
 public void Clear()
 {
     if (_head != null)
     {
         for (; _length > 0; _length--)
         {
             this[_length - 1] = default(T);
         }
         _head = null;
     }
     _length = 0;
 }
Пример #5
0
 private void AddLast(CustomLinkedListNode <T> value)
 {
     if (_length == 0)
     {
         _head = value;
     }
     else
     {
         CustomLinkedListNode <T> last = FindNode(_length - 1);
         last.Next          = value;
         last.Next.Previous = last;
     }
     _length++;
 }
Пример #6
0
        private CustomLinkedListNode <T> FindNode(T value)
        {
            CustomLinkedListNode <T> temp = _head;

            for (int i = 0; i < _length; i++)
            {
                if (temp.Value.Equals(value))
                {
                    return(temp);
                }
                temp = temp.Next;
            }
            throw new IndexOutOfRangeException("The value you requested did not exist in the list.");
        }
Пример #7
0
        private CustomLinkedListNode <T> FindNode(int index)
        {
            if (index < 0 || index >= _length)
            {
                throw new IndexOutOfRangeException(string.Format("The index you requested was out of range. Requested index: {0}, Length of LinkedList: {1}.", index, _length));
            }
            CustomLinkedListNode <T> temp = _head;

            for (; index > 0; index--)
            {
                temp = temp.Next;
            }
            return(temp);
        }
Пример #8
0
 private void AddFirst(CustomLinkedListNode <T> value)
 {
     if (_length == 0)
     {
         _head = value;
     }
     else
     {
         CustomLinkedListNode <T> temp = _head;
         temp.Previous = value;
         _head         = value;
         _head.Next    = temp;
     }
     _length++;
 }
Пример #9
0
 private void Insert(int index, CustomLinkedListNode <T> value)
 {
     if (index == 0)
     {
         AddFirst(value);
     }
     else
     {
         CustomLinkedListNode <T> temp = FindNode(index);
         value.Previous     = temp.Previous;
         value.Next         = temp;
         temp.Previous.Next = value;
         temp.Previous      = value;
         _length++;
     }
 }
Пример #10
0
 public T this[int index]
 {
     get { return(Find(index)); }
     set
     {
         if (index == 0)
         {
             if (_head == null)
             {
                 _head = new CustomLinkedListNode <T>();
             }
             _head.Value = value;
         }
         else
         {
             CustomLinkedListNode <T> val = FindNode(index) ?? new CustomLinkedListNode <T>();
             val.Value = value;
         }
     }
 }
Пример #11
0
 private void Delete(CustomLinkedListNode <T> value)
 {
     if (value.Previous == null) // head
     {
         _head = value.Next;
         if (value.Next != null)
         {
             value.Next.Previous = _head;
         }
     }
     else
     {
         value.Previous.Next = value.Next;
         if (value.Next != null)
         {
             value.Next.Previous = value.Previous;
         }
     }
     value = null;
 }