java1、用单链表表方式实现在线通讯录的设计,可以对通讯录作插入、删除、显示、查找、修改等操作
时间: 2023-10-19 11:19:07 浏览: 182
以下是Java单链表实现在线通讯录的代码:
```
import java.util.Scanner;
class Contact {
String name;
String phone;
Contact next;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
this.next = null;
}
}
class ContactList {
Contact head;
public ContactList() {
this.head = null;
}
public void insert(Contact contact) {
if (head == null) {
head = contact;
} else {
Contact curr = head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = contact;
}
System.out.println("Successfully added " + contact.name + " to the contact list.");
}
public void display() {
if (head == null) {
System.out.println("No contacts found.");
} else {
Contact curr = head;
System.out.println("Contact List:");
while (curr != null) {
System.out.println("Name: " + curr.name + ", Phone: " + curr.phone);
curr = curr.next;
}
}
}
public void search(String name) {
if (head == null) {
System.out.println("No contacts found.");
} else {
Contact curr = head;
while (curr != null) {
if (curr.name.equals(name)) {
System.out.println("Name: " + curr.name + ", Phone: " + curr.phone);
return;
}
curr = curr.next;
}
System.out.println("Contact not found.");
}
}
public void delete(String name) {
if (head == null) {
System.out.println("No contacts found.");
} else if (head.name.equals(name)) {
head = head.next;
System.out.println("Contact " + name + " has been deleted.");
} else {
Contact prev = head;
Contact curr = head.next;
while (curr != null) {
if (curr.name.equals(name)) {
prev.next = curr.next;
System.out.println("Contact " + name + " has been deleted.");
return;
}
prev = curr;
curr = curr.next;
}
System.out.println("Contact not found.");
}
}
public void modify(String name, String phone) {
if (head == null) {
System.out.println("No contacts found.");
} else {
Contact curr = head;
while (curr != null) {
if (curr.name.equals(name)) {
curr.phone = phone;
System.out.println("Contact " + name + " has been modified.");
return;
}
curr = curr.next;
}
System.out.println("Contact not found.");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ContactList contactList = new ContactList();
while (true) {
System.out.println("Please select an operation:");
System.out.println("1. Add a contact");
System.out.println("2. Display contacts");
System.out.println("3. Search for a contact");
System.out.println("4. Delete a contact");
System.out.println("5. Modify a contact");
System.out.println("6. Exit");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.println("Please enter the name:");
String name = sc.nextLine();
System.out.println("Please enter the phone number:");
String phone = sc.nextLine();
Contact contact = new Contact(name, phone);
contactList.insert(contact);
break;
case 2:
contactList.display();
break;
case 3:
System.out.println("Please enter the name:");
name = sc.nextLine();
contactList.search(name);
break;
case 4:
System.out.println("Please enter the name:");
name = sc.nextLine();
contactList.delete(name);
break;
case 5:
System.out.println("Please enter the name:");
name = sc.nextLine();
System.out.println("Please enter the new phone number:");
phone = sc.nextLine();
contactList.modify(name, phone);
break;
case 6:
System.exit(0);
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
使用单链表方式实现在线通讯录的设计,可以对通讯录作插入、删除、显示、查找、修改等操作。通过控制台输入,用户可以选择相应的操作。如果用户选择添加联系人,则需要输入姓名和电话号码,程序将自动将联系人添加到通讯录中。如果用户选择查看联系人,则程序将显示所有联系人的姓名和电话号码。如果用户选择查找联系人,则需要输入联系人姓名,程序将在通讯录中查找该联系人并显示其姓名和电话号码。如果用户选择删除联系人,则需要输入联系人姓名,程序将从通讯录中删除该联系人。如果用户选择修改联系人,则需要输入联系人姓名和新的电话号码,程序将修改联系人的电话号码。如果用户选择退出,则程序将退出。
阅读全文