通信录查询系统(查找应用) 设计内容: 设计散列表实现通讯录查找系统。 (1) 设每个记录有下列数据项:电话号码、用户名、地址; (2) 从键盘输入各记录,分别以电话号码为关键字建立散列表; (3) 采用二次探测再散列法解决冲突; (4) 查找并显示给定电话号码的记录; (5) 通讯录信息文件保存; (6) 要求人机界面友好,使用图形化界面;
时间: 2023-12-09 10:06:40 浏览: 114
设计散列表实现电话号码查找系统
好的,我可以为你提供一个Java实现的通讯录查询系统的代码示例。以下是代码示例:
Contact.java
```java
public class Contact {
private String phoneNumber;
private String name;
private String address;
public Contact(String phoneNumber, String name, String address) {
this.phoneNumber = phoneNumber;
this.name = name;
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Phone Number: " + phoneNumber + ", Name: " + name + ", Address: " + address;
}
}
```
HashTable.java
```java
public class HashTable {
private int size;
private Contact[] contacts;
public HashTable(int size) {
this.size = size;
this.contacts = new Contact[size];
}
private int hash(String phoneNumber) {
return Math.abs(phoneNumber.hashCode() % size);
}
public void add(Contact contact) {
int hash = hash(contact.getPhoneNumber());
int i = 0;
while (i < size && contacts[hash] != null && !contacts[hash].getPhoneNumber().equals(contact.getPhoneNumber())) {
i++;
hash = (hash + i * i) % size;
}
if (i == size) {
System.out.println("Hash table is full!");
return;
}
contacts[hash] = contact;
}
public Contact find(String phoneNumber) {
int hash = hash(phoneNumber);
int i = 0;
while (i < size && contacts[hash] != null && !contacts[hash].getPhoneNumber().equals(phoneNumber)) {
i++;
hash = (hash + i * i) % size;
}
if (i == size || contacts[hash] == null) {
return null;
}
return contacts[hash];
}
public void delete(String phoneNumber) {
int hash = hash(phoneNumber);
int i = 0;
while (i < size && contacts[hash] != null && !contacts[hash].getPhoneNumber().equals(phoneNumber)) {
i++;
hash = (hash + i * i) % size;
}
if (i == size || contacts[hash] == null) {
System.out.println("Contact not found!");
return;
}
contacts[hash] = null;
}
public void display() {
for (Contact contact : contacts) {
if (contact != null) {
System.out.println(contact);
}
}
}
}
```
ContactBook.java
```java
import java.util.Scanner;
public class ContactBook {
private HashTable hashTable;
private Scanner scanner;
public ContactBook(int size) {
this.hashTable = new HashTable(size);
this.scanner = new Scanner(System.in);
}
public void run() {
while (true) {
System.out.println("1. Add contact");
System.out.println("2. Find contact");
System.out.println("3. Delete contact");
System.out.println("4. Display all contacts");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addContact();
break;
case 2:
findContact();
break;
case 3:
deleteContact();
break;
case 4:
displayContacts();
break;
case 5:
return;
default:
System.out.println("Invalid choice!");
break;
}
}
}
private void addContact() {
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter address: ");
String address = scanner.nextLine();
Contact contact = new Contact(phoneNumber, name, address);
hashTable.add(contact);
System.out.println("Contact added successfully!");
}
private void findContact() {
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
Contact contact = hashTable.find(phoneNumber);
if (contact == null) {
System.out.println("Contact not found!");
} else {
System.out.println(contact);
}
}
private void deleteContact() {
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
hashTable.delete(phoneNumber);
}
private void displayContacts() {
hashTable.display();
}
}
```
Main.java
```java
public class Main {
public static void main(String[] args) {
ContactBook contactBook = new ContactBook(10);
contactBook.run();
}
}
```
这个示例使用了散列表来实现通讯录的查找功能,以电话号码作为关键字建立散列表,并采用二次探测再散列法解决冲突。同时实现了添加联系人、查找联系人、删除联系人和显示所有联系人等功能。
阅读全文