[英]Remove all occurrences of item from a Linked List
我已經在這個實驗室任務工作了幾個小時,無法理解為什么這個代碼不起作用。 問題是添加方法int removeEvery(T item)
,刪除所有出現的item,並將刪除的項目數返回到實現鏈接列表界面的鏈接列表類。
這是我的代碼:它刪除了一些項目,但不是全部。
public int removeEvery(T item){
int index = 0;
Node currentNode = firstNode;
for(int i = 1; i <= numberOfEntries; i++)
{
System.out.println(currentNode.getData());
if (item.equals(currentNode.getData())){
index++;
remove(i);}
else{
currentNode = currentNode.getNextNode();}
}
if(index != 0)
return index;
return -1;
}
這是LinkList類中包含的remove方法:
public T remove(int givenPosition)
{
T result = null; // return value
if ((givenPosition >= 1) && (givenPosition <= numberOfEntries))
{
assert !isEmpty();
if (givenPosition == 1) // case 1: remove first entry
{
result = firstNode.getData(); // save entry to be removed
firstNode = firstNode.getNextNode();
if (numberOfEntries == 1)
lastNode = null; // solitary entry was removed
}
else // case 2: givenPosition > 1
{
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeToRemove = nodeBefore.getNextNode();
Node nodeAfter = nodeToRemove.getNextNode();
nodeBefore.setNextNode(nodeAfter); // disconnect the node to be removed
result = nodeToRemove.getData(); // save entry to be removed
if (givenPosition == numberOfEntries)
lastNode = nodeBefore; // last node was removed
} // end if
numberOfEntries--;
} // end if
return result; // return removed entry, or
// null if operation fails
} // end remove
我認為你的問題來自remove(i)
。
當你刪除第i-th
元素時,第i+1-th
元素變為第i-th
,依此類推:每個元素都被移位。 因此,如果您需要刪除列表中位於索引j
和j+1
的j-th
元素,則刪除調用remove(j)
j-th
元素將移動索引j
處的j+1-th
元素。 因此刪除第二個元素需要再次調用remove(j)
,而不是remove(j+1)
。
所以你需要在刪除后減少i
。
由於remove
方法實際上會減少numberOfEntries
,因此while
循環上的條件已正確更新。 所以你需要做的就是更換
if (item.equals(currentNode.getData())) {
index++;
remove(i);
}
else {
currentNode = currentNode.getNextNode();
}
通過
if (item.equals(currentNode.getData())) {
index++;
remove(i--);
}
// update the current node, whether removing it or not
currentNode = currentNode.getNextNode();
Iterator.remove()
您正在描述的這個問題顯示了Iterator.remove()在使用JDK中的數據結構來瀏覽可迭代集合並在執行它時刪除元素時的用處。
鏈接列表有一些特殊內容,您可以使用current.getNextNode訪問下一個元素,但使用元素索引刪除。 您應該查看實現的其余部分如何管理此索引。 第一個元素是否具有索引0或1(以1開始循環)。 刪除一個元素的索引會發生什么。 元素是否知道他們的索引?
你可以用類似的東西
int deletedNodes = 0;
int currentIndex = 0; // check if 1 or 0
currentNode = fist;
while(currentNode != null){ // I guess lastNode.getNextNode() is null
if(//should remove){
remove(currentIndex);
deletedNodes++
// probably no need to change the index as all element should have been shifted back one index
} else {
currentIndex++; // index changes only if no node was deleted
}
currentNode = currentNode.getNextNode(); // will work even if it was deleted
}
return deletedNodes;
刪除節點后,如@Vakimshaar建議的那樣,您需要遞減i
因為此索引處的節點已被刪除,並且在同一索引處有一個新節點。 除此之外,您還需要更新currentNode
引用,因為它仍然指向您剛剛刪除的節點,但它應該指向已移動到此索引的新節點。
所以在if (item.equals(currentNode.getData())){
block中你需要執行以下操作:
Node nextNode = currentNode.getNextNode();
index++;
remove(i--);
currentNode = nextNode;
有了這個,您的代碼應該正確刪除所有出現。
這是一個Java代碼,用於刪除鏈接列表中所有項目的出現:
public class LinkedList{
Node head;
class Node{
int data;
Node next;
Node(int d){data =d; next = null;}
}
public void push(int new_data){
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void insertAfter(Node givenNode, int new_data){
if(givenNode == null)
System.out.println("Given node cannot be empty");
Node new_node = new Node(new_data);
new_node.next = givenNode.next;
givenNode.next = new_node;
}
public void append(int new_data){
Node new_node = new Node(new_data);
if(head == null)
head = new_node;
else{
Node last = head;
while(last.next != null)
last = last.next;
last.next = new_node;
}
}
public void printList(){
Node temp = head;
while(temp != null){
System.out.println(temp.data + " ");
temp = temp.next;
}
}
void deleteNode(int key){
// Store head node
Node temp = head, prev=null;
// If head node itself holds the key or multiple occurrences of key
while(temp != null && temp.data == key){
head = temp.next;
temp = head;
}
// Delete occurrences other than head
while(temp != null){
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev.next'
while(temp != null && temp.data != key){
prev = temp;
temp = temp.next;
}
// If key was not present in linked list
if(temp == null) return;
// Unlink the node from linked list
prev.next = temp.next;
//Update Temp for next iteration of outer loop
temp = prev.next;
}
}
public static void main(String[] args){
LinkedList llist = new LinkedList();
llist.push(6);
llist.append(7);
llist.append(7);
llist.append(7);
llist.append(9);
llist.push(10);
llist.deleteNode(7);
llist.printList();
}
}
輸出:
10 6 9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.