数据结构Java代码实现以下内容:通信录查询系统(查找应用) 设计内容: 设计散列表实现通讯录查找系统。(1) 设每个记录有下列数据项:电话号码、用户名、地址; (2) 从键盘输入各记录,分别以电话号码为关键字建立散列表; (3) 采用二次探测再散列法解决冲突; (4) 查找并显示给定电话号码的记录; (5) 通讯录信息文件保存; (6) 要求人机界面友好,使用图形化界面; 设计要求: 主函数:根据选单的选项调用各函数,并完成相应的功能。Menu()的功能:显示英文提示选单。Quit()的功能:退出选单。Create()的功能:创建新的通讯录。Append()的功能:在通讯录的末尾写入新的信息,并返回选单。Find():查询某人的信息,如果找到了,则显示该人的信息,如果没有则提示通讯录中没有此人的信息,并返回选单。Alter()的功能:修改某人的信息,如果未找到要修改的人,则提示通讯录中没有此人的信息,并返回选单。Delete()的功能:删除某人的信息,如果未找到要删除的人,则提示通讯录中没有此人的信息,并返回选单。List()的功能:显示通讯录中的所有记录。save()的功能:保存通讯录中的所有记录到指定文件中。Load()的功能:从指定文件中读取通讯录中的记录。
时间: 2024-03-02 17:52:06 浏览: 115
数据结构课程设计基于C-C++,包括通讯录,哈夫曼编码,交通咨询系统以及图书管理系统(含实验报告+源代码+文档说明)
以下是通信录查询系统的Java代码实现:
```java
import java.io.*;
import java.util.*;
class Record {
public String phoneNum;
public String name;
public String address;
public Record(String phoneNum, String name, String address) {
this.phoneNum = phoneNum;
this.name = name;
this.address = address;
}
}
class HashTable {
private int size;
private Record[] table;
private int count;
public HashTable(int size) {
this.size = size;
table = new Record[size];
count = 0;
}
private int hash(String phoneNum) {
int hashVal = 0;
for (int i = 0; i < phoneNum.length(); i++) {
hashVal = (hashVal * 27 + phoneNum.charAt(i) - '0') % size;
}
return hashVal;
}
private int rehash(int hashVal, int i) {
return (hashVal + i * i) % size;
}
public void insert(Record record) {
if (count == size) {
System.out.println("Error: Hash table is full");
return;
}
String phoneNum = record.phoneNum;
int hashVal = hash(phoneNum);
int i = 0;
while (table[hashVal] != null && !table[hashVal].phoneNum.equals(phoneNum)) {
i++;
hashVal = rehash(hashVal, i);
}
if (table[hashVal] == null) {
count++;
}
table[hashVal] = record;
}
public Record find(String phoneNum) {
int hashVal = hash(phoneNum);
int i = 0;
while (table[hashVal] != null && !table[hashVal].phoneNum.equals(phoneNum)) {
i++;
hashVal = rehash(hashVal, i);
}
if (table[hashVal] == null) {
return null;
} else {
return table[hashVal];
}
}
public void delete(String phoneNum) {
int hashVal = hash(phoneNum);
int i = 0;
while (table[hashVal] != null && !table[hashVal].phoneNum.equals(phoneNum)) {
i++;
hashVal = rehash(hashVal, i);
}
if (table[hashVal] != null) {
table[hashVal] = null;
count--;
}
}
public void display() {
for (int i = 0; i < size; i++) {
if (table[i] != null) {
System.out.println(table[i].phoneNum + " " + table[i].name + " " + table[i].address);
}
}
}
public void saveToFile(String fileName) {
try {
FileWriter fw = new FileWriter(fileName);
for (int i = 0; i < size; i++) {
if (table[i] != null) {
fw.write(table[i].phoneNum + "," + table[i].name + "," + table[i].address + "\n");
}
}
fw.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
public void loadFromFile(String fileName) {
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
insert(new Record(parts[0], parts[1], parts[2]));
}
}
br.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
public class Main {
private static Scanner scanner = new Scanner(System.in);
private static HashTable hashTable = new HashTable(100);
public static void main(String[] args) {
while (true) {
System.out.println("Menu:");
System.out.println("1. Create new record");
System.out.println("2. Find record");
System.out.println("3. Alter record");
System.out.println("4. Delete record");
System.out.println("5. List all records");
System.out.println("6. Save records to file");
System.out.println("7. Load records from file");
System.out.println("8. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
createRecord();
break;
case 2:
findRecord();
break;
case 3:
alterRecord();
break;
case 4:
deleteRecord();
break;
case 5:
listRecords();
break;
case 6:
saveToFile();
break;
case 7:
loadFromFile();
break;
case 8:
quit();
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
private static void createRecord() {
System.out.print("Enter phone number: ");
String phoneNum = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter address: ");
String address = scanner.nextLine();
hashTable.insert(new Record(phoneNum, name, address));
}
private static void findRecord() {
System.out.print("Enter phone number to find: ");
String phoneNum = scanner.nextLine();
Record record = hashTable.find(phoneNum);
if (record == null) {
System.out.println("Record not found");
} else {
System.out.println(record.phoneNum + " " + record.name + " " + record.address);
}
}
private static void alterRecord() {
System.out.print("Enter phone number to alter: ");
String phoneNum = scanner.nextLine();
Record record = hashTable.find(phoneNum);
if (record == null) {
System.out.println("Record not found");
} else {
System.out.print("Enter new name: ");
String name = scanner.nextLine();
System.out.print("Enter new address: ");
String address = scanner.nextLine();
hashTable.insert(new Record(phoneNum, name, address));
}
}
private static void deleteRecord() {
System.out.print("Enter phone number to delete: ");
String phoneNum = scanner.nextLine();
hashTable.delete(phoneNum);
}
private static void listRecords() {
hashTable.display();
}
private static void saveToFile() {
System.out.print("Enter file name: ");
String fileName = scanner.nextLine();
hashTable.saveToFile(fileName);
}
private static void loadFromFile() {
System.out.print("Enter file name: ");
String fileName = scanner.nextLine();
hashTable.loadFromFile(fileName);
}
private static void quit() {
System.exit(0);
}
}
```
说明:
- `Record` 类表示记录,包含电话号码、用户名、地址三个属性。
- `HashTable` 类表示散列表,使用二次探测再散列法解决冲突。实现了插入、查找、删除、显示、保存到文件和从文件加载等操作。
- 主函数中使用一个 `Scanner` 对象读取用户输入,根据选单的选项调用各函数完成相应的功能。
阅读全文