鍊錶實現的雜湊表(Java版)

2021-07-29 23:29:11 字數 1334 閱讀 2059

public class separatechaininghashtable

@suppresswarnings("unchecked")

public separatechaininghashtable(int size)

/** make the hash table logically empty

*/public void makeempty()

public void insert(anytype x)}/*

* 移除指定元素

*/public void remove(anytype x)}/*

* rehashing for separate chaining hash table

*/@suppresswarnings("unchecked")

private void rehash(){

list oldlists=thelists;//原來雜湊表的長度

thelists=new list[nextprime(2*thelists.length)];//現在雜湊表的長度

for(int j=0;j();//對現在雜湊表的每個元素再new乙個鍊錶

currentsize=0;

for(int i=0;i測試:

public static void main(string args) {

// todo auto-generated method stub

separatechaininghashtables=new separatechaininghashtable<>();

s.insert("hello");

s.insert("the");

s.insert("world");

s.insert("!");

s.insert("12");

system.out.println("當前雜湊表是否含有world:"+s.contains("world"));

s.remove("world");

system.out.println("當前雜湊表是否含有world:"+s.contains("world"));

system.out.println("當前雜湊表是否含有hello:"+s.contains("hello"));

s.makeempty();

system.out.println("當前雜湊表是否含有hello:"+s.contains("hello"));

效果:當前雜湊表是否含有world:true

當前雜湊表是否含有world:false

當前雜湊表是否含有hello:true

當前雜湊表是否含有hello:false

雜湊表(雜湊表)的實現

雜湊函式直接用key size的形式,size為雜湊表的大小。衝突處理採用平方探測法,為保證可以探測到整個雜湊表空間,雜湊表大小設定為4k 3形式的素數。當雜湊表中的元素過多時會造成效能下降,這時應該倍增雜湊表的大小,重新計算原來雜湊表中每個元素在新的雜湊表中的位置。雜湊表的實現 hashtable...

鍊錶 java實現雙向鍊錶

前面已經總結了單向鍊錶,有興趣的兄弟可以進我部落格看一下。大家對比就可以看出,實現同樣的功能單向鍊錶要比雙向鍊錶痛苦的多。所以呀不斷地總結前輩留下的東西,是社會進步的基礎呀。可以直接看linkedlist的原始碼,其就是個雙向鍊錶。一 雙向鍊錶的結構。1 首先節點的結構,其中包含本節點內容,同時需要...

鍊錶 java實現

package com.shine.linearlist 單鏈表結點類 public class node public node package com.shine.linearlist 線性表介面 2015 01 19 qizhang public inte ce llist package c...