Fix SkipList remove operation (#3160)
This commit is contained in:
parent
e59568bc5e
commit
d14a5d1eed
@ -117,7 +117,9 @@ public class SkipList<E extends Comparable<E>> {
|
||||
}
|
||||
for (int i = 0; i <= layer; i++) {
|
||||
current.previous(i).setNext(i, current.next(i));
|
||||
current.next(i).setPrevious(i, current.previous(i));
|
||||
if (current.next(i) != null) {
|
||||
current.next(i).setPrevious(i, current.previous(i));
|
||||
}
|
||||
}
|
||||
size--;
|
||||
}
|
||||
|
@ -42,12 +42,26 @@ class SkipListTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
void removeFromHead() {
|
||||
SkipList<String> skipList = createSkipList();
|
||||
String mostLeftElement = skipList.get(0);
|
||||
int initialSize = skipList.size();
|
||||
print(skipList);
|
||||
|
||||
skipList.remove("a");
|
||||
skipList.remove(mostLeftElement);
|
||||
|
||||
print(skipList);
|
||||
assertEquals(initialSize - 1, skipList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeFromTail() {
|
||||
SkipList<String> skipList = createSkipList();
|
||||
String mostRightValue = skipList.get(skipList.size() - 1);
|
||||
int initialSize = skipList.size();
|
||||
print(skipList);
|
||||
|
||||
skipList.remove(mostRightValue);
|
||||
|
||||
print(skipList);
|
||||
assertEquals(initialSize - 1, skipList.size());
|
||||
|
Loading…
Reference in New Issue
Block a user