資料結構 雜湊表及其模擬實現

2021-10-03 21:52:04 字數 3289 閱讀 5773

直接定址法:hashfunc(date)=a*date+b;【a,b為常數】

從雜湊衝突的位置,找下乙個空餘位置插入元素。尋找方式有兩種,線性探測和二次探測。

直接向後+1查詢。

h(i)=h0+i^2; 或者h(i)=h0- i^2;

de需要的原因,此位置不可以插入元素,也不可設定為em 。查詢時遇em就停止了,可能出現錯誤。

1.不可以按照vector方式擴容。**長度增加,相應的元素位置也會改變。

2.先新建雜湊表,將元素乙個乙個重新插入到新**。

將雜湊衝突的元素按鍊錶的形式掛在結點位置。每個位置叫做乙個雜湊桶。

1.不按照vector方式擴容,也不按照閉雜湊方式擴容【新建結點,空間開銷大】

2.將**結點直接插入新**即可。

當**中每個位置都有元素時,擴容!

#pragma once

#include#include#include#include"common.h"

enum state;

templatestruct elem

t _date;

state _state;

};//約定:雜湊**中的元素必須唯一

template,bool isline=true>

class hashtable

bool insert(const t& date)

}else

} //插入元素

_table[hashaddr]._date = date;

_table[hashaddr]._state = exist;

++_size;

++_total;

return true;

} int find(const t& date)

if (isline)

}else

} return -1;

} bool erase(const t& date)

_table[pos]._state = delete;

_size--;

return true;

} void swap(hashtable& ht)

size_t size()const

private:

void checkcapacity()

}swap(newht);

} }size_t hashfunc(const t& date)

std::vector> _table;

size_t _size;

size_t _total;

};void test2()

#pragma once

#include"common.h"

#include#includetemplatestruct hashnode

hashnode* _pnext;

t _date;

};template>

class hashbucket

bool insertunique(const t& date)

//3.插入新節點

//採用頭插法

pcur = new node(date);

pcur->_pnext = _table[bucketnum];

_table[bucketnum] = pcur;

_size++;

return true;

} bool insertequal(const t& date){}

bool eraseunique(const t& date)

else

delete pcur;

_size--;

return true;

}ppre = pcur;

pcur = pcur->_pnext;

} return false;

} bool eraseequal(const t& date) {}

node* find(const t& date)const

return nullptr;

} size_t size()

bool empty()const

void print()

std::cout << "\n";

} }private:

size_t hashfunc(const t& date) const

//如果雜湊桶儲存元素個數和桶的個數相同

//原因:最佳狀態:每個桶中儲存乙個元素

//每個桶中都有乙個元素,就會雜湊衝突。

//桶的利用率最大

void checkcapacity()

} if (flag==1)

}newht._size = _size;

_table.swap(newht._table);

} }private:

std::vector_table;

size_t _size;

};void testbucket() ;

for (auto e : array)

ht.insertunique(e);

ht.print();

ht.insertunique(44);

ht.insertunique(54);

ht.print();

ht.eraseunique(44);

ht.print();

}

#pragma once

#includesize_t getnextprime(size_t capacity);

templateclass dfdef

};class dfstr

private:

size_t bkdrhash(const char*str)

return hash;

}};

#include"common.h"

const int primecount = 28;

const size_t primelist[primecount] = ;

size_t getnextprime(size_t capacity)

return primelist[primecount];

}

#include#include"haxi.h"

#include"hasubucket.hpp"

int main()

js模擬實現雜湊表

在演算法中,尤其是有關陣列的演算法中,雜湊表的使用可以很好的解決問題,所以這篇文章會記錄一些有關js實現雜湊表並給出解決實際問題的例子。說明 這篇部落格所寫並不是真正意義的雜湊表,只是與雜湊表的使用有相似之處。屬性的列舉 var person for var prop in person 輸出 即對...

模擬實現「棧」資料結構

棧 是一種資料結構 特殊的線性表。原則是後進先出 lifo 只允許在固定的一一端進行插入 刪除,稱為 棧頂 而另一端稱為 棧底 棧 可以用陣列 鍊錶來模擬實現,但是陣列方式更優,以下我們就使用陣列來模擬實現。stack.h pragma once include commen.h 棧 陣列實現 ty...

模擬實現「佇列」資料結構

佇列也是一種特殊的資料結構,原則是先進先出 fifo 只允許在一端插入,稱為 隊尾 在另一端刪除,稱為 隊頭 佇列可以用陣列 鍊錶實現,但是用鍊錶實現更優。以下就是使用鍊錶實現的。queue.h pragma once include commen.h 佇列 單鏈表實現 typedef int qd...