Java 如何从链表中删除项目

Java 如何从链表中删除项目,java,linked-list,bag,Java,Linked List,Bag,我有一个包类,给我的是这样的: import java.util.Iterator; import java.util.NoSuchElementException; public class Bag<Item> implements Iterable<Item> { private int N; // number of elements in bag private Node<Item> first; // begin

我有一个包类,给我的是这样的:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Bag<Item> implements Iterable<Item> {

private int N;               // number of elements in bag
private Node<Item> first;    // beginning of bag

// helper linked list class
private class Node<Item> {
    private Item item;
    private Node<Item> next;
}

/**
 * Initializes an empty bag.
 */
public Bag() {
    first = null;
    N = 0;
}

/**
 * Is this bag empty?
 * @return true if this bag is empty; false otherwise
 */
public boolean isEmpty() {
    return first == null;
}

/**
 * Returns the number of items in this bag.
 * @return the number of items in this bag
 */
public int size() {
    return N;
}

/**
 * Adds the item to this bag.
 * @param item the item to add to this bag
 */
public void add(Item item) {
    Node<Item> oldfirst = first;
    first = new Node<Item>();
    first.item = item;
    first.next = oldfirst;
    N++;
}

public void remove(Item item){ 
    // currentNode is the reference to the first node in the list and to the Item
    Node<Item> currentNode = first; 
    // if items equals the first node in the list, then first = currentNode.next which will make the first item 
    Node<Item> temp = currentNode;
    while(temp.next != null){
        temp = currentNode;
        if(item.equals(currentNode.item)){
            currentNode = currentNode.next;
            temp.next = currentNode;
            break;
        }else{
            currentNode = currentNode.next;
        }
    }
    N--; 
}

/**
 * Returns an iterator that iterates over the items in the bag in arbitrary order.
 * @return an iterator that iterates over the items in the bag in arbitrary order
 */
public ListIterator<Item> iterator()  {
    return new ListIterator<Item>(first);  
}

// an iterator, doesn't implement remove() since it's optional
private class ListIterator<Item> implements Iterator<Item> {
    private Node<Item> current;

    public ListIterator(Node<Item> first) {
        current = first;
    }

    public boolean hasNext()  { return current != null;                     }
    public void remove()      { throw new UnsupportedOperationException();  }

    public Item next() {
        if (!hasNext()) throw new NoSuchElementException();
        Item item = current.item;
        current = current.next; 
        return item;
    }
}
import java.util.Iterator;
导入java.util.NoSuchElementException;
公共类包{
private int N;//包中的元素数
private Node first;//包的开头
//助手链表类
私有类节点{
私人物品;
私有节点下一步;
}
/**
*初始化空包。
*/
公文包(){
第一个=空;
N=0;
}
/**
*这个包是空的吗?
*@如果此包为空,则返回true;否则返回false
*/
公共布尔值为空(){
返回first==null;
}
/**
*返回此包中的项目数。
*@返回此包中的物品数量
*/
公共整数大小(){
返回N;
}
/**
*将项目添加到此包中。
*@param item要添加到此包中的项目
*/
公共作废添加(项目){
节点oldfirst=第一个;
第一个=新节点();
first.item=项目;
first.next=oldfirst;
N++;
}
公共作废删除(项目){
//currentNode是对列表中第一个节点和项目的引用
节点当前节点=第一个;
//如果items等于列表中的第一个节点,那么first=currentNode.next哪个将成为第一个项
节点温度=当前节点;
while(temp.next!=null){
温度=当前节点;
if(item.equals(currentNode.item)){
currentNode=currentNode.next;
temp.next=当前节点;
打破
}否则{
currentNode=currentNode.next;
}
}
N--;
}
/**
*返回一个迭代器,该迭代器按任意顺序遍历包中的项目。
*@返回一个迭代器,该迭代器以任意顺序对包中的项目进行迭代
*/
公共列表迭代器迭代器(){
返回新的ListIterator(第一个);
}
//迭代器不实现remove(),因为它是可选的
私有类ListIterator实现了迭代器{
专用节点电流;
公共ListIterator(节点优先){
电流=第一;
}
公共布尔值hasNext(){返回当前值!=null;}
public void remove(){抛出新的UnsupportedOperationException();}
公共项目下一步(){
如果(!hasNext())抛出新的NoSuchElementException();
项目=当前项目;
当前=当前。下一步;
退货项目;
}
}

我需要实现一个remove方法,为它编写的代码是我的。它不工作,我想知道是否有人能告诉我为什么以及如何更正它?

你的remove方法有几个问题

  • 只有在实际删除节点时才应减少N
  • 如果包中包含重复的项目,那么remove()方法是否应该删除该项目的所有重复项?我希望如此
  • 您需要处理删除第一个节点并更新第一个引用的情况
  • 如果不删除第一个节点,则需要更新上一个节点以指向要删除的节点后的下一个节点
把这一切结合起来:

public void remove(Item item){
    Node<Item> currentNode = first;
    Node<Item> previousNode = null;
    while(currentNode != null){
        if(item.equals(currentNode.item)){
            if(previousNode  == null) {
              first = currentNode.next;
            }
            else {
              previousNode.next = currentNode.next;
            }
            N--;
        }
        else {
          previousNode = currentNode;
        }
        currentNode = currentNode.next;
    }
}
公共作废删除(项目){
节点当前节点=第一个;
节点previousNode=null;
while(currentNode!=null){
if(item.equals(currentNode.item)){
if(previousNode==null){
first=currentNode.next;
}
否则{
previousNode.next=currentNode.next;
}
N--;
}
否则{
previousNode=currentNode;
}
currentNode=currentNode.next;
}
}

首先要做的是在IDE调试器中对代码进行单步调试,看看您是否能够发现哪里出了问题。在这里发布之前,请这样做。我用字符串更改Item类尝试了您的代码,效果很好。我想您应该再试一次,效果很好。非常感谢!非常有帮助