@Test public void testAdd() { TestNode node1 = new TestNode(3, "node1"); TestNode node2 = new TestNode(3, "node2"); TestNode node3 = new TestNode(3, "node3"); TestNode node4 = new TestNode(3, "node4"); TestNode node5 = new TestNode(3, "node5"); TestNode node6 = new TestNode(3, "node6"); TestNode node7 = new TestNode(5, "node7"); TestNode node8 = new TestNode(4, "node8"); MultiSkipList<TestNode> multiSkipList = new MultiSkipList<>(Comparator.comparingInt(TestNode::getPriority)); multiSkipList.add(node1); multiSkipList.add(node2); multiSkipList.add(node3); multiSkipList.add(node4); multiSkipList.add(node5); multiSkipList.add(node6); multiSkipList.add(node7); multiSkipList.add(node8); multiSkipList.print(); multiSkipList.remove(node4); multiSkipList.print(); multiSkipList.add(node4, 1); multiSkipList.print(); multiSkipList.add(new TestNode(3, "node9")); multiSkipList.add(new TestNode(4, "node10")); multiSkipList.print(); }
public boolean add(E element, int position) { if (size < position) { return add(element); } else { SkipListNode<E> newNode = new SkipListNode<>(element); SkipListNode curNode = bottomHead.right; while (curNode.right != null && position > 0) { curNode = curNode.right; position--; } if (curNode.left != null) { SkipListNode leftNode = curNode.left; horizontalLink(leftNode, newNode); horizontalLink(newNode, curNode); size++; } else { return false; } } return true; }
MultiSkipList { public boolean add(E element, int position) { if (size < position) { return add(element); } else { SkipListNode<E> newNode = new SkipListNode<>(element); SkipListNode curNode = bottomHead.right; while (curNode.right != null && position > 0) { curNode = curNode.right; position--; } if (curNode.left != null) { SkipListNode leftNode = curNode.left; horizontalLink(leftNode, newNode); horizontalLink(newNode, curNode); size++; } else { return false; } } return true; } }
MultiSkipList { public boolean add(E element, int position) { if (size < position) { return add(element); } else { SkipListNode<E> newNode = new SkipListNode<>(element); SkipListNode curNode = bottomHead.right; while (curNode.right != null && position > 0) { curNode = curNode.right; position--; } if (curNode.left != null) { SkipListNode leftNode = curNode.left; horizontalLink(leftNode, newNode); horizontalLink(newNode, curNode); size++; } else { return false; } } return true; } MultiSkipList(Comparator<E> comparator); }
MultiSkipList { public boolean add(E element, int position) { if (size < position) { return add(element); } else { SkipListNode<E> newNode = new SkipListNode<>(element); SkipListNode curNode = bottomHead.right; while (curNode.right != null && position > 0) { curNode = curNode.right; position--; } if (curNode.left != null) { SkipListNode leftNode = curNode.left; horizontalLink(leftNode, newNode); horizontalLink(newNode, curNode); size++; } else { return false; } } return true; } MultiSkipList(Comparator<E> comparator); boolean add(E element, int position); boolean add(E element); boolean addAll(Collection<? extends E> c); Iterator<E> iterator(); int size(); boolean remove(E element); List<E> toList(); void clear(); void print(); }
MultiSkipList { public boolean add(E element, int position) { if (size < position) { return add(element); } else { SkipListNode<E> newNode = new SkipListNode<>(element); SkipListNode curNode = bottomHead.right; while (curNode.right != null && position > 0) { curNode = curNode.right; position--; } if (curNode.left != null) { SkipListNode leftNode = curNode.left; horizontalLink(leftNode, newNode); horizontalLink(newNode, curNode); size++; } else { return false; } } return true; } MultiSkipList(Comparator<E> comparator); boolean add(E element, int position); boolean add(E element); boolean addAll(Collection<? extends E> c); Iterator<E> iterator(); int size(); boolean remove(E element); List<E> toList(); void clear(); void print();  }