java数据结构 HashTable链地址法

这篇博客探讨了Java中使用链地址法实现的HashTable数据结构。通过将哈希表的每个单元设为链表,处理下标冲突时,数据会被插入到对应单元的链表中。内容涉及数据结构的多层次设计,包括Info、Node、LinkList和HashTable,并详细讲解了Node类以及如何将数组转换为LinkList类型。
摘要由CSDN通过智能技术生成

链地址法:哈希表每个单元设置为链表,当关键字通过hashCode映射到哈希表单元中,将数据本身插入到该单元的链表中

数据结构层次:Info->Node->LinkList->HashTable

1、Node类

/*
 * 链结点
 */
public class Node {
	//数据域
	public Info info;   //员工信息
	//指针域
	public Node next;
	//默认构造方法
	public Node(Info info){
		this.info=info;
	}
}
2、LinkList类

/*
 * 链表
 */
public class LinkList {
	// 头结点
	private Node first;

	public LinkList() {
		first = null;
	}
	/*
	 * 插入一个结点,在头结点后进行插入
	 */
	public void insertFirst(Info info) {
		Node node = new Node(info);
		if (first == null)
			first = node;
		else {
			node.next = first;
			first= node;
		}
	}

	/*
	 * 删除一个结点,在头结点后进行删除
	 */
	public Node deleteFirst() {
		Node tmp = first;
		first = tmp.next;
		return tmp;
	}

	/*
	 * 查找,根据关键字
	 */
	public Node find(String key){
		Node cur=first;
		while(!key.equals(cur.info.getKey())){   //比较关键字
			if(cur.next==null)
				return null;
			cur=cur.next;
		}
		return cur;
	}
	/*
	 * 删除,根据关键字
	 */
	public Node delete(String key){
		Node cur=first;
		Node pre=first;
		while(!key.equals(cur.info.getKey())){    //比较关键字
			if(cur.next==null)
				return null;
			pre=cur;
			cur=cur.next;
		}
		if(cur==first){   //删第一个结点
			first=first.next;
		}
		else{   //不是第一个结点
			pre.next=cur.next;		
		}
		return cur;
	}
}
3、HashTable类

数组改成LinkList型

public class HashTable {

	private LinkList[] arr;
	/*
	 * 默认构造方法
	 */
	public HashTable(){
		arr=new LinkList[100];
	}
	/*
	 * 数组初始化
	 */
	public HashTable(int maxSize){
		arr=new LinkList[maxSize];
	}
	/*
	 * 插入数据
	 */
	public void insert(Info info){
		//获得关键字
		String key=info.getKey();
		//关键字对应的哈希数
		int hashVal=hashCode(key);
		//创建链表
		if(arr[hashVal]==null){
			arr[hashVal]=new LinkList();
		}
		//在链表中插入
		arr[hashVal].insertFirst(info);
	}
	/*
	 * 查找数据
	 */
	public Info find(String key){
		int hashVal=hashCode(key);
		return arr[hashVal].find(key).info;
	}
	/*
	 * 删除数据
	 */
	public Info delete(String key){
		int hashVal=hashCode(key);
		return arr[hashVal].delete(key).info;
	}
	/*
	 * 字母的ASCII码求和/幂连乘
	 */
	public int hashCode(String key){
		BigInteger hashValue=new BigInteger("0");
		BigInteger pow27=new BigInteger("1");
		for(int i=key.length()-1;i>=0;i--){
			int letter=key.charAt(i)-96;
			BigInteger letterB=new BigInteger(String.valueOf(letter));
			hashValue=hashValue.add(letterB.multiply(pow27));
			pow27=pow27.multiply(new BigInteger(String.valueOf(27)));
		}
		//压缩可选值,但会出现冲突--->开放地址法、链地址法
		return hashValue.mod(new BigInteger(String.valueOf(arr.length))).intValue();
	}
}


#include #include typedef struct node { int data; struct node *next; }node; init_hash(node **A,int n) { int i; for(i=0;idata=0; A[i]->next=NULL; } } insert_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; if(A[key]->next!=NULL) { p=A[key]->next; while(p->next!=NULL) p=p->next; q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; p->next=q; } else { q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; A[key]->next=q; } } int search_hash(node **A,int value,int n) { int key; node *p; key=value%n; if(A[key]->next==NULL) return 0; else { p=A[key]->next; while(p!=NULL) { if(p->data==value) return 1; } return 0; } } delete_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; p=A[key]; q=A[key]->next; while(q->data!=value) { p=q; q=q->next; } p->next=q->next; free(q); } print_hash(node **A,int n) { int i; node *p; for(i=0;inext!=NULL) { p=A[i]->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } } printf("\n"); } main() { int i,n,value,Case; node **A; printf("输入待排序元素个数:\n"); scanf("%d",&n); A=(node **)malloc(sizeof(node*)*n); //申请一个指针型数组A[n] init_hash(A,n);//初始化数组A printf("输入hash表的值(空格键分开):\n"); for(i=0;i<n;i++) //建hash表 { scanf("%d",&value); insert_hash(A,value,n); } printf("请选择hash表的操作\n1.查询\t2.插入\t3.删除\t4.输出\n"); scanf("%d",&Case); switch(Case) { case 1: { printf("请输入要查询的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"Success!\n":"Failure!\n"); //查询 } case 2: { printf("请输入要插入的元素:"); scanf("%d",&value); insert_hash(A,value,n); //插入 } case 3: { printf("请输入要删除的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"删除成功!\n":"表中不存在该元素!\n"); delete_hash(A,value,n); } case 4: //输出 { printf("表中元素为:\n"); print_hash(A,n); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值