Пример #1
0
        public void DeleteNode(int index)
        {
            SLinkedListNode <T> node;
            SLinkedListNode <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(SLinkedListNode <T> newNode)
 {
     if (_lastNode == null)
     {
         _firstNode = _lastNode = newNode;
     }
     else
     {
         _lastNode.next = newNode;
         newNode.next   = null;
     }
 }
Пример #3
0
 public void PrependNode(SLinkedListNode <T> newNode)
 {
     if (Head == null)
     {
         _firstNode = _lastNode = newNode;
     }
     else
     {
         newNode.next = _firstNode;
         _firstNode   = newNode;
     }
 }
Пример #4
0
        public void InsertNewNode(SLinkedListNode <T> newNode, int index)
        {
            int currentIndex = 0;
            SLinkedListNode <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 != null)
                {
                    if (currentIndex == index)
                    {
                        var p = currentNode.next;
                        currentNode.next = newNode;
                        newNode.next     = p;
                    }
                    currentIndex++;
                    currentNode = currentNode.next;
                }
            }
        }
Пример #5
0
 public SLinkedListNode(T value, SLinkedListNode <T> nextNode)
 {
     data = value;
     next = nextNode;
 }