用于链表的迭代器



我的项目应该实现两个类。一个基本链表和一个排序链表。一切似乎都很好,除了一些原因,我不能通过排序链表迭代。类结构如下:

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循环可以编译。只是没有打印出来。我调试了它,所有的添加,删除的东西工作如预期。这只是迭代器没有做它应该做的事情。我应该为这个类创建一个单独的新迭代器吗?但这不是多余的代码吗,因为我已经继承了它?感谢帮助!

编辑:下面是排序列表的add方法
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

这是错误的。如果你想在子类中继承这些字段,你应该在超类中将这些变量标记为受保护,并从子类中删除它们。

2) private class Node也是如此。在SortedLinkedListBasicLinkedList中声明Node类。您应该做的是声明它一次(可能在超类中?),并在两个地方使用相同的类。如果这样做,两个类都可以访问构造函数和字段。所以你必须改变访问修饰符(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;
    }
}

相关内容

  • 没有找到相关文章

最新更新