Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Prepends an item onto the list
        /// </summary>
        /// <param name="item">Item to prepend</param>
        /// <returns>Item that was successfully prepended.</returns>
        public T AddFirst(T item)
        {
            lock (_lock)
            {
                ParentAwareNode <T> newNode = new ParentAwareNode <T>(item);

                if (_head == null)
                {
                    _head = _last = newNode;
                }
                else
                {
                    newNode.SetChild(_head);
                    _head = newNode;
                }
                _length++;
            }

            return(item);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds an item to the end of a list
        /// </summary>
        /// <param name="item">Item to add</param>
        /// <returns>The item that was successfully added</returns>
        public T Add(T item)
        {
            lock (_lock)
            {
                ParentAwareNode <T> newNode = new ParentAwareNode <T>(item);

                if (_head == null)
                {
                    _head = _last = newNode;
                }
                else
                {
                    _last.SetChild(newNode);
                    newNode.SetParent(_last);
                    _last = newNode;
                }
                _length++;
            }

            return(item);
        }
Exemplo n.º 9
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--;
        }
Exemplo n.º 10
0
 /// <summary>
 /// setting head and length
 /// </summary>
 public LinkedList(ParentAwareNode <T> head, int length)
 {
     _head   = head;
     _length = length;
 }
Exemplo n.º 11
0
 /// <summary>
 /// setting head and length
 /// </summary>
 public DoublyLinkedList(ParentAwareNode <T> head, ParentAwareNode <T> last, int length)
 {
     _head   = head;
     _last   = last;
     _length = length;
 }