如何从链表中删除项目

时间:2014-02-04 23:15:31

标签: java linked-list bag

我有一个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;    // 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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

您的删除方法存在一些问题。

  • 如果您实际删除节点,则应该只减少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;
    }
}
相关问题