Ejemplo n.º 1
0
        public void Remove(T t)
        {
            SkipListNode[] needUpdates = new SkipListNode[this.levelCount];
            SkipListNode   temp        = this.head;

            // 查找边界点:
            // A[i] < t,A[i+1]>= t,即找到A[i]之后
            // 需要进一步判断就是否为要删除的值
            for (int i = this.levelCount - 1; i >= 0; i--)
            {
                while (temp != null && temp.GetForward(i) != null && temp.GetForward(i).item.CompareTo(t) < 0)
                {
                    temp = temp.levels[i].forward;
                }

                needUpdates[i] = temp;
            }

            // 源链表中的节点,如果该值与要删除的不相等,说明要删除的元素在跳表中不存在
            var node = temp.GetForward(0);

            if (node != null && node.item.CompareTo(t) == 0)
            {
                // A->B->C,更新为:A->C,其中B为要删除的结点
                for (int i = 0; i < this.levelCount; i++)
                {
                    SkipListNode forward = needUpdates[i].levels[i].forward;
                    if (forward.item.CompareTo(t) == 0)
                    {
                        needUpdates[i].levels[i].forward = forward.levels != null ? forward.levels[i].forward : null;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void CalculateLevelCount()
        {
            SkipListNode temp = this.head;

            while (temp != null && temp.GetForward(0) != null)
            {
                if (temp.levels.Length > this.levelCount)
                {
                    this.levelCount = temp.levels.Length;
                }

                temp = temp.GetForward(0);
            }
        }