Java 链表删除和insertAfter方法
我在使用delete和insertAfter方法时遇到了问题,这些方法用于执行操作并返回一个布尔值,如果操作成功(true)或失败(false)。InsertAfter方法将插入一个字符串,但始终在同一位置,Delete方法将始终删除同一节点Java 链表删除和insertAfter方法,java,methods,linked-list,Java,Methods,Linked List,我在使用delete和insertAfter方法时遇到了问题,这些方法用于执行操作并返回一个布尔值,如果操作成功(true)或失败(false)。InsertAfter方法将插入一个字符串,但始终在同一位置,Delete方法将始终删除同一节点 private class StrNode { String data; StrNode next; } private StrNode head; // the head of the singly
private class StrNode {
String data;
StrNode next;
}
private StrNode head; // the head of the singly-linked list.
public StringList() {
head = null;
}
public void prepend(String s) {
var newNode = new StrNode();
// TODO: Adds an item to the start of the list.
newNode.data = s;
if(head == null) {
head = newNode;
}
else {
newNode.next = head;
head = newNode;
}
}
/**
* Inserts an item after the first instance of a key if the key exists.
*
* @param s the item to insert
* @param key the item in the list to insert after
* @return whether the insertion was successful
*/
public boolean insertAfter(String s, String key) {
// TODO: Inserts an item after the first instance of a key if the key exists.
var newNode = new StrNode();
StrNode current = head;
newNode.data = s;
if(head == null) {
head = newNode;
}
else if(current == newNode.next){
current.next = newNode;
current = newNode;
}
else {
newNode.next = current.next;
current.next = newNode;
}
return false;
}
/**
* Deletes the first instance of an item from the list.
*
* @param key the value of the item to delete from the list.
* @return whether the deletion was successful.
*/
public boolean delete(String key) {
// TODO: Deletes the first instance of an item from the list.
StrNode current = head;
StrNode sucNode = current;
if(current == null) {
sucNode = head.next;
head = sucNode;
return true;
}
else if(current.next != null) {
sucNode = current.next.next;
current.next = sucNode;
return true;
}
return false;
}
我想用的主要方法是在三之后插入四,应该是:三,二,四,一。
但我得到:三,四,二,一
delete方法只是删除四个,实际上应该是:三,四,二
但我得到:三,二,一
主要内容:
您询问了两种方法:
insertAfter
一些问题:
- 您不使用参数
键
- 如果
为null,则永远不能满足以下条件:节点应插入到具有密钥的节点之后,因此在这种情况下,应返回falsehead
被初始化为current
,因此您正在将新节点分配给head
不检查head。下一步
是否匹配键
- 您总是返回
,从不返回false
true
public boolean insertAfter(String s, String key) {
// Inserts an item after the first instance of a key if the key exists.
StrNode current = head;
while (current != null) {
if (current.data == key) { // found the insertion spot
var newNode = new StrNode();
newNode.data = s;
newNode.next = current.next;
current.next = newNode;
return true;
}
current = current.next; // need to walk along the list
}
return false; // didn't find the key
}
删除
- 您不使用参数
键
- 如果
(ishead
)为null,则永远不能满足要删除的节点应具有给定密钥的条件,因此在这种情况下,应返回falsecurrent
- 在另一种情况下,您总是在不检查
键的情况下删除第二个节点
public boolean delete(String key) {
// Deletes the first instance of an item from the list.
StrNode current = head;
if (head == null) return false;
if (head.data == key) {
head = head.next;
return true;
}
while (current.next != null) {
if (current.next.data == key) { // found it
current.next = current.next.next; // delete it
return true;
}
current = current.next; // need to walk along the list
}
return false; // not found
}
关于前置的备注
:
您不需要if
语句。else
块中的代码在head
为空时工作正常,因此您的代码可以是:
public void prepend(String s) {
// Adds an item to the start of the list.
StrNode node = new StrNode();
node.data = s;
node.next = head;
head = node;
}
我想我昨天看到了这个问题。你又发了吗?我清楚地记得prepend()方法,它给我的印象是一个奇怪的方法名。我会使用addFirst()。无论如何,你应该包括prepend()方法的代码,因为这就是你设置的方式。是的,我的错,我想我会让事情变得更清楚,只是为了看看我正在尝试的方法谢谢!
public void prepend(String s) {
// Adds an item to the start of the list.
StrNode node = new StrNode();
node.data = s;
node.next = head;
head = node;
}