java - 链表的迭代器

标签 java linked-list iterator

我的项目应该实现两个类。基本链表和排序链表。一切似乎都工作正常,除了由于某种原因我无法迭代排序的链表。类结构如下:

public class BasicLinkedList<T> implements Iterable<T> {
    public int size;

    private class Node {
        private T data;
        private Node next;

        private Node(T data) {
            this.data = data;
            next = null;
        }
    }

    private Node head;
    private Node tail;

    public BasicLinkedList() {
        head = tail = null;
    }
//Add, remove method 

public Iterator<T> iterator() {
        return new Iterator<T>() {

            Node current = head;

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

            @Override
            public T next() {
                if(hasNext()){
                    T data = current.data;
                    current = current.next;
                    return data;
                }
                return null;
            }

            @Override
            public void remove(){
                throw new UnsupportedOperationException("Remove not implemented.");
            }

        };

现在,当我测试这个类时,它工作得很好。迭代器可以工作,我可以测试一切。问题出在扩展了这个类的排序链表类中。这是它的实现和我在构造函数中使用的比较器类:

public class SortedLinkedList<T> extends BasicLinkedList<T>{
    private class Node{
        private T data;
        private Node next;

        private Node(T data){
            this.data = data;
            next = null;
        }
    }

    private Node head;
    private Node tail;
    private Comparator<T> comp;

    public SortedLinkedList(Comparator<T> comparator){
        super();
        this.comp = comparator;
    }

这是比较器类和我在单独的类中运行的测试:

public class intComparator implements Comparator<Integer>{
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
}

public static void main(String[] args) {
System.out.println("---------------SortedLinkedList--------------");
        SortedLinkedList<Integer> sortedList = new SortedLinkedList<Integer>(new intComparator());
        sortedList.add(3);
        sortedList.add(5);
        sortedList.add(2);
        for(int i: sortedList){
            System.out.println(i);
        }
    }   

什么也没有打印出来。我假设继承的迭代器会帮助我遍历这个,没有问题,并且显然它是合法的,因为 for-each 循环会编译。只是什么也没有打印出来。我调试了它,所有添加、删除的内容都按预期工作。只是迭代器没有做它应该做的事情。我应该为这个类创建一个单独的新迭代器吗?但这不是多余的代码吗,因为我已经继承了它?感谢帮助!

编辑:这是排序列表的添加方法

public SortedLinkedList<T> add(T element){
        Node n = new Node(element);
        Node prev = null, curr = head;
        if(head == null){
            head = n;
            tail = n;
        }
        //See if the element goes at the very front
        else if(comp.compare(n.data, curr.data) <= 0){
            n.next = head;
            head = n;
        }
        //See if the element is to be inserted at the very end
        else if(comp.compare(n.data, tail.data)>=0){
            tail.next = n;
            tail = n;
        }
        //If element is to be inserted in the middle
        else{
            while(comp.compare(n.data, curr.data) > 0){
                prev = curr;
                curr = curr.next;
            }
            prev.next = n;
            n.next = curr;
        }

        size++;
        return this;
    }

最佳答案

1) SortedLinkedList 扩展了 BasicLinkedList 但两者都有

private Node head; 
private Node tail

这是错误的。如果你想在子类中继承这些字段,你应该在父类(super class)中将这些变量标记为 protected ,然后将它们从子类中删除。

2) 私有(private)类 Node 也是如此。您在 SortedLinkedListBasicLinkedList 中声明 Node 类。您应该做的是声明一次(也许在父类(super class)中?)并在两个地方使用相同的类。如果这样做,两个类都应该可以访问构造函数和字段。因此,您必须更改访问修饰符(private 是您现在拥有的)。

我将发布下面有效的代码,但我没有花任何时间进行设计。只是发布它来演示如何更改代码以使其工作。您必须决定使用哪些访问修饰符以及将类放在哪里。

import java.util.Comparator;
import java.util.Iterator;

public class Test {
    public static void main(String[] args) {
        System.out.println("---------------SortedLinkedList--------------");
        SortedLinkedList<Integer> sortedList = new SortedLinkedList<Integer>(new intComparator());
        sortedList.add(3);
        sortedList.add(5);
        sortedList.add(2);
        for (int i : sortedList) {
            System.out.println(i);
        }
    }
}

class BasicLinkedList<T> implements Iterable<T> {
    public int size;

    class Node {
        T data;
        Node next;

        Node(T data) {
            this.data = data;
            next = null;
        }
    }

    protected Node head;
    protected Node tail;

    public BasicLinkedList() {
        head = tail = null;
    }

    // Add, remove method

    public Iterator<T> iterator() {
        return new Iterator<T>() {

            Node current = head;

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

            @Override
            public T next() {
                if (hasNext()) {
                    T data = current.data;
                    current = current.next;
                    return data;
                }
                return null;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Remove not implemented.");
            }

        };

    }
}

class SortedLinkedList<T> extends BasicLinkedList<T> {


    private Comparator<T> comp;

    public SortedLinkedList(Comparator<T> comparator) {
        super();
        this.comp = comparator;
    }

    public SortedLinkedList<T> add(T element) {
        Node n = new Node(element);
        Node prev = null, curr = head;
        if (head == null) {
            head = n;
            tail = n;
        }
        // See if the element goes at the very front
        else if (comp.compare(n.data, curr.data) <= 0) {
            n.next = head;
            head = n;
        }
        // See if the element is to be inserted at the very end
        else if (comp.compare(n.data, tail.data) >= 0) {
            tail.next = n;
            tail = n;
        }
        // If element is to be inserted in the middle
        else {
            while (comp.compare(n.data, curr.data) > 0) {
                prev = curr;
                curr = curr.next;
            }
            prev.next = n;
            n.next = curr;
        }

        size++;
        return this;
    }
}

class intComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o1 - o2;
    }
}

关于java - 链表的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072986/

相关文章:

java - 成对字符串数组?

c++ - 链表深拷贝构造函数

c++ - 字符串迭代器不可取消引用的 C++ 问题

java - 判断链表是否是回文

lua - 下面的Lua迭代器是无状态的吗?

c++ - 在 C++20 中,如何编写连续迭代器?

Java/Android : Literally Compare date of 2 Calendar objects

java - 使用 Md5 保存密码

javascript - 单击带有 Selenium 的动态链接

java - Java中的有序LinkedList逻辑,每次都会重新分配LinearNode头