帮我用java写一段代码,要求如下:1、实现手机通讯录的新建、插入、删除与查询(要求用排序顺序表、排序单链表两种存储结构都要完成)。
时间: 2023-11-19 13:04:20 浏览: 96
需要别人帮忙用JAVA编手机通讯录,需要的代码都在里面
好的,以下是使用排序顺序表和排序单链表实现手机通讯录的Java代码:
排序顺序表实现:
```java
import java.util.Scanner;
public class ContactList {
private static class Contact {
String name;
String phoneNumber;
}
private static class SeqList {
Contact[] data;
int length;
int maxSize;
SeqList(int maxSize) {
this.maxSize = maxSize;
data = new Contact[maxSize];
length = 0;
}
int find(String name) {
for (int i = 0; i < length; i++) {
if (data[i].name.equals(name)) {
return i;
}
}
return -1;
}
void insert(Contact contact) {
int i;
for (i = 0; i < length; i++) {
if (data[i].name.compareTo(contact.name) > 0) {
break;
}
}
for (int j = length; j > i; j--) {
data[j] = data[j - 1];
}
data[i] = contact;
length++;
}
void delete(String name) {
int i = find(name);
if (i != -1) {
for (int j = i; j < length - 1; j++) {
data[j] = data[j + 1];
}
length--;
}
}
void display() {
System.out.println("姓名\t\t电话号码");
for (int i = 0; i < length; i++) {
System.out.println(data[i].name + "\t\t" + data[i].phoneNumber);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SeqList seqList = new SeqList(100);
while (true) {
System.out.println("请选择操作:1.新建联系人 2.插入联系人 3.删除联系人 4.查询联系人 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1: {
Contact contact = new Contact();
System.out.println("请输入姓名:");
contact.name = scanner.next();
System.out.println("请输入电话号码:");
contact.phoneNumber = scanner.next();
seqList.insert(contact);
seqList.display();
break;
}
case 2: {
Contact contact = new Contact();
System.out.println("请输入姓名:");
contact.name = scanner.next();
System.out.println("请输入电话号码:");
contact.phoneNumber = scanner.next();
seqList.insert(contact);
seqList.display();
break;
}
case 3: {
System.out.println("请输入要删除的联系人姓名:");
String name = scanner.next();
seqList.delete(name);
seqList.display();
break;
}
case 4: {
System.out.println("请输入要查询的联系人姓名:");
String name = scanner.next();
int i = seqList.find(name);
if (i != -1) {
System.out.println("姓名:" + seqList.data[i].name);
System.out.println("电话号码:" + seqList.data[i].phoneNumber);
} else {
System.out.println("未找到该联系人!");
}
break;
}
case 5:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
排序单链表实现:
```java
import java.util.Scanner;
public class ContactList {
private static class Contact {
String name;
String phoneNumber;
Contact next;
}
private static class LinkList {
Contact head;
void insert(Contact contact) {
if (head == null || head.name.compareTo(contact.name) > 0) {
contact.next = head;
head = contact;
} else {
Contact p = head;
while (p.next != null && p.next.name.compareTo(contact.name) < 0) {
p = p.next;
}
contact.next = p.next;
p.next = contact;
}
}
void delete(String name) {
if (head != null) {
if (head.name.equals(name)) {
head = head.next;
} else {
Contact p = head;
while (p.next != null && !p.next.name.equals(name)) {
p = p.next;
}
if (p.next != null) {
p.next = p.next.next;
}
}
}
}
Contact find(String name) {
Contact p = head;
while (p != null && !p.name.equals(name)) {
p = p.next;
}
return p;
}
void display() {
System.out.println("姓名\t\t电话号码");
Contact p = head;
while (p != null) {
System.out.println(p.name + "\t\t" + p.phoneNumber);
p = p.next;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
LinkList linkList = new LinkList();
while (true) {
System.out.println("请选择操作:1.新建联系人 2.插入联系人 3.删除联系人 4.查询联系人 5.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1: {
Contact contact = new Contact();
System.out.println("请输入姓名:");
contact.name = scanner.next();
System.out.println("请输入电话号码:");
contact.phoneNumber = scanner.next();
linkList.insert(contact);
linkList.display();
break;
}
case 2: {
Contact contact = new Contact();
System.out.println("请输入姓名:");
contact.name = scanner.next();
System.out.println("请输入电话号码:");
contact.phoneNumber = scanner.next();
linkList.insert(contact);
linkList.display();
break;
}
case 3: {
System.out.println("请输入要删除的联系人姓名:");
String name = scanner.next();
linkList.delete(name);
linkList.display();
break;
}
case 4: {
System.out.println("请输入要查询的联系人姓名:");
String name = scanner.next();
Contact contact = linkList.find(name);
if (contact != null) {
System.out.println("姓名:" + contact.name);
System.out.println("电话号码:" + contact.phoneNumber);
} else {
System.out.println("未找到该联系人!");
}
break;
}
case 5:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
阅读全文