一个最简单的跳跃表例子

原文在[url=http://www.dreamincode.net/forums/blog/114/entry-2727-introduction-to-a-skiplist-data-structure-in-java/]这里[/url], 是一个简化的跳跃表实现, 非常浅显易懂.
public class SkipListExample {
private static Node Head = null;
private static Node Tail = null;
private static Node Current = null;

// Records previous quarter or half mark
private static Node quarter = null;
private static Node half = null;

public static void main(String[] args) {
int ListCount = 100;

// Setup a standard double linked list manually
for (int i = 1; i <= ListCount; i++) {
Node newNode = new Node();
newNode.data = i;


// Assign to head node if this is the first node
if (Head == null) {
Head = newNode;
Tail = newNode;
Current = newNode;

quarter = newNode;
half = newNode;
}
else {
Current.next = newNode;
newNode.prev = Current;

// Add a quarter pointer if this node is a multiple of 25
if ((i % 25) == 0) {
newNode.prevQuarter = quarter;
quarter.nextQuarter = newNode;

quarter = newNode;
}

// Add a half pointer if this node is a multiple of 50
if ((i % 50) == 0) {
newNode.prevHalf = half;
half.nextHalf = newNode;

half = newNode;
}

// Set current node to be the next in line, set the tail
// and then loop back around for next node.
Current = newNode;
Tail = newNode;
}
}


// Run some tests
System.out.println("Find number 7... ");
FindNumber(7);

System.out.println("Find Number 33... ");
FindNumber(33);

System.out.println("Find number 67... ");
FindNumber(67);

System.out.println("Find number 101... ");
FindNumber(101);
}


// Searches our skip lists to locate our number
public static void FindNumber(int num) {
Node currentNode = Head;
boolean Found = false;

while (currentNode != null) {
// Simply show what nodes we have checked in our search
System.out.println("Checked node with data: " + currentNode.data);

// End search if value is greater than the value we are looking for.
if (currentNode.data > num) { break; }


// Check our various pointers to see if a jump would get us closer.
if (currentNode.data != num) {
if ((currentNode.nextHalf != null) && (currentNode.nextHalf.data <= num)) { currentNode = currentNode.nextHalf; }
else if ((currentNode.nextQuarter != null) && (currentNode.nextQuarter.data <= num)) { currentNode = currentNode.nextQuarter; }
else { currentNode = currentNode.next; }
}
else {
Found = true;
break;
}
}

// Report our findings
if (Found) { System.out.println("Number Found!"); }
else { System.out.println("Number wasn't found!"); }
}
}


// Our Node object with prev/next pointers and jump pointers
class Node {
public int data;
public Node next = null;
public Node prev = null;

// Specialized skip list pointers
public Node nextHalf = null;
public Node prevHalf = null;

public Node nextQuarter = null;
public Node prevQuarter = null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值