Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
来源:互联网 发布:淘宝正品保障图标 编辑:程序博客网 时间:2024/05/16 23:50
//
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structures C++ Weiss: P.535 Deterministic Skip Lists,
// the typename "Comparable" is int
#include<iostream>
using namespace std;
#include<math.h>
class DSL
{
public:
DSL()
{
bottom=newSkipNode(INT_MAX);
bottom->right=bottom->down=bottom;
tail=newSkipNode(INT_MAX);
tail->right=tail;
header=newSkipNode(INT_MAX,tail,bottom);
}
constDSL &operator=(constDSL &rhs)
{
if(this!=&rhs)
{
makeEmpty();
deletebottom;deleteheader;deletetail;
bottom=newSkipNode(INT_MAX);
bottom->right=bottom->down=bottom;
tail=newSkipNode(INT_MAX);
tail->right=tail;
header=clone(rhs.header);
}
return *this;
}
DSL(constDSL &rhs)
{
if(this!=&rhs)
{
bottom=newSkipNode(INT_MAX);
bottom->right=bottom->down=bottom;
tail=newSkipNode(INT_MAX);
tail->right=tail;
header=clone(rhs.header);
}
}
~DSL()
{
makeEmpty();
deleteheader;
deletetail;
deletebottom;
}
// delete from top to bottom
void makeEmpty()
{
SkipNode *current=header;
while( current->down!=bottom)
{
current=current->down;
SkipNode *p=current->right;
while( p!=tail)
{
SkipNode *temp=p;
p=p->right;
delete temp;
}
p=current;
current=current->down;
delete p;
}
}
bool contains(constint &x)const
{
SkipNode *current=header;
bottom->element=x;
for(;;)
{
if( x<current->element)
current=current->down;
elseif( current->element<x)
current=current->right;
else
return current!=bottom;
}
}
void insert(constint &x)
{
SkipNode *current=header;
bottom->element=x;
while( current!=bottom)
{
while( current->element<x)
current=current->right;
//If gap size is 3 or at bottom level and
//must insert, then promote middle element
if( current->down->right->right->element < current->element)
{
current->right=newSkipNode(current->element, current->right, current->down->right->right);
current->element=current->down->right->element;
}
else
current=current->down;
}
//Raise height of DSL if necessary
if(header->right!=tail)
header=newSkipNode(INFINITY,tail,header);
}
bool isEmpty()const
{
returnheader->right==tail &&header->down==bottom;
}
constint & findMin()const
{
if(isEmpty())
cout<<"Empty DSL"<<endl;
else
{
SkipNode *current=header;
while( current->down!=bottom)
current=current->down;
return current->element;
}
}
constint & findMax()const
{
if(isEmpty())
cout<<"Empty DSL"<<endl;
else
{
SkipNode *current=header;
while( current->down!=bottom)
{
while( current->right->right!=tail)
current=current->right;
current=current->down;
}
while( current->right->right!=tail)
current=current->right;
return current->element;
}
}
// Print the list according to increasing order
void Print()const
{
SkipNode *current=header;
while( current->down!=bottom)
current=current->down;
while( current->right->right!=tail)
{
cout<<current->element<<" ";
current=current->right;
}
cout<<current->element<<endl;
}
void TotalPrint()const
{
SkipNode *current=header->down;
while( current!=bottom)
{
SkipNode *p=current;
while( p->right->right!=tail)
{
cout<<p->element<<" ";
p=p->right;
}
cout<<p->element<<endl;
current=current->down;
}
}
constint Header()const
{
returnheader->element;
}
private:
struct SkipNode
{
int element;
SkipNode *right;
SkipNode *down;
SkipNode(constint & theElement,SkipNode *rt=NULL,SkipNode *dt=NULL)
:element(theElement),right(rt),down(dt){}
};
SkipNode *header; // the list
SkipNode *tail;
SkipNode *bottom;
SkipNode *clone(SkipNode *t)const
{
if( t->right==t && t->down==t)
returnbottom;
elseif( t->right==t)
returntail;
elsereturnnewSkipNode(t->element,clone(t->right),clone(t->down));
}
};
int main()
{
DSL L1;
for(int i=0; i<10; ++i)
L1.insert(rand()%100);
DSL L=L1;
cout<<"Print"<<endl;
L.Print();
cout<<"TotalPrint"<<endl;
L.TotalPrint();
cout<<"Header"<<endl;
cout<<L.Header()<<endl;
int x;
while(cin>>x)
{
cout<<L.contains(x)<<endl;
}
cout<<L.findMax()<<" "<<L.findMin()<<endl;
DSL L2;
L2.findMin();
return 0;
}
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表,Comparable)
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 12: Splay Tree 伸展树
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 3: List C++
- Data Structures (Weiss) Chapter 5: Separate Chain
- Data Structures (Weiss) Chapter 6: Leftist Heap
- Data Structures (Weiss) Chapter 6: Binomial Queue
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- ASM using ASMLib and Raw Devices (for oracle 10g)【不错的图文讲解】
- Javascript - Select操作大集合
- hdu 4349 Xiao Ming's Hope
- 表单提交后,提交按钮不可用,防止用户多次提交
- 渠道商用假量冒充真实用户:开发者求给条活路
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- c# MD5+时间戳 加密
- oracle中imp命令详解
- O(n)回文子串算法
- YII memcache的使用
- Ogre 工具集
- Generate PDF programmatically in iPhone/iPad SDK
- 关于Spring3报org.aopalliance.intercept.MethodInterceptor错的问题解决方法
- html5视频<video>