[英]Linked List Delete and insertAfter Method
我在使用 delete 和 insertAfter 方法時遇到問題,這些方法是執行操作並返回一個布爾值,如果操作成功(真)或不成功(假)。 InsertAfter 方法將插入一個字符串但總是在同一個位置,Delete 方法將總是刪除同一個節點。
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 方法只是刪除了實際上應該看起來像的四個:三,四,二,但我得到:三,二,一主要:
public static void main(String[] args) {
StringList s = new StringList();
s.prepend("one");
s.prepend("two");
s.prepend("three");
System.out.println(s);
s.insertAfter("four", "three");
System.out.println(s);
System.out.println(s.delete("one"));
System.out.println(s);
}
你問了兩種方法:
insertAfter
一些問題:
key
。head
為 null,則永遠無法滿足應將節點插入到具有鍵的節點之后的條件,因此在這種情況下,您應該返回 false。current
初始化為head
,因此您將新節點分配給head.next
而不檢查key
匹配...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
}
delete
key
。head
(is current
) 為空,則永遠無法滿足要刪除的節點應具有給定鍵的條件,因此在這種情況下應返回 false。key
匹配的情況下刪除第二個節點...更正:
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
}
prepend
備注: 您不需要if
語句。 當head
為空時, else
塊中的代碼工作正常,因此您的代碼可以是:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.