package phonelist;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class LinkedList {
private Node head;
private int size = 0;
public LinkedList() {
load(); // Load a contacts from a file
}
public void add(Contact contact) {
Node newNode = new Node(contact, null);
size++;
if (head == null) {
newNode.setNextNode(head);
head = newNode;
} else {
Node c = head;
while ((c.getNextNode() != null)) {
c = c.getNextNode();
}
newNode.setNextNode(c.getNextNode());
c.setNextNode(newNode);
}
}
public boolean remove(String name) {
return true;
}
public Contact search(String name) {
return null;
}
public String getAll() {
return null;
}
/** Save contacts to a text file **/
public void save() {
if (size == 0) {
return;
}
Scanner in = new Scanner(System. in );
try {
PrintWriter outFile = new PrintWriter("contacts.txt");
if (head != null) {
Node currentNode = head;
do {
Contact contact = currentNode.getContact();
outFile.println(contact.getName() + "," + contact.getNumber());
currentNode = currentNode.getNextNode();
} while (currentNode != null);
}
outFile.close();
} catch (FileNotFoundException e) {
System.out.println("Cannot find that file");
}
}
/** Load contacts from a text file **/
public void load() {
try {
FileReader file = new FileReader("contacts.txt");
Scanner inFile = new Scanner(file);
while (inFile.hasNext()) {
String contact = inFile.nextLine();
int index = contact.indexOf(',');
String name = contact.substring(0, index);
String number = contact.substring(index + 1, contact.length());
add(new Contact(name, number));
}
inFile.close();
} catch (FileNotFoundException e) {
System.out.println("Cannot find that file");
}
}
}
我无法弄清楚如何删除特定节点,也无法弄清楚如何正确搜索。它不断给我一个节点位置而不是我添加的。我花了最后5个小时来完成这项工作,如果没有至少能够搜索,我无法完成其余的工作。如果有人可以在这里给我一些指示,或者给我一些例子,那将非常感激。
这是我尝试删除方法的方法。
public boolean remove(String name) {
if (name.equals(name)) {
remove(name);
return true;
} else {
return false;}}
节点类
package phonelist;
public class Node {
private Contact contact;
private Node next;
public Node(Contact contact, Node next) {
// Do something here
this.contact = contact;
this.next = next;
}
public void setNextNode(Node next) {
// Do something here
this.next = next;
}
public Node getNextNode() {
// Replace return null with something useful
return next;
}
public Contact getContact() {
// Replace return null with something useful
return contact;
}
}
联系班级
package phonelist;
public class Contact {
private String name;
private String number;
public Contact(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
}
在LinkeList类中我创建了一个toString()方法,但我目前只打印出内存中的节点位置而不是实际数据。想法?
@Override
public String toString() {
return "LinkedList{" + "head=" + head + ", size=" + size + '}';
}
public String getAll() {
System.out.println(toString());
return null;
答案 0 :(得分:2)
至于搜索,你可以在你的search(String name)
方法中编写一个while循环,从头开始并按照你在add函数中完成的方式遍历列表(你已经完成了:
Node c = head;
while ((c.getNextNode() != null)) {
c = c.getNextNode();
}
)。但是,当指向的当前联系人在下一次联系时具有null时,不是停止,而是在当前联系人具有您正在搜索的名称时停止。在不找到联系人的情况下,一旦到达列表末尾,请小心停止(所以基本上只是在现有的循环条件中添加一些东西)。
至于删除元素,它与搜索相同,但还有一个额外的步骤。这就像搜索循环一样,但这次你想要停止下一个联系人是你正在寻找的那个。然后在要删除的联系人之后将当前节点的“下一个”引用设置为节点。
我可以用一个例子编写这个代码,但我会把它留给你,所以你一定会理解它:-)祝你好运!
答案 1 :(得分:0)
请参阅Javadoc中的解释。如果您有任何疑问,请与我联系。
搜索方法
/**
*
* @param name this the name of the contact to search for. If list has two
* contacts with same name the first one found will be used
* @return Contact
*/
public Contact search(String name) {
Node currentNode = head;
Contact contact = null;
for (int i = 0; i < size; i++) {
if (currentNode.getContact().getName().equalsIgnoreCase(name)) {
contact = currentNode.getContact();
break;
}
currentNode = currentNode.getNextNode();
}
return contact;
}
删除方法和辅助方法searchNodeOneBeforeTheTargetNode
/**
*
* @param name of the contact to remove
* @return True for successful removal
*/
public boolean remove(String name) {
Node node = searchNodeOneBeforeTheTargetNode(name);
boolean removalSuccessfull = false;
if (node != null) {
node.setNextNode(node.getNextNode().getNextNode());
removalSuccessfull = true;
}
return removalSuccessfull;
}
/**
* Pay attention to this method. Since your linked list a Singly linked
* list, meaning every node only knows about the next node not the previous.
* This method return the previous node to of the target node. All that is
* needed to be done here is that we get the next node of target and
* attached that to the previous node of the target e.d if we want to remove B from below
*
* A->B->C A know B but B does not know about A it know about C . To delete B we connect A->C and B is deleted
*
*
*
* @param name
* @return
*/
private Node searchNodeOneBeforeTheTargetNode(String name) {
Node currentNode = head;
Node nodeOneBeforeTheTargetNode = null;
for (int i = 0; i < size; i++) {
if (currentNode.getContact().getName().equalsIgnoreCase(name)) {
nodeOneBeforeTheTargetNode = currentNode;
break;
}
currentNode = currentNode.getNextNode();
}
return nodeOneBeforeTheTargetNode;
}
获取所有方法注意您的方法返回String。如果您获得链接列表的所有元素(在这种情况下是联系人),那么它应该返回一些集合。我正在使用Array,因为这是最基本的。
/**
*
* @return Array of all contacts in linked list
*/
public Contact[] getAll() {
Contact[] contacts = new Contact[size];
Node currentNode = head;
for (int i = 0; i < size; i++) {
if (currentNode != null) {
contacts[i] = currentNode.getContact();
currentNode = currentNode.getNextNode();
}
}
return contacts;
}