电子商务网站建设与管理期末考试,什么是网站集约化建设,云羽网络做网站怎么样,短网址链接生成目录 实验须知
代码实现
实验报告
一#xff1a;问题分析
二、数据结构
1.逻辑结构
2.物理结构
三、算法
#xff08;一#xff09;主要算法描述
1.用除留余数法构造哈希函数
2.线性探测再散列法
#xff08;一#xff09;主要算法实现代码
四、上机调试 实…目录 实验须知
代码实现
实验报告
一问题分析
二、数据结构
1.逻辑结构
2.物理结构
三、算法
一主要算法描述
1.用除留余数法构造哈希函数
2.线性探测再散列法
一主要算法实现代码
四、上机调试 实验须知 实验目的 深入理解哈希表解决冲突的办法。 实验内容 针对自己所在班级中学生姓名设计一个哈希表使得平均查找长度不超过 R 并完成相应的建表和查表程序。 实验要求 1 创建名为 kcsj19.cpp 的文件在其中编写哈希查找学生姓名的程序 2 假设学生姓名为中国人姓名的汉语拼音形式。待填入哈希表的学生姓名共有 30 个取平均查找 长度的上限为 2 。哈希函数用除留余数法构造用线性探测再散列法或链地址法处理冲突 3 测试数据取读者周围较熟悉的 30 个学生姓名。 代码实现
import java.util.Arrays;
import java.util.Scanner;public class Kcsj19 {private static final int TABLE_SIZE 61; // 哈希表的大小选择质数可以减少冲突private String[] table;public Kcsj19() {table new String[TABLE_SIZE];Arrays.fill(table, );}// 哈希函数除留余数法private int hashFunction(String key) {int hashValue 0;for (char ch : key.toCharArray()) {hashValue (hashValue * 31 ch) % TABLE_SIZE;}return hashValue;}// 线性探测再散列法private int linearProbe(int index, int attempt) {return (index attempt) % TABLE_SIZE;}// 插入元素private void insert(String name) {int index hashFunction(name);int attempt 0;while (!table[index].isEmpty()) {attempt;index linearProbe(index, attempt);}table[index] name;}// 查找元素private boolean search(String name) {int index hashFunction(name);int attempt 0;while (!table[index].isEmpty()) {if (table[index].equals(name)) {System.out.println(学生姓名 name 在哈希表中。);return true;}attempt;index linearProbe(index, attempt);}System.out.println(学生姓名 name 不在哈希表中。);return false;}// 打印哈希表private void displayTable() {for (int i 0; i TABLE_SIZE; i) {if (!table[i].isEmpty()) {System.out.println(哈希表[ i ]: table[i]);}}}public static void main(String[] args) {Kcsj19 hashTable new Kcsj19();// 待填入哈希表的学生姓名String[] students {蔡开俊, 陈恒, 陈慧强, 董彪, 冯迪, 冯伟宇, 谷婉如,韩欣言, 何继翔,李宝才, 李琳琳, 李现珍, 李向阳, 李毅琛, 路亚博,路政睿 ,孙志坤,王龙杰,王蕊, 王艺涵, 薛获宇, 元亚捷, 岳婧婧, 詹思航, 张浩,张恒, 张佳熙, 张锦彩, 张梦娇, 张轩硕};// 插入学生姓名到哈希表for (String student : students) {hashTable.insert(student);}// 打印哈希表hashTable.displayTable();// 测试查找学生姓名Scanner scanner new Scanner(System.in);// 判断学生姓名是否存在于哈希表中while(true) {System.out.print(请输入学生姓名输入exit退出);String inputName scanner.nextLine();if (inputName.equalsIgnoreCase(exit)) {System.out.println(程序已退出。);break;}hashTable.search(inputName);}}
}实验报告
一问题分析 描述问题该实验可归结为哈希表的建表和查表功能的实现。用除留余数法构建哈希表把学生姓名以汉语拼音的形式储存在哈希表中当发生冲突时会使用线性探测再散列法在哈希表中的下一个位置继续搜索直到找到空槽。解决该问题的方法和思路使用除留余数法构造哈希表然后向表中添加学生姓名如果在插入和查找的过程中发生冲突用线性探测散列法解决冲突。
二、数据结构
1.逻辑结构
哈希表的应用实习项目的逻辑结构是一个大小为TABLE_SIZE的数组每个数组元素都可以存储一个学生姓名。哈希函数将学生姓名转化成一个数组索引然后在该索引位置上插入或查找学生姓名。当插入学生姓名时如果对应的数组索引位置已经被占用则使用线性探测再散列法来寻找下一个可用的位置。当查找学生姓名时根据哈希函数得到学生姓名对应的数组索引然后根据线性探测再散列法依次检查该索引位置及其后续位置直到找到匹配的学生姓名或者遇到空位置为止。
2.物理结构
物理结构采用的是顺序存储结构。
三、算法
一主要算法描述
1.用除留余数法构造哈希函数
1哈希函数的输入是一个字符串key它使用了一个for循环遍历 字符串key的每个字符并结合当前的哈希值hashValue进行计算。具体的计算公式是hashValue(hashValue * 31ch)%TABLE_SIZE。 2其中ch表示当前字符的ASCII值。乘以31是为了增加哈希函数的散列性能使得不同的字符组合能够得到不同的哈希值。最后取余数TABLE_SIZE 是将哈希值限定在哈希表的大小范围内以确保插入和查询的正确性。 3然后根据这个哈希值确定存储学生姓名的位置索引在哈希表中的哪个位置。最终将学生姓名保存在哈希表的相应位置上。
2.线性探测再散列法
1代码中使用了线性探测再散列法来处理哈希冲突。在插入元素和查找元素的方法中通过不断尝试下一个位置来解决冲突。如果当前位置已经被占用就尝试下一个位置直到找到一个空闲的位置或者遍历完整个哈希表。 2linearProbe方法接受两个参数index表示原始的哈希值对应的索引attempt表示当前的探测次数。通过将原始索引index和探测次数attempt相加并对TABLE_SIZE取模得到一个新的索引值。这个新的索引值即为通过线性探测法计算得到的下一个位置。 3在插入元素的方法中先把通过学生姓名计算出的哈希值赋值给index此时设置探测次数attempt为0如果哈希表在index位置不为空那么attempt加一再用linearProbe方法来计算下一个位置即(indexattempt) % TABLE_SIZE。如果哈希表在index位置为空的话就把学生姓名添加到哈希表中。 4在查找元素的方法中首先通过调用hashFunction 哈希函数来计算给定学生姓名的哈希值并将其赋值给index变量。然后设置初始的探测次数attempt 为0。接下来使用一个循环来判断哈希表在index位置是否为空。如果不为空则进入循环体。在循环体中首先判断哈希表在index位置的值与目标姓名是否相等。如果相等说明找到了目标姓名将相关信息打印出来并返回true表示找到了目标。如果不相等则增加attempt的值并使用linearProbe方法计算下一个探测位置。linearProbe方法使用线性探测再散列法的思想通过(index attempt)%TABLE_SIZE的计算来获取下一个探测位置。当哈希表在index位置为空时说明未找到目标姓名将相关信息打印出来并返回false表示未找到目标。
一主要算法实现代码
1.用除留余数法构建哈希表
private int hashFunction(String key) {int hashValue 0;for (char ch : key.toCharArray()) {hashValue (hashValue * 31 ch) % TABLE_SIZE;}return hashValue;}
2.线性探测再散列法解决冲突
private int linearProbe(int index, int attempt) {return (index attempt) % TABLE_SIZE;
}
3.向哈希表中插入元素
private void insert(String name) {int index hashFunction(name);int attempt 0;while (!table[index].isEmpty()) {attempt;index linearProbe(index, attempt);}table[index] name;
}
4.在哈希表中查找元素
private boolean search(String name) {int index hashFunction(name);int attempt 0;while (!table[index].isEmpty()) {if (table[index].equals(name)) {System.out.println(学生姓名 name 在哈希表中。);return true;}attempt;index linearProbe(index, attempt);}System.out.println(学生姓名 name 不在哈希表中。);return false;}
四、上机调试
问题1 输入exit并不会退出程序而是输出“学生姓名exit不在哈希表中”。 图2 无法退出程序
解决办法出现这种情况的原因是使用“”来判断两个字符串是否相等要比较两个字符串的内容是否相等可以使用equals方法或者equalsIgnoreCase方法可以忽略大小写。 图3 使用equalsIgnoreCase方法比较字符串是否相同
问题2哈希表的存储与哈希值有关 如何获取字符串的哈希值 进行存储。 解决办法对于输入的字符串key遍历其中的每个字符ch将hashValue乘以31后加上ch的ASCII码值并取模TABLE_SIZE得到新的哈希值。
问题3 出现空指针异常。 图4出现空指针异常
解决办法出现异常的原因是没有对数组进行初始化。使用Arrays.fill()方法将数组中所有元素赋值为空字符串避免在插入或查找元素时出现值null 因为如果数组元素为null那么在调用字符串方法时就会抛出空指针异常。 图5对数组进行初始化 文章转载自: http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.mbmtz.cn.gov.cn.mbmtz.cn http://www.morning.rwjfs.cn.gov.cn.rwjfs.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.ymjrg.cn.gov.cn.ymjrg.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.ryqsq.cn.gov.cn.ryqsq.cn http://www.morning.hsjfs.cn.gov.cn.hsjfs.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.nhzps.cn.gov.cn.nhzps.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.fhjnh.cn.gov.cn.fhjnh.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.jxscp.cn.gov.cn.jxscp.cn http://www.morning.qjsxf.cn.gov.cn.qjsxf.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.zwznz.cn.gov.cn.zwznz.cn http://www.morning.thbnt.cn.gov.cn.thbnt.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.grryh.cn.gov.cn.grryh.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn