Java-从链表中删除节点
好的,这只是一个简单的程序,它将接收来自用户的输入,并将其添加到Java-从链表中删除节点,java,Java,好的,这只是一个简单的程序,它将接收来自用户的输入,并将其添加到链接列表,同时为用户提供查看列表和删除节点的选项。它编译良好,可以添加节点并显示列表,但不会删除节点。当我在没有键盘输入的情况下手工编写代码时,即使使用相同的变量名,它也能工作,所以这就是问题所在 public class LinkedList { public class Link { public String content; public Link next;
链接列表
,同时为用户提供查看列表
和删除节点
的选项。它编译良好,可以添加节点并显示列表,但不会删除节点。当我在没有键盘输入的情况下手工编写代码时,即使使用相同的变量名,它也能工作,所以这就是问题所在
public class LinkedList {
public class Link {
public String content;
public Link next;
public Link(String content) {
this.content = content;
}
public void display(){
System.out.println(content);
}
}
public static Link head;
LinkedList(){
head = null;
}
public boolean isEmpty() {
return(head == null);
}
public void insertFirstLink(String content) {
Link newLink = new Link(content);
newLink.next = head;
head = newLink;
}
public void display() {
Link theLink = head;
while(theLink != null) {
theLink.display();
theLink = theLink.next;
}
}
public Link removeLink(String content) {
Link curr = head;
Link prev = head;
while(curr.content != content) {
if (curr.next == null) {
return null;
}
else {
prev = curr;
curr = curr.next;
}
}
if(curr == head) {
head = head.next;
}
else {
prev.next = curr.next;
}
return curr;
}
}
public class Testlist {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice = 0;
String content;
System.out.println("Enter 1 to add to list");
System.out.println("Enter 2 to display list");
System.out.println("Enter 3 to delete node");
System.out.println("Enter 4 to quit");
choice = keyboard.nextInt();
LinkedList newlist = new LinkedList();
while(choice != 4) {
if (choice == 1) {
content = keyboard.next();
newlist.insertFirstLink(content);
newlist.display();
}
if (choice == 2) {
newlist.display();
}
if (choice == 3) {
content = keyboard.next(); // this is where is goes wrong
newlist.removeLink(content);
newlist.display();
}
System.out.println("Enter 1 to add to list");
System.out.println("Enter 2 to display list");
System.out.println("Enter 3 to delete node");
System.out.println("Enter 4 to quit");
choice = keyboard.nextInt();
}
}
}
您正在使用
=代码>,它通过对象的引用而不是值进行比较。您想使用.equals()
,即:
while(!curr.content.equals(content))
有些人可能需要仔细检查,但我相当肯定这是因为nextInt()方法获取第一个整数,仅此而已。在输入流中保留“回车/回车”。因此,当运行next()方法时,它将获取enter。明确地放入一些调试行以查看内容。在这一行中也使用'equals'
:'if(curr==head){。
要比较字符串,应该使用equals或equalsignorecase()
例如String1=“xyz”;
和String2=“xyz”如果在比较对象而不是实际内容时使用==或!=比较这两个字符串,则这两个字符串是不同的
package stackoverflow.practice;
public class LinkedList {
public class Link {
public String content;
public Link next;
public Link(String content) {
this.content = content;
}
public void display(){
System.out.println(content);
}
}
public static Link head;
LinkedList(){
head = null;
}
public boolean isEmpty() {
return(head == null);
}
public void insertFirstLink(String content) {
Link newLink = new Link(content);
newLink.next = head;
head = newLink;
}
public void display() {
Link theLink = head;
while(theLink != null) {
theLink.display();
theLink = theLink.next;
}
}
public Link removeLink(String content) {
Link curr = head;
Link prev = head;
while(!curr.content.equalsIgnoreCase(content)) {
if (curr.next == null) {
return null;
}
else {
prev = curr;
curr = curr.next;
}
}
if(curr == head) {
head = head.next;
}
else {
prev.next = curr.next;
}
return curr;
}
}
发布完整的堆栈跟踪。@user3076911没有问题。