public DoublyLinkedListNode DeleteANodeInDoublyLinkedList(int data)
        {
            DoublyLinkedListNode node = this.head;

            Console.WriteLine("---Printing DLL after removing the element---");
            while (node != null && node.GetDoublyLinkedListNodeData() != data)
            {
                node = node.GetDoublyLinkedListNextNode();
            }

            DoublyLinkedListNode nodeToBeDeleted = node;

            node.GetDoublyLinkedListPreviousNode()
            .SetDoublyLinkednodeNext(node.GetDoublyLinkedListNextNode());
            node.GetDoublyLinkedListNextNode()
            .SetDoublyLinkedListNodePrevious(node.GetDoublyLinkedListPreviousNode());

            return(this.head);
        }
Пример #2
0
        /// <summary>
        /// Add the node to the end of the list
        /// </summary>
        /// <param name="value">The node to add</param>
        public void AddLast(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++;
        }
Пример #3
0
        public bool Remove(T item)
        {
            DoublyLinkedListNode Previous = null, current = First, next = First.Next;

            while (current != null)
            {
                if (current.Value.Equals(item))
                {
                    if (Previous != null)
                    {
                        Previous.Next = next;
                        if (next == null)
                        {
                            Last = Previous;
                        }
                        else
                        {
                            next.Previous = Previous;
                        }
                    }
                    else
                    {
                        First = First.Next;
                        if (First == null)
                        {
                            Last = null;
                        }
                        else
                        {
                            First.Previous = null;
                        }
                    }
                    Count--;
                    return(true);
                }
                Previous = current;
                current  = next;
                next     = next?.Next;
            }

            return(false);
        }
Пример #4
0
        public void Add(DoublyLinkedListNode <T> newNode)
        {
            if (_head is null)
            {
                _head = newNode;
            }
            else
            {
                var current = _head;
                // urrent.Next이 for문을 돌 때마다
                // 1. 현재 값의 다음 값을 계속 불러온다.
                for ( ; current != null && current.Next != null;)
                {
                    current = current.Next;
                }

                // 추가할 때 양 방향 연결
                current.Next = newNode;
                newNode.Prev = current;
                newNode.Next = null;
            }
        }
Пример #5
0
        public DoublyLinkedList(params T[] arr)
        {
            var current = new DoublyLinkedListNode(default(T));

            foreach (var elem in arr)
            {
                DoublyLinkedListNode node = new DoublyLinkedListNode(elem);
                if (Count == 0)
                {
                    current = node;
                    First   = current;
                }
                else
                {
                    current.Next  = node;
                    node.Previous = current;
                    current       = node;
                }

                Last = current;
                Count++;
            }
        }
Пример #6
0
        /// <summary>
        /// Removes the last node from the list
        /// </summary>
        public void RemoveLast()
        {
            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--;
            }
        }
Пример #7
0
        /// <summary>
        /// Removes the first node from the list.
        /// </summary>
        public void RemoveFirst()
        {
            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;
                }
            }
        }
 /// <summary>
 /// Removes an item from the list.
 /// </summary>
 /// <param name="value">Item to remove. Generic type.</param>
 /// <returns>True if an item is removed. Otherwise false.</returns>
 /// <exception cref="InvalidOperationException">List is empty.</exception>
 public bool Remove(T value)
 {
     if (_count == 0)
     {
         throw new InvalidOperationException("List is empty!");
     }
     if (Head.Value.Equals(value))
     {
         RemoveFirst();
         return(true);
     }
     else if (Tail.Value.Equals(value))
     {
         RemoveLast();
         return(true);
     }
     else
     {
         DoublyLinkedListNode <T> current = Head;
         while (current != null)
         {
             if (current.Value.Equals(value))
             {
                 DoublyLinkedListNode <T> prev = current.Previous;
                 DoublyLinkedListNode <T> next = current.Next;
                 prev.Next     = next;
                 next.Previous = prev;
                 current       = null;
                 _count--;
                 return(true);
             }
             current = current.Next;
         }
     }
     return(false);
 }
Пример #9
0
        public bool RemoveAt(int index)
        {
            if (Head == null)
            {
                return(false);
            }

            else
            {
                DoublyLinkedListNode <T> tempnode = Head;
                DoublyLinkedListNode <T> prevnode = null;
                for (int i = 0; i < index; i++)
                {
                    prevnode = tempnode;
                    tempnode = tempnode.Next;
                }
                prevnode.Next = tempnode.Next;
                if (tempnode.Next == null)
                {
                    Tail = prevnode.Next;
                }
            }
            return(true);
        }
 public void SetDoublyLinkedListHead(DoublyLinkedListNode doublyLinkedListNode)
 {
     head = doublyLinkedListNode;
 }
 public DoublyLinkedList(DoublyLinkedListNode doublyLinkedListNode)
 {
     head = doublyLinkedListNode;
 }
 /// <summary>
 /// Clears the items in the list.
 /// </summary>
 public void Clear()
 {
     _count = 0;
     Head   = Tail = null;
 }
Пример #13
0
 public void SetDoublyLinkednodeNext(DoublyLinkedListNode doublyLinkedListNode)
 {
     this.next = doublyLinkedListNode;
 }
 public void Reset()
 {
     curNode = first;
 }
 public ListEnumerator(DoublyLinkedListNode <T> node)
 {
     first = node;
     Reset();
 }
Пример #16
0
 public DoublyLinkedListNode(T value, DoublyLinkedListNode <T> previous, DoublyLinkedListNode <T> next)
 {
     Value    = value;
     Previous = previous;
     Next     = next;
 }
Пример #17
0
 public DoublyLinkedList()
 {
     Head = null;
     Tail = null;
 }
Пример #18
0
 public DoublyLinkedListNode(T value)
 {
     Value    = value;
     Previous = null;
     Next     = null;
 }
Пример #19
0
 public void SetDoublyLinkedListNodePrevious(DoublyLinkedListNode doublyLinkedListNode)
 {
     this.previous = doublyLinkedListNode;
 }