Beispiel #1
0
 public void Remove(MyDoublyLinkedListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException();
     }
     if (!ContainsNode(node))
     {
         throw new InvalidOperationException();
     }
     if (node == Head)
     {
         RemoveFirst();
         return;
     }
     if (node == Tail)
     {
         RemoveLast();
         return;
     }
     node.Prev.Next = node.Next;
     node.Next.Prev = node.Prev;
     node.Next      = null;
     node.Prev      = null;
     Count--;
 }
Beispiel #2
0
        public MyDoublyLinkedList(MyDoublyLinkedListNode <T> head)
        {
            Head = head;

            Count++;

            var h = Head;

            while (h.Next != null)
            {
                Count++;
                h = h.Next;
            }
            Tail = h;
        }
Beispiel #3
0
        public MyDoublyLinkedListNode <T> FindLast(T value)
        {
            var h = Head;
            MyDoublyLinkedListNode <T> result = null;

            while (h != null)
            {
                if (h.Data.Equals(value))
                {
                    result = h;
                }
                h = h.Next;
            }

            return(result);
        }
Beispiel #4
0
 public void AddLast(MyDoublyLinkedListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException();
     }
     if (Count == 0)
     {
         AddFirst(node);
     }
     else
     {
         node.Prev = Tail;
         Tail.Next = node;
         Tail      = node;
         Count++;
     }
 }
Beispiel #5
0
 public void AddFirst(MyDoublyLinkedListNode <T> node)
 {
     if (node == null)
     {
         throw new ArgumentNullException();
     }
     if (Head == null)
     {
         Head = Tail = node;
     }
     else
     {
         node.Next = Head;
         Head.Prev = node;
         Head      = node;
     }
     Count++;
 }
Beispiel #6
0
        private bool ContainsNode(MyDoublyLinkedListNode <T> node)
        {
            bool isFound = false;
            var  h       = Head;
            var  t       = Tail;

            while (h != t && h != null && t != null)
            {
                if (h.Equals(node) || t.Equals(node))
                {
                    isFound = true;
                    break;
                }
                h = h.Next;
                t = t.Prev;
            }
            if (h == t && h == node && h != null)
            {
                isFound = true;
            }
            return(isFound);
        }
Beispiel #7
0
 public void AddBefore(MyDoublyLinkedListNode <T> node, MyDoublyLinkedListNode <T> newNode)
 {
     if (node == null || newNode == null)
     {
         throw new ArgumentNullException();
     }
     if (!ContainsNode(node))
     {
         throw new InvalidOperationException();
     }
     if (node == Head)
     {
         AddFirst(newNode);
     }
     else
     {
         node.Prev.Next = newNode;
         newNode.Prev   = node.Prev;
         newNode.Next   = node;
         node.Prev      = newNode;
         Count++;
     }
 }
Beispiel #8
0
 public void AddAfter(MyDoublyLinkedListNode <T> node, MyDoublyLinkedListNode <T> newNode)
 {
     if (node == null || newNode == null)
     {
         throw new ArgumentNullException();
     }
     if (!ContainsNode(node))
     {
         throw new InvalidOperationException();
     }
     if (node == Tail)
     {
         AddLast(newNode);
     }
     else
     {
         node.Next.Prev = newNode;
         newNode.Next   = node.Next;
         node.Next      = newNode;
         newNode.Prev   = node;
         Count++;
     }
 }
Beispiel #9
0
 public MyDoublyLinkedList(T data)
 {
     Head  = new MyDoublyLinkedListNode <T>(data);
     Tail  = Head;
     Count = 1;
 }