自定义迭代器不打印链表结果

时间:2017-10-23 19:08:09

标签: java linked-list iterator listiterator

目前正在创建一个程序,我正在使用“Java.util。*”中的自定义迭代器。问题是,当我试图迭代程序时,什么都没有出来。这是代码

import java.util.Iterator;
import java.util.ListIterator;

  public class LL_p2{

  public static void main(String[] args){


     LinkedListIterator_1 op = new LinkedListIterator_1();

     op.add(0,"h");
     op.add(1,"2");
     op.add(2,"4");




     Iterator<String> s = op.iterator();
     while(s.hasNext()){
         System.out.println(s.next() + " ");

     }

     //System.out.println(o);
}

}

   class LinkedListIterator_1<E>
    implements java.util.Iterator<E> {
  Node<E> head, tail;
  Node<E> current = head; // Current index
  int size = 0;
  public LinkedListIterator_1() {
  }

  public LinkedListIterator_1(int index) {
    // Implement this for Exercise 24.3
  }

  public void add(int index,E e) {
      if (index == 0) {
            addFirst(e);

          }
          else if (index >= size) {
            addLast(e);
            index++;
          }
          else {
            Node<E> current = tail;

            for (int i = 1; i < index; i++) {
              current = current.next;
           }

            Node<E> temp = current.next;
            current.next = new Node<>(e);
            (current.next).previous = current;
            (current.next).next = temp;
            size++;
          }
  }

      public void addFirst(E e) {
          Node<E> newNode = new Node<>(e); // Create a new node

          // This is orginal source code
           newNode.next = head; // link the new node with the head
           newNode.previous = null;
           head = newNode; // head points to the new node

          size++; // Increase list size

          if (tail == null) // the new node is the only node in list
            tail = head;
        }

        public void addLast(E e) {
              Node<E> newNode = new Node<>(e); // Create a new for element e

              if (tail == null) {
                head = tail = newNode; // The new node is the only node in list
              }
              else {
                tail.next = newNode; // Link the new with the last node
                newNode.previous = tail;
                tail = tail.next; // tail now points to the last node
              }

              size++; // Increase size
            }



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

  @Override
  public E next() {
    E e = current.element;
    current = current.next;
    return e;
  }

  @Override
  public void remove() {
    // Left as an exercise
  }

  @Override
  public String toString() {
      StringBuilder result = new StringBuilder("[");

      Node<E> current = head;
      for (int i = 0; i < size; i++) {
        result.append(current.element);
        current = current.next;
        if (current != null) {
          result.append(", "); // Separate two elements with a comma
        }
        else {
          result.append("]"); // Insert the closing ] in the string
        }
      }
      return result.toString();
  }
        public java.util.Iterator<E> iterator() {
              return new LinkedListIterator_1();
            }

            public java.util.Iterator<E> iterator(int index) {
              return new LinkedListIterator_1(index);
            }

}


class Node<E> {


  E element;
  Node<E> next;
  Node<E> previous; 
  public Node(E element) {
    this.element = element;
  }
}

每次运行时,在添加每个元素后,都不会通过iterable语句打印任何内容。我对使用add语句或我创建的自定义迭代器语句的内容感到困惑。

每次我实现LinkedListIterator_1类时,除非有,否则它不会运行。我认为这是问题所在,但我无法使程序运行。不知道我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

问题在于current指向null

修复:将iterator()方法更改为:

public java.util.Iterator<E> iterator() {
    current = head;
    return this;
}

运行代码后应用修复程序后,我得到输出:

h 
2 
4 

完整代码:

class LinkedListIterator_1<E>
    implements java.util.Iterator<E> {
    Node<E> head, tail;
    Node<E> current; // Current index
    int size = 0;
    public LinkedListIterator_1() {
    }

    public LinkedListIterator_1(int index) {
        current = head;
        while (index > 0) {
            current = current.next;
            index--;
        }
    }

    public void add(int index,E e) {
        if (index == 0) {
            addFirst(e);

        }
        else if (index >= size) {
            addLast(e);
            index++;
        }
        else {
            Node<E> current = tail;

            for (int i = 1; i < index; i++) {
                current = current.next;
            }

            Node<E> temp = current.next;
            current.next = new Node<>(e);
            (current.next).previous = current;
            (current.next).next = temp;
            size++;
        }
    }

    public void addFirst(E e) {
        Node<E> newNode = new Node<>(e); // Create a new node

        // This is orginal source code
        newNode.next = head; // link the new node with the head
        newNode.previous = null;
        head = newNode; // head points to the new node

        size++; // Increase list size

        if (tail == null) // the new node is the only node in list
            tail = head;
    }

    public void addLast(E e) {
        Node<E> newNode = new Node<>(e); // Create a new for element e

        if (tail == null) {
            head = tail = newNode; // The new node is the only node in list
        }
        else {
            tail.next = newNode; // Link the new with the last node
            newNode.previous = tail;
            tail = tail.next; // tail now points to the last node
        }

        size++; // Increase size
    }



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

    @Override
    public E next() {
        E e = current.element;
        current = current.next;
        return e;
    }

    @Override
    public void remove() {
        // Left as an exercise
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("[");

        Node<E> current = head;
        for (int i = 0; i < size; i++) {
            result.append(current.element);
            current = current.next;
            if (current != null) {
                result.append(", "); // Separate two elements with a comma
            }
            else {
                result.append("]"); // Insert the closing ] in the string
            }
        }
        return result.toString();
    }
    public java.util.Iterator<E> iterator() {
        current = head;
        return this;
    }

    public java.util.Iterator<E> iterator(int index) {
        return new LinkedListIterator_1(index);
    }

}