All of the skip list implementations I have found so far use keys and associate them with values. But what I need is a skip list, where I can insert a value at an index position i, so that all the values following this index i can be queried with an index incremented by one.
here a little example for clarification:
//pseudocode
//let skipList sk be a list of ints, containing 5 elements.
//insert 6 at index 3
sk.insert(3, 6);
//insert 5 at index 3
sk.insert(3, 5);
//get index 4
int value = sk.get(4);
now value
should be 6, because 5 was inserted at index 3 so the value 6 moved one index up.
it is possible to build a data structure like this using a skip list, see Indexable skiplist here: http://en.wikipedia.org/wiki/Skip_list
but i could not find an implementation. it would be very useful to have such a datastructure eg in situations where you have many random inserts and accesses on big lists.
There appears to be a link to an implementation in the wikipedia page you posted: http://code.activestate.com/recipes/576930/
It is python though.
Alternatively, take an existing C++ implementation and add the link widths to it and implement the indexed lookup.
I implemented a skiplist class in C# that has a method which returns the k-th largest value in the collection. It does exactly what you need for your get
method.
However, the insertion method doesn't specify a position since it always keeps the list sorted therefore the position is determined by a lookup among existing values.
Take a look at https://github.com/htoma/algorithms/blob/master/Algorithms/Algorithms/Sources/SkipLists/SkipList.cs , method FindKLargestElement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.