簡體   English   中英

鏈表的迭代器

[英]Iterator for a linkedlist

我的項目應該實現兩個類。 基本鏈表和排序鏈表。 除了某些原因,我無法遍歷已排序的鏈表,一切似乎都工作正常。 類的結構如下:

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

這是錯誤的。 如果要繼承子類中的那些字段,則應將變量標記為超類中的受保護變量,並將其從子類中刪除。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM