Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表,Comparable)
2013-08-01 18:58
393 查看
//
// 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
// do not use the variable INFINITY;
// CORRECT
#include<iostream>
using namespace
std;
#include<math.h>
template<typename Comparable>
class DSL
{
public:
DSL(
const Comparable & inf)
{
bottom=new
SkipNode(inf);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(inf);
tail->right=tail;
header=new
SkipNode(inf, tail,
bottom);
}
const DSL &
operator=( const
DSL &rhs)
{
if( this!=&rhs)
{
makeEmpty();
delete bottom;
delete header;
delete tail;
Comparable temp=rhs.header->element;
// header->element==INFINITY
bottom= new
SkipNode(temp);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(temp);
tail->right=tail;
header=clone(rhs.header);
}
return *this;
}
DSL(
const DSL &rhs)
{
if( this!=&rhs)
{
Comparable temp=rhs.header->element;
// header->element==INFINITY
bottom=new
SkipNode(temp);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(temp);
tail->right=tail;
header=clone(rhs.header);
}
}
~DSL()
{
makeEmpty();
delete header;
delete tail;
delete bottom;
}
// 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( const Comparable &x)
const
{
SkipNode *current=header;
bottom->element=x;
for(;;)
{
if( x<current->element)
current=current->down;
else if( current->element<x)
current=current->right;
else
return current!=bottom;
}
}
void insert( const Comparable &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=new
SkipNode(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=new
SkipNode(
tail->element,
tail, header);
// tail->element==INFINITY
}
bool isEmpty() const
{
return
header->right==tail &&
header->down==bottom;
}
const Comparable & findMin()
const
{
if( isEmpty())
cout<<"Empty DSL"<<endl;
else
{
SkipNode *current=header;
while( current->down!=bottom)
current=current->down;
return current->element;
}
}
const Comparable & 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;
}
// print all levels
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;
}
}
private:
struct SkipNode
{
Comparable element;
SkipNode *right;
SkipNode *down;
SkipNode(
const Comparable & 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)
{
if( t->right==t && t->down==t)
// t==bottom
return bottom;
else if( t->right==t)
// t==tail
return tail;
else return
new SkipNode(t->element,
clone(t->right),
clone(t->down));
}
};
int main()
{
DSL<int> L1(3000000);
DSL<int> L=L1;
for( int i=0; i<30; ++i)
L.insert(rand()%113);
L.Print();
L.TotalPrint();
cout<<L.findMin()<<" "<<L.findMax()<<endl;
return
0;
}
// 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
// do not use the variable INFINITY;
// CORRECT
#include<iostream>
using namespace
std;
#include<math.h>
template<typename Comparable>
class DSL
{
public:
DSL(
const Comparable & inf)
{
bottom=new
SkipNode(inf);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(inf);
tail->right=tail;
header=new
SkipNode(inf, tail,
bottom);
}
const DSL &
operator=( const
DSL &rhs)
{
if( this!=&rhs)
{
makeEmpty();
delete bottom;
delete header;
delete tail;
Comparable temp=rhs.header->element;
// header->element==INFINITY
bottom= new
SkipNode(temp);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(temp);
tail->right=tail;
header=clone(rhs.header);
}
return *this;
}
DSL(
const DSL &rhs)
{
if( this!=&rhs)
{
Comparable temp=rhs.header->element;
// header->element==INFINITY
bottom=new
SkipNode(temp);
bottom->right=bottom->down=bottom;
tail=new
SkipNode(temp);
tail->right=tail;
header=clone(rhs.header);
}
}
~DSL()
{
makeEmpty();
delete header;
delete tail;
delete bottom;
}
// 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( const Comparable &x)
const
{
SkipNode *current=header;
bottom->element=x;
for(;;)
{
if( x<current->element)
current=current->down;
else if( current->element<x)
current=current->right;
else
return current!=bottom;
}
}
void insert( const Comparable &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=new
SkipNode(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=new
SkipNode(
tail->element,
tail, header);
// tail->element==INFINITY
}
bool isEmpty() const
{
return
header->right==tail &&
header->down==bottom;
}
const Comparable & findMin()
const
{
if( isEmpty())
cout<<"Empty DSL"<<endl;
else
{
SkipNode *current=header;
while( current->down!=bottom)
current=current->down;
return current->element;
}
}
const Comparable & 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;
}
// print all levels
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;
}
}
private:
struct SkipNode
{
Comparable element;
SkipNode *right;
SkipNode *down;
SkipNode(
const Comparable & 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)
{
if( t->right==t && t->down==t)
// t==bottom
return bottom;
else if( t->right==t)
// t==tail
return tail;
else return
new SkipNode(t->element,
clone(t->right),
clone(t->down));
}
};
int main()
{
DSL<int> L1(3000000);
DSL<int> L=L1;
for( int i=0; i<30; ++i)
L.insert(rand()%113);
L.Print();
L.TotalPrint();
cout<<L.findMin()<<" "<<L.findMax()<<endl;
return
0;
}
相关文章推荐
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- Data Structures (Weiss) Chapter 12: Splay Tree 伸展树
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 5: Separate Chain
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- Chapter 2 | Linked Lists--移除未排序链表中的重复项
- Skip List跳跃链表
- Chapter 2 | Linked Lists--实现两个单链表数据的和
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Chapter 2 | Linked Lists--查找循环链表中的环的开始结点
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- java.util.concurrent.ConcurrentSkipListSet 基于跳跃链表的并发set