用双向链表作数据结构,用java语言编写一个通讯录管理系统,本系统应完成一下几方面的功能: 1)输入信息——enter(); 2)显示信息———display( ); 3)查找以姓名作为关键字 ———search( ); 4)删除信息———delete( ); 5)存盘———save ( ); 6)装入———load( ) ; 设计要求: 1)每条信息至包含 :姓名(NAME )街道(STREET)城市(CITY)邮编(EIP)国家(STATE)几项 2)作为一个完整的系统,应具有友好的界面和较强的容错能力,上机能正常运行
时间: 2023-10-22 21:07:58 浏览: 101
以下是一个使用双向链表作为数据结构的通讯录管理系统的 Java 代码,包括输入信息、显示信息、查找、删除、存盘和装入等功能:
```java
import java.io.*;
import java.util.Scanner;
class Contact {
String name;
String street;
String city;
String zip;
String state;
Contact prev;
Contact next;
public Contact(String name, String street, String city, String zip, String state) {
this.name = name;
this.street = street;
this.city = city;
this.zip = zip;
this.state = state;
prev = null;
next = null;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Street: " + street);
System.out.println("City: " + city);
System.out.println("Zip: " + zip);
System.out.println("State: " + state);
}
}
class ContactList {
Contact head;
Contact tail;
public ContactList() {
head = null;
tail = null;
}
public void enter() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter street: ");
String street = sc.nextLine();
System.out.print("Enter city: ");
String city = sc.nextLine();
System.out.print("Enter zip: ");
String zip = sc.nextLine();
System.out.print("Enter state: ");
String state = sc.nextLine();
Contact newContact = new Contact(name, street, city, zip, state);
if (head == null) {
head = newContact;
tail = newContact;
} else {
tail.next = newContact;
newContact.prev = tail;
tail = newContact;
}
}
public void display() {
Contact current = head;
while (current != null) {
current.display();
current = current.next;
}
}
public void search() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name to search: ");
String name = sc.nextLine();
Contact current = head;
boolean found = false;
while (current != null) {
if (current.name.equalsIgnoreCase(name)) {
current.display();
found = true;
}
current = current.next;
}
if (!found) {
System.out.println("No contact found with name " + name);
}
}
public void delete() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name to delete: ");
String name = sc.nextLine();
Contact current = head;
boolean found = false;
while (current != null) {
if (current.name.equalsIgnoreCase(name)) {
found = true;
if (current == head) {
head = current.next;
if (head != null) {
head.prev = null;
}
} else if (current == tail) {
tail = current.prev;
if (tail != null) {
tail.next = null;
}
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
System.out.println("Contact deleted.");
break;
}
current = current.next;
}
if (!found) {
System.out.println("No contact found with name " + name);
}
}
public void save() {
try {
FileOutputStream fos = new FileOutputStream("contacts.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(head);
oos.close();
fos.close();
System.out.println("Contacts saved to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
public void load() {
try {
FileInputStream fis = new FileInputStream("contacts.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
head = (Contact) ois.readObject();
ois.close();
fis.close();
System.out.println("Contacts loaded from file.");
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ContactList list = new ContactList();
while (true) {
System.out.println("1. Enter contact");
System.out.println("2. Display contacts");
System.out.println("3. Search contact");
System.out.println("4. Delete contact");
System.out.println("5. Save contacts to file");
System.out.println("6. Load contacts from file");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1:
list.enter();
break;
case 2:
list.display();
break;
case 3:
list.search();
break;
case 4:
list.delete();
break;
case 5:
list.save();
break;
case 6:
list.load();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
}
}
}
}
```
在这个实现中,`Contact` 类表示一个联系人,包含姓名、街道、城市、邮编和国家等信息,以及前后指针。`ContactList` 类表示一个联系人列表,维护头尾指针,并提供输入、显示、查找、删除、存盘和装入等方法。主函数提供了一个菜单,用户可以选择不同的功能。通讯录的信息可以保存到文件中,也可以从文件中装入。
阅读全文