gpt4 book ai didi

java - 链表 Java SearchAll 和 Boolean Remove

转载 作者:行者123 更新时间:2023-12-02 04:38:48 24 4
gpt4 key购买 nike

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;
}
}

联系类(class)

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;

最佳答案

“文字”算法

搜索

对于搜索,您可以在 search(String name) 方法中编写一个 while 循环,该循环从头部开始并迭代列表,就像您在 add 函数中所做的那样(你已经完成的地方:

Node c = head;
while ((c.getNextNode() != null)) {
c = c.getNextNode();
}

)。但是,不要在当前所指向的联系人作为下一个联系人为 null 时停止,而是在当前联系人具有您正在搜索的姓名时停止。到达列表末尾后,如果您找不到联系人,请务必小心停止(因此基本上只需向现有循环条件添加一些内容即可)。

删除

至于删除元素,与搜索相同,但多了一个步骤。这就像搜索循环,但这次您需要在下一个联系人就是您要查找的联系人时停止。然后将当前节点的“下一个”引用设置为要删除的联系人之后的节点。

我可以用一个例子来编写代码,但我会把它留给你,这样你一定会理解它:-)祝你好运!

关于java - 链表 Java SearchAll 和 Boolean Remove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com