Пример #1
0
        /// <summary>
        /// Remove item from list
        /// </summary>
        /// <param name="item">item to remove</param>
        /// <returns>Item, if it was successfully removed</returns>
        public T Remove(T item)
        {
            lock (_lock)
            {
                int i = 0;
                ParentAwareNode <T> previousNode = null;
                ParentAwareNode <T> currentNode  = _head;
                while (i < _length && !item.Equals(currentNode.Value))
                {
                    previousNode = currentNode;
                    currentNode  = currentNode.Next() as ParentAwareNode <T>;
                    i++;
                }
                if (i == _length)
                {
                    throw new IndexOutOfRangeException();
                }

                item = currentNode.Value;
                if (previousNode == null)
                {
                    _head = currentNode.Next() as ParentAwareNode <T>;
                }
                else
                {
                    previousNode.SetChild(currentNode.Next());
                }
                _length--;
            }
            return(item);
        }
Пример #2
0
        /// <summary>
        /// Removes item at specified index
        /// </summary>
        /// <param name="index">index of item to remove</param>
        /// <returns>item which was removed</returns>
        public T RemoveAt(int index)
        {
            if (index > _length - 1)
            {
                throw new IndexOutOfRangeException();
            }

            T item;

            lock (_lock)
            {
                int i = 0;
                ParentAwareNode <T> previousNode = null;
                ParentAwareNode <T> currentNode  = _head;
                while (i < index)
                {
                    previousNode = currentNode;
                    currentNode  = currentNode.Next() as ParentAwareNode <T>;
                    i++;
                }
                item = currentNode.Value;
                if (previousNode == null)
                {
                    _head = currentNode.Next() as ParentAwareNode <T>;
                }
                else
                {
                    previousNode.SetChild(currentNode.Next());
                }
                _length--;
            }
            return(item);
        }
Пример #3
0
        /// <summary>
        /// Get Item at specified index
        /// </summary>
        /// <param name="index">the index to return item</param>
        /// <returns>item at the specified index</returns>
        public T Get(int index)
        {
            if (index > _length - 1)
            {
                throw new IndexOutOfRangeException();
            }

            ParentAwareNode <T> node = _head;

            int i        = 0;
            int midpoint = GetMidPointIndex();

            if (index > midpoint)
            {
                node = _last;
                // find starting at end
                i = _length - 1;
                while (i > index)
                {
                    node = node.Previous() as ParentAwareNode <T>;
                    i--;
                }
            }
            else
            {
                while (i < index)
                {
                    node = node.Next() as ParentAwareNode <T>;
                    i++;
                }
            }

            return(node.Value);
        }
Пример #4
0
        /// <summary>
        /// Remove item from list
        /// </summary>
        /// <param name="item">item to remove</param>
        /// <returns>Item, if it was successfully removed</returns>
        public T Remove(T item)
        {
            lock (_lock)
            {
                ParentAwareNode <T> currentNode = _head;

                while (
                    currentNode != null &&                              // if currentNode is null, we are at the end
                    ((item == null && currentNode.Value != null) ||     //if item is null and current value is null we found it
                     (item != null && !item.Equals(currentNode.Value))) // if item is not null and item equals current value we found it
                    )
                {
                    currentNode = currentNode.Next() as ParentAwareNode <T>;
                }

                if (currentNode == null)
                {
                    throw new IndexOutOfRangeException();
                }

                item = currentNode.Value;
                DeleteNode(currentNode);
            }
            return(item);
        }
Пример #5
0
 /// <summary>
 /// Returns the tail of the list
 /// </summary>
 /// <returns>The items after the first item in the list</returns>
 public List <T> Tail()
 {
     if (_length < 2)
     {
         return(new LinkedList <T>());
     }
     return(new LinkedList <T>(_head.Next() as ParentAwareNode <T>, _length - 1));
 }
Пример #6
0
        private void DeleteNode(ParentAwareNode <T> node)
        {
            if (node.Previous() == null)
            {
                _head = node;
            }
            else
            {
                (node.Previous() as IChildableNode <T>).SetChild(node.Next());
            }

            if (node.Next() == null)
            {
                _last = node;
            }
            else
            {
                (node.Next() as ParentAwareNode <T>).SetParent(node.Previous());
            }
            _length--;
        }
Пример #7
0
        /// <summary>
        /// Get Item at specified index
        /// </summary>
        /// <param name="index">the index to return item</param>
        /// <returns>item at the specified index</returns>
        public T Get(int index)
        {
            if (index > _length - 1)
            {
                throw new IndexOutOfRangeException();
            }

            ParentAwareNode <T> node = _head;

            int i = 0;

            while (i < index)
            {
                node = node.Next() as ParentAwareNode <T>;
                i++;
            }
            return(node.Value);
        }
Пример #8
0
        /// <summary>
        /// Get index of the item
        /// </summary>
        /// <param name="item">Item to return index</param>
        /// <returns>index of the item, if it is in the list.
        /// If item is not in the list, returns -1
        /// </returns>
        public int IndexOf(T item)
        {
            if (_length == 0)
            {
                return(-1);
            }

            int index = 0;
            ParentAwareNode <T> currentNode = _head;

            while (currentNode != null)
            {
                if (item.Equals(currentNode.Value))
                {
                    return(index);
                }
                index++;
                currentNode = currentNode.Next() as ParentAwareNode <T>;
            }
            return(-1);
        }