コード例 #1
0
        public DoublyLinkedListNode <T> Find(T item)
        {
            DoublyLinkedListNode <T> current = Head;

            while (current != null)
            {
                // Head -> 3 -> 5 -> 7
                // Value: 5
                if (current.Value.Equals(item))
                {
                    return(current);
                }

                current = current.Next;
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Removes the first occurance of the item from the list (searching
        /// from Head to Tail).
        /// </summary>
        /// <param name="item">The item to remove</param>
        /// <returns>True if the item was found and removed, false otherwise</returns>
        public bool Remove(T item)
        {
            DoublyLinkedListNode <T> found = Find(item);

            if (found == null)
            {
                return(false);
            }

            DoublyLinkedListNode <T> previous = found.Previous;
            DoublyLinkedListNode <T> next     = found.Next;

            if (previous == null)
            {
                // we're removing the head node
                Head = next;
                if (Head != null)
                {
                    Head.Previous = null;
                }
            }
            else
            {
                previous.Next = next;
            }

            if (next == null)
            {
                // we're removing the tail
                Tail = previous;
                if (Tail != null)
                {
                    Tail.Next = null;
                }
            }
            else
            {
                next.Previous = previous;
            }

            Count--;

            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Add the node to the end of the list
        /// </summary>
        /// <param name="node">The node to add</param>
        public void AddTail(DoublyLinkedListNode <T> node)
        {
            if (Count == 0)
            {
                Head = node;
            }
            else
            {
                Tail.Next = node;

                // Before: Head -> 3 <-> 5 -> null
                // After:  Head -> 3 <-> 5 <-> 7 -> null
                // 7.Previous = 5
                node.Previous = Tail;
            }

            Tail = node;
            Count++;
        }
コード例 #4
0
        /// <summary>
        /// Removes the last node from the list
        /// </summary>
        public void RemoveTail()
        {
            if (Count != 0)
            {
                if (Count == 1)
                {
                    Head = null;
                    Tail = null;
                }
                else
                {
                    // Before: Head --> 3 --> 5 --> 7
                    //         Tail = 7
                    // After:  Head --> 3 --> 5 --> null
                    //         Tail = 5
                    // Null out 5's Next pointer
                    Tail.Previous.Next = null;
                    Tail = Tail.Previous;
                }

                Count--;
            }
        }
コード例 #5
0
        /// <summary>
        /// Removes the first node from the list.
        /// </summary>
        public void RemoveHead()
        {
            if (Count != 0)
            {
                // Before: Head -> 3 <-> 5
                // After:  Head -------> 5

                // Head -> 3 -> null
                // Head ------> null
                Head = Head.Next;

                Count--;

                if (Count == 0)
                {
                    Tail = null;
                }
                else
                {
                    // 5.Previous was 3, now null
                    Head.Previous = null;
                }
            }
        }