예제 #1
0
        public void DeleteNode(int index)
        {
            DLinkedListNode <T> node;
            DLinkedListNode <T> currentNode;

            if (Head == null)
            {
                return;
            }
            else if (index < 0 || index > Count)
            {
                throw new Exception("Index out of bounds");
            }
            else if (index == 0)
            {
                var p = Head;
                currentNode = Head.Next;
                _firstNode  = currentNode;
                p           = null;
                return;
            }
            else
            {
                int i = 1;
                currentNode = Head.Next;
                while (i <= index)
                {
                    var p = currentNode.Next;
                    currentNode = currentNode.Next.Next;
                    p.Data      = default(T);
                    p.Next      = null;
                }
                return;
            }
        }
예제 #2
0
 public void AppendNode(DLinkedListNode <T> newNode)
 {
     if (Head == null)
     {
         _firstNode = _lastNode = newNode;
     }
     else
     {
         newNode.Next        = _firstNode;
         newNode.Previous    = _firstNode.Previous;
         _firstNode.Previous = newNode;
     }
 }
예제 #3
0
        public void InsertNewNode(DLinkedListNode <T> newNode, int index)
        {
            int currentIndex = 0;
            DLinkedListNode <T> currentNode;

            if (newNode == null)
            {
                return;
            }

            if (index == 0)
            {
                PrependNode(newNode);
            }
            else if (index == Count)
            {
                AppendNode(newNode);
            }
            else if (Head == null)
            {
                Head = newNode = LastNode;
            }
            else
            {
                currentNode = Head;
                while (currentNode.Next != Head)
                {
                    if (currentIndex == index)
                    {
                        var p = currentNode.Next;
                        currentNode.Next = newNode;
                        newNode.Previous = currentNode;
                        newNode.Next     = p;
                        p.Previous       = newNode;
                    }
                    currentIndex++;
                    currentNode = currentNode.Next;
                }
            }
        }
예제 #4
0
 public DLinkedListNode(T value, DLinkedListNode <T> nextNode, DLinkedListNode <T> prevNode)
 {
     Data     = value;
     Next     = nextNode;
     Previous = prevNode;
 }
예제 #5
0
 public DLinkedListNode(T value)
 {
     Data     = value;
     Next     = null;
     Previous = null;
 }