迭代器不打印明显存在的项目是LinkedList

时间:2015-07-12 13:54:28

标签: java iterator

我正在编写一个维护Deque的类(也就是说它可以添加到LinkedList的头部或尾部)。我使用一个主类创建了一个测试,它将一个项添加到头部,一个添加到尾部,看起来它们存在,因为size = 2并打印,但实际的字符串我添加了不打印。为什么呢?

这是我的代码:

import java.util.Iterator;
import java.util.NoSuchElementException;


/**
 *
 * @author David Farthing
 */
public class Deque<Item> implements Iterable<Item> {
    //fields for Deque class
    private LinkedList deque;
    private int numNodes;

    //constructor
    public Deque(){
        deque = new LinkedList();
        numNodes = 0;
    }

        private class DequeIterator implements Iterator<Item> {
        private Node<Item> curr;
        public DequeIterator(Node<Item> head) {
            curr = head;
        }
        @Override
        public boolean hasNext(){return curr != null;}
        @Override
        public void remove(){ throw new UnsupportedOperationException();}

        @Override
        public Item next() {
            if(!hasNext()) throw new NoSuchElementException();
            Item item = curr.data;
            curr = curr.next;
            return item;
        }
    } //end DequeIterator

        //inner class to implement a doubly linked list for the deque
    private class LinkedList {
       Node head;
       Node tail;

       private LinkedList(){
           head = null;
           tail = null;
       }
       private Node getHead(){
           return head;
       }
       private Node getTail(){
           return tail;
       }
    }//end inner class Linked List

    //inner class to implement a Node for the LinkedList
    private class Node<Item>{
        private Item data;
        private Node<Item> next; //connection to the next node in the list
        private Node<Item> prev; //connection to the previous node in the list


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

        private Item getData(){
            return data;
        }
        private Node getNext(){
            return next;
        }
        private Node getPrev(){
            return prev;
        }
    }//end inner class Node

    //add item to end
    public void addLast(Item item){
        Node h = deque.getHead();
        Node t = deque.getTail();
        //if list is empty
        if(h==null && t==null){
            h = new Node(item);
            t = new Node(item);
            h.next = t;
            t.prev = h;
        }
        //if there is only one item in list
        else if(h==t){
            t = new Node(item);
            t.prev = h;
            h.next = t;
        }
        else {
        //get the node previous to tail
        Node oldLast = t.getPrev();
        Node newLastNode = new Node(item);
        newLastNode.next = t;   //the new last node is pointing to tail
        oldLast.next = newLastNode; //the previous last node which temp is
                                 //pointing to now points to new last node 
        }
        numNodes++;
    }

    //remove and return item from front
    public Item removeFirst(){
        Node h = deque.getHead();
        //get Item data from first node in list; uses convert to turn Object 
        //data into Item data
        Item first = convert(h.next.getData()); 
        Node second = h.next.next;  //second item in list
        h.next = second;            //head now points to second item
        second.prev = h;            //second item points back to head
        numNodes--;
        return first;
    }

    //remove and return item from end
    public Item removeLast(){
        //get the node previous to tail
        Node lastNode = deque.getTail().prev;
        //get the data from the last node; uses convert to turn Object into Item
        Item last = convert(lastNode.getData());    
        Node t = deque.getTail();//get the tail itself
        t.prev = lastNode.prev;             //make the tail point back to the
                                            //node previous to the last
        numNodes--;                         //decrement the number of nodes
        return last;
    }

    //return an Iterator over the items from front to end
    @Override
    public Iterator<Item> iterator(){
        Node h = deque.getHead();
        return new DequeIterator(h);
    }

    //convert any object to Item type
    private Item convert(Object o){
        return (Item) o;
    }

    //is the Deque empty
    public boolean isEmpty(){
        if(numNodes == 0) return true;
        else return false;
    }

    //return the number of items in Deuque
    public int size(){
        return numNodes;
    }

    //add item to front
    public void addFirst(Item item){
        Node h = deque.getHead();
        if(h == null) {
            h = new Node(item);
            Node t = deque.getTail();
            t = new Node(item);
            h.next = t;
            t.prev = h;
            numNodes++;
        }
        else {
            Node temp = new Node(item); //create new node containing item
            temp.next = h.next;         //have temp point to the successor to
                                        //head
            h.next.prev = temp;         //prev of first item now points to temp
            h.next = temp;              //successor of head is now the new node
            temp.prev = h;              //the previous pointer now points to h          
            numNodes++;
        }
    }




    //unit test
    public static void main(String[] args){
        Deque myList = new Deque();
        String f = "first";
        String l = "last";        
        myList.addFirst(f);
        myList.addLast(l);
        Iterator itr = myList.iterator();
        while(itr.hasNext()) {
            StdOut.print(itr.next() + " ");
        }
        StdOut.println("Number of nodes: " + myList.size());
    }// end main


}//end class Deque

打印Deque(LinkedList)的输出是:

运行: 节点数:2

问题:为什么while循环中的迭代器不会打印字符串&#34;首先&#34;和&#34;最后&#34;?

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是你实际上没有在列表中放任何东西。

调用addFirst时,它未设置deque.headdeque.tail。 该版本将起作用:

public void addFirst(Item item){
    Node h = deque.getHead();
    if(h == null) {
        h = new Node(item);
        Node t = deque.getTail();
        t = new Node(item);
        h.next = t;
        t.prev = h;
        numNodes++;
        deque.head = h;
        deque.tail = t;
    }

但我怀疑是否需要实施自己的Deque。这是一些试运动吗? JDK包括java.util.LinkedList,这是一个Deque。