public SkipList(List <T> inputList) { int maximumLevel = (int)Math.Log(inputList.Count, 2); Head = new SkipListNode <T> { Key = default(T), Level = maximumLevel }; var a = Head.Next; for (int i = 0; i < inputList.Count; i++) { a = new SkipListNode <T> { Key = inputList[i] }; for (int j = 1; j <= maximumLevel; j++) { if (i % Math.Pow(2, j) != 0) { a.Level = j - 1; } } a = a.Next; } a = new SkipListNode <T> { Key = default(T), Level = maximumLevel, Next = null }; var levelHead = Head; var levelIterator = Head; a = levelHead.Lower; for (int i = 2; i <= maximumLevel; i++) { a = new SkipListNode <T> { Key = default(T), Level = maximumLevel, Upper = a }; while (levelIterator != null) { if (levelIterator.Level >= i) { levelIterator.Lower = new SkipListNode <T> { Key = levelIterator.Key, Level = levelIterator.Level, Upper = levelIterator }; a.Next = levelIterator.Lower; a = a.Next; } levelIterator = levelIterator.Next; } a.Next = new SkipListNode <T> { Key = default(T), Level = maximumLevel, Next = null, Upper = levelIterator }; levelHead = levelHead.Lower; a = levelHead.Lower; levelIterator = levelHead; } }
public void Add(T value) { var a = Find(value); var newNode = new SkipListNode <T> { Key = value, Next = a.Next }; a.Next = newNode; var randKey = new Random(); int coin = randKey.Next(2); while (coin != 0) { a = a.Lower; newNode = new SkipListNode <T> { Key = value, Next = a.Next }; a.Next = newNode; coin = randKey.Next(2); } }