在LinkedList上实现自定义迭代器

时间:2015-03-23 07:17:00

标签: java linked-list iterator

我正在尝试在我创建的LinkedList类上创建自定义迭代器。我被要求改变添加功能,以便它从最小到最大的顺序添加对象Term。 (术语是一个简单的类,其形式为Term(int power))

我无法弄清楚如何在addTerm()中创建一个循环,以便继续搜索下一个元素,看它是否大于Term中的当前功率。有人可以帮忙吗?

import java.util.Iterator;

public class customImpl implements custom{

    private static class Node {
        Term data;
        Node next;
    }

    private Node head;

    private class TermIterator implements Iterator<Term> {

        private Node current;

        private TermIterator(Node start) {
            current = start;
        }

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Term next() {
            Term result = current.data;
            current = current.next;
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported");
        }
    }

    /**
     * Add a term to the expression
     *
     * @param term the term to be added.
     */
    @Override
    public void addTerm(Term term) {

        TermIterator iterator = new TermIterator(head);
        Node newNode = new Node();

        while(iterator.hasNext()) {
            if(term.getPower() > iterator.next().getPower()) {
                newNode.next = head;
            }
            else newNode.data = term;
        }

        newNode.data = term;
        newNode.next = head;
        head = newNode;
    }

    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<Term> iterator() {
        return new TermIterator(head);
    }

}

1 个答案:

答案 0 :(得分:1)

您无法轻松使用迭代器,因为它通过值而不是节点:

@Override
public void addTerm(Term term) {

    Node newNode = new Node();
    newNode.term = term;


    Node smaller = null; //smaller holds last element smaller than new term
    Node current = head;
    while(current != null) {
        if(term.getPower() > current.term.getPower()) {
            smaller = current;
            break;
        }
        current = current.next;
    }

    if (smaller == null) {
        newNode.next = head;
        head = newNode;
    } else {
        newNode.next = smaller.next;
        smaller.next = newNode;
    }

}

如果你想使用迭代器,你应该定义'Node'迭代器(并在你的addTerm方法中使用它),然后重新使用它来定义'Term'迭代器:

class NodeIterator implements Iterator<Node> {

    Node next;

    NodeIterator() {
        next = head;
    }

    @Override
    public boolean hasNext() {
        return (next != null);
    }

    @Override
    public Node next() {
        if (next == null) throw new NoSuchElementException();
        Node res = next;
        next = next.next;
        return res;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }        
}

class TermIterator implements Iterator<Term> {

    final NodeIterator iter = new NodeIterator();

    @Override
    public boolean hasNext() {
        return iter.hasNext();
    }

    @Override
    public Term next() {
        return iter.next().term;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}