基於字典的跳表實現
#include #include #include #include #include using namespace std;
template struct skipnode
};template class dictionary;
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual pair* find(const k&) const = 0;
virtual void erase(const k&) = 0;
virtual void insert(const pair&) = 0;
};template class skiplist : public dictionary
bool empty() const
pair* find(const k&) const;
void erase(const k&);
void insert(const pair&);
void output(ostream&) const;
protected:
float m_cutoff; //確定層數
int level() const; //通過隨機數產生鏈表層數
int m_maxlevel; //允許的最大鏈表層數
int m_levels; //當前最大非空鏈表層數
k m_tailkey; //最大關鍵字
int m_size; //插入元素個數
skipnode* m_ptheadnode; //頭節點指標
skipnode* m_pttailnode; //尾節點指標
skipnode** last; //last[i]表示i層的最後節點
skipnode* search(const k&) const;
};template skiplist::skiplist(k largekey, int maxpairs, float prob)
}template skiplist::~skiplist()
delete m_pttailnode;
delete last;
}template pair* skiplist::find(const k & thekey) const
skipnode*ptbeforenode = m_ptheadnode;
for (int i = m_levels; i >= 0; i--)
}if (ptbeforenode->next[0]->element.first == thekey)
return null;
}template int skiplist::level() const
return levels > m_maxlevel ? m_maxlevel : levels;
}template skipnode* skiplist::search(const k& thekey) const
last[i] = pbeforenode;
}return pbeforenode->next[0];
}template void skiplist::insert(const pair&thepair)
skipnode*pnode = search(thepair.first);
if (pnode->element.first == thepair.first)
int thelevel = level();
if (thelevel > m_levels)
skipnode*pinsertnode = new skipnode(thepair, thelevel + 1);
for (int i = 0; i <= thelevel; i++)
m_size++;
return;
}template void skiplist::erase(const k &thekey)
skipnode* pdelnode = search(thekey);
if (pdelnode->element.first != thekey)
for (int i = 0; i <= m_levels && last[i]->next[i] == pdelnode; i++)
while (m_levels > 0 && m_ptheadnode->next[m_levels] == m_pttailnode)
delete pdelnode;
m_size--;
return;
}template void skiplist::output(ostream &out) const
}template ostream& operator << (ostream &out, const skiplist& node)
int main()
; my_memcpy(dst , a, 5);
cout << dst << endl;
int shu[5] = ;
shuffle(shu, 5);
skiplistz(20000000, 10000000, 0.2);
pairp;
p.first = 2;
p.second = 2;
z.insert(p);
p.first = 1;
p.second = 2;
z.insert(p);
p.first = 3;
p.second = 2;
z.insert(p);
cout << z.size() << endl;
cout << z << endl;
z.erase(2);
cout << z.size() << endl;
cout << z << endl;
p.first = 5;
p.second = 88;
z.insert(p);
cout << z << endl;
cout << z.find(5)->second << endl;
time_t start, end;
double ret;
for (int i = 0; i < 15000000; i++)
start = clock();
for(int i = 0; i < 2000000; i++)
end = clock();
ret = double(end - start) / clocks_per_sec;
cout << ret << endl;
start = clock();
cout << z.find(7211234)->second << endl;
end = clock();
ret = double(end - start) / clocks_per_sec;
cout << ret << endl;
return 0;
}
手搓跳表 SkipList
public class skiplist 查詢資料 param data return public node search int data return res 通過data來查詢節點 param data return private node findnode int data if cu...
資料結構 跳表(Skip List)
從頂層鍊錶的首元素開始,從左往右搜尋,知道找到乙個大於或等於目標的元素,或者到達當前層鍊錶的尾部 如果該元素等於目標元素,則表明該元素已被找到 如果該元素大於目標元素或已到達鍊錶的尾部,則退回到當前層的前乙個元素,然後轉入下一層進行搜尋 新節點的層數 int newlevel randomlevel...
SkipList跳表記錄一下
節點擁有鍵和值,並按排序單獨鏈結順序,可能帶有一些中間的標記節點。清單是以可作為head.node訪問的虛擬節點為首。值字段僅宣告為object,因為它需要特殊的非v標記和標頭節點的值。static final class node 索引節點代表跳過列表的級別。注意即使節點和索引都具有前向指向字段,...