用java写一个利用单链表存储词典,可以实现从文件中加载数据、查询单词、添加词条、删除词条、将词典保存到文件等功能。单链表带有专用头结点head。是否区分大小写可自行规定。 一个词条由英文单词或短语及对应的中文释义构成。
时间: 2023-12-29 21:03:46 浏览: 28
好的,下面是一个利用单链表存储词典的Java代码实现。代码中实现了从文件中加载数据、查询单词、添加词条、删除词条、将词典保存到文件等功能。
```java
import java.io.*;
import java.util.*;
// 定义词条类
class WordEntry {
String word;
String meaning;
WordEntry next;
public WordEntry(String word, String meaning) {
this.word = word;
this.meaning = meaning;
this.next = null;
}
}
public class Dictionary {
WordEntry head;
// 从文件中加载数据
public void loadFromFile(String filename) {
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
String[] arr = line.split(" ");
String word = arr[0];
String meaning = line.substring(word.length() + 1);
WordEntry node = new WordEntry(word, meaning);
// 将节点插入到链表中
if (head == null) {
head = node;
} else {
WordEntry p = head;
while (p.next != null) {
p = p.next;
}
p.next = node;
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 查询单词
public void searchWord(String word) {
WordEntry p = head;
while (p != null) {
if (word.equalsIgnoreCase(p.word)) {
System.out.println(p.word + ": " + p.meaning);
return;
}
p = p.next;
}
System.out.println("Word not found.");
}
// 添加词条
public void addWord(String word, String meaning) {
WordEntry node = new WordEntry(word, meaning);
// 将节点插入到链表中
if (head == null) {
head = node;
} else {
WordEntry p = head;
while (p.next != null) {
p = p.next;
}
p.next = node;
}
System.out.println("Word added successfully.");
}
// 删除词条
public void deleteWord(String word) {
if (head == null) {
System.out.println("Dictionary is empty.");
return;
}
if (word.equalsIgnoreCase(head.word)) {
head = head.next;
System.out.println("Word deleted successfully.");
return;
}
WordEntry p = head;
while (p.next != null) {
if (word.equalsIgnoreCase(p.next.word)) {
p.next = p.next.next;
System.out.println("Word deleted successfully.");
return;
}
p = p.next;
}
System.out.println("Word not found.");
}
// 将词典保存到文件
public void saveToFile(String filename) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
WordEntry p = head;
while (p != null) {
bw.write(p.word + " " + p.meaning);
bw.newLine();
p = p.next;
}
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Dictionary saved successfully.");
}
public static void main(String[] args) {
Dictionary dict = new Dictionary();
dict.loadFromFile("dictionary.txt");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter command:");
String cmd = sc.nextLine();
String[] arr = cmd.split(" ");
String op = arr[0];
if (op.equalsIgnoreCase("search")) {
dict.searchWord(arr[1]);
} else if (op.equalsIgnoreCase("add")) {
dict.addWord(arr[1], cmd.substring(op.length() + arr[1].length() + 2));
} else if (op.equalsIgnoreCase("delete")) {
dict.deleteWord(arr[1]);
} else if (op.equalsIgnoreCase("save")) {
dict.saveToFile("dictionary.txt");
} else if (op.equalsIgnoreCase("exit")) {
break;
} else {
System.out.println("Invalid command.");
}
}
}
}
```
以上代码实现了从文件中加载数据、查询单词、添加词条、删除词条、将词典保存到文件等功能。可以根据具体需求进行调整。