3 回答

TA貢獻2065條經驗 獲得超14個贊
將插入方法更改為:
public void insertAfter(int givenData, int newData) {
Node n = head;
while (n != null && n.data != givenData) {
n = n.next;
}
if (n != null) return;
Node newNode = new Node(newData);
newNode.next = n.next;
n.next = newNode;
}
并使用以下方法刪除方法:
public void remove(int current) {
Node prev = head;
if(head == null) return;
if(head.data == current){head = null; return;}
Node n = prev.next;
while( n != null && n.data != current){
prev = n;
n = n.next;
}
if (n == null) return;
prev.next = n.next;
}

TA貢獻1796條經驗 獲得超4個贊
好的,您可能需要稍微增強您的代碼。首先,你需要一個清單。所以讓我們從那個開始。
public class MyLinkedList {
public Node *head = NULL;
}
head 指向列表的前面。
現在, insertAfter 的基本算法很簡單:
查找包含“插入位置”值的節點
創建一個新節點并在之后插入。
可愛吧?
void insertAfter(int beforeData, int valueToInsert) {
Node *ptr = head;
Node *lastPtr = NULL;
while (ptr != NULL && ptr->value != beforeData) {
lastPtr = ptr;
ptr = ptr->next;
}
// At this point, ptr COULD be null, if the data is never found.
// Insertion is at the end, and lastPtr helps with that.
Node * newNode = new Node(valueToInsert);
if (ptr == NULL) {
# At the end of the list.
if (lastPtr != NULL) {
lastPtr->next = newNode;
newNode->previous = lastPtr;
}
else {
// The entire list is null
head = newNode;
}
}
else {
// ptr points to the data we were searching for, so insert after.
newNode->next = ptr->next;
newNode->previous = ptr;
ptr->next = newNode;
}
}
然后,您可以執行以下操作:
MyLinkedList list;
list.insertAfter(0, 1);
list.insertAfter(1, 17);
list.insertAfter(1, 10);
如果您隨后轉儲列表的內容(您需要從 head 遍歷 next 指針),您將看到列表按 1、10、17 的順序包含。

TA貢獻1790條經驗 獲得超9個贊
我試了一下。
請注意,我實際上并不知道 remove 真正應該做什么。
您的代碼正在尋找具有給定索引的節點,因此我堅持使用該節點(第一個刪除版本)。
但評論建議,您需要刪除具有給定數據的節點。所以我添加了第二個版本,就是這樣做的(第二個刪除版本)。
public void insertAfter(int givenData, int newData) {
// Your code here
Node previous = head;
while (null != previous && previous.data != givenData) {
previous = previous.next;
}
// this prevents insertion if given Data was not found
if (previous == null) {
System.out.println("the given data doesn't exist");
return;
}
Node newNode = new Node(newData);
newNode.next = previous.next;
previous.next = newNode;
}
刪除具有給定索引的節點的版本:
// Removes the Node with the given index
public void remove(int current) {
Node previous = head;
for (int i = 0; i < current - 1; i++) {
if (null == previous) {
System.out.println("the given position doesn't exist");
return;
}
previous = previous.next;
}
if (current == 0) {
head = head.next;
} else if (previous.next == null) {
System.out.println("the given position is after the end - nothing to do!");
} else {
previous.next = previous.next.next;
}
}
刪除具有給定數據的元素的版本
// Removes the Node with the given data
public void remove(int data) {
Node previous = head;
if(head == null){
System.out.println("Empty list - nothing to remove!");
return;
} if (head.data == data){
head = head.next;
return;
}
while(previous.next != null && previous.next.data != data){
previous = previous.next;
}
if (previous.next == null) {
System.out.println("the given element was not found - nothing to do!");
} else {
previous.next = previous.next.next;
}
}
添加回答
舉報