用双向链表作数据结构,用java语言编写一个通讯录管理系统,本系统应完成一下几方面的功能: 1)输入信息——enter(); 2)显示信息———display( ); 3)查找以姓名作为关键字 ———search( ); 4)删除信息———delete( ); 5)存盘———save ( ); 6)装入———load( ) ; 设计要求: 1)每条信息至包含 :姓名(NAME )街道(STREET)城市(CITY)邮编(EIP)国家(STATE)几项 2)作为一个完整的系统,应具有友好的界面和较强的容错能力,上机能正常运行 3)具有可视化窗口界面
时间: 2023-10-24 12:05:50 浏览: 94
好的,我明白了你的问题。以下是一个用双向链表作数据结构,用 Java 语言编写的通讯录管理系统的基本框架。你可以在此基础上进行扩展和完善。
```java
import java.io.*;
import java.util.*;
class Node {
String name;
String street;
String city;
String zip;
String state;
Node prev;
Node next;
public Node(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;
this.prev = null;
this.next = null;
}
}
public class AddressBook {
Node head;
Node tail;
public AddressBook() {
this.head = null;
this.tail = null;
}
public void enter(String name, String street, String city, String zip, String state) {
Node newNode = new Node(name, street, city, zip, state);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
System.out.println("New entry added.");
}
public void display() {
Node current = head;
while (current != null) {
System.out.println("Name: " + current.name);
System.out.println("Street: " + current.street);
System.out.println("City: " + current.city);
System.out.println("Zip: " + current.zip);
System.out.println("State: " + current.state);
current = current.next;
}
}
public void search(String name) {
Node current = head;
while (current != null) {
if (current.name.equalsIgnoreCase(name)) {
System.out.println("Name: " + current.name);
System.out.println("Street: " + current.street);
System.out.println("City: " + current.city);
System.out.println("Zip: " + current.zip);
System.out.println("State: " + current.state);
return;
}
current = current.next;
}
System.out.println("Entry not found.");
}
public void delete(String name) {
Node current = head;
while (current != null) {
if (current.name.equalsIgnoreCase(name)) {
if (current == head) {
head = current.next;
if (head != null) {
head.prev = null;
} else {
tail = null;
}
} else if (current == tail) {
tail = current.prev;
tail.next = null;
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
System.out.println("Entry deleted.");
return;
}
current = current.next;
}
System.out.println("Entry not found.");
}
public void save(String filename) {
try {
FileWriter fw = new FileWriter(filename);
Node current = head;
while (current != null) {
fw.write(current.name + "," + current.street + "," + current.city + "," + current.zip + "," + current.state + "\n");
current = current.next;
}
fw.close();
System.out.println("Data saved.");
} catch (IOException e) {
System.out.println("Error saving data.");
}
}
public void load(String filename) {
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
String[] fields = line.split(",");
String name = fields[0];
String street = fields[1];
String city = fields[2];
String zip = fields[3];
String state = fields[4];
enter(name, street, city, zip, state);
}
br.close();
System.out.println("Data loaded.");
} catch (IOException e) {
System.out.println("Error loading data.");
}
}
public static void main(String[] args) {
AddressBook book = new AddressBook();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("1. Enter new data");
System.out.println("2. Display all data");
System.out.println("3. Search for data");
System.out.println("4. Delete data");
System.out.println("5. Save data");
System.out.println("6. Load data");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = input.nextInt();
input.nextLine(); // consume newline character
switch (choice) {
case 1:
System.out.print("Enter name: ");
String name = input.nextLine();
System.out.print("Enter street: ");
String street = input.nextLine();
System.out.print("Enter city: ");
String city = input.nextLine();
System.out.print("Enter zip: ");
String zip = input.nextLine();
System.out.print("Enter state: ");
String state = input.nextLine();
book.enter(name, street, city, zip, state);
break;
case 2:
book.display();
break;
case 3:
System.out.print("Enter name: ");
name = input.nextLine();
book.search(name);
break;
case 4:
System.out.print("Enter name: ");
name = input.nextLine();
book.delete(name);
break;
case 5:
System.out.print("Enter filename: ");
String filename = input.nextLine();
book.save(filename);
break;
case 6:
System.out.print("Enter filename: ");
filename = input.nextLine();
book.load(filename);
break;
case 7:
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}
```
这个程序中,`Node` 类表示双向链表中的一个节点,包含了通讯录中的信息以及指向前一个节点和后一个节点的指针。`AddressBook` 类表示整个通讯录,包含了双向链表的头节点和尾节点,以及各种操作方法(如添加、删除、查找、显示等)。在 `main` 方法中,使用一个无限循环来不断等待用户输入操作,根据用户的选择调用相应的方法。
阅读全文