예제 #1
0
        public void AddTheEnd(T data)
        {
            if (_head == null)
            {
                _head = new SinglyNode <T>(data);
                goto end;
            }

            GetOrNull(_count - 1).NextData = new SinglyNode <T>(data);
end:
            _count += 1;
        }
예제 #2
0
        public void Remove(int index)
        {
            if (index == 0)
            {
                _head = _head.NextData;
            }
            else
            {
                SinglyNode <T> topSinglyNode = GetOrNull(index - 1);
                SinglyNode <T> endSinglyNode = GetOrNull(index + 1);
                topSinglyNode.NextData = endSinglyNode;
            }


end:
            _count -= 1;
        }
예제 #3
0
        public void Insert(T data, int index)
        {
            if (index == 0 && _head == null)
            {
                _head = new SinglyNode <T>(data);
                goto end;
            }

            var topNode = GetOrNull(index);

            if (topNode == null)
            {
                GetOrNull(index - 1).NextData = new SinglyNode <T>(data);
                goto end;
            }

            //head 把head赋值,head的next赋值原node
            if (index == 0)
            {
                var itemHead = _head;
                var node     = new SinglyNode <T>(data);
                node.NextData = itemHead;
                _head         = node;
            }
            //不是Head 把index-1的next赋值value node ,value node next赋值原 node
            else
            {
                var topNode_  = GetOrNull(index - 1);
                var itemNode_ = topNode_.NextData;
                topNode_.NextData          = new SinglyNode <T>(data);
                topNode_.NextData.NextData = itemNode_;
            }

end:
            _count += 1;
        }