4

リンクリストを使用して Java で Deque を実装しようとしています。まず、メソッドを実装したいと思いaddFirst()ます。これが私が得ている問題です-「1」、「2」、「3」などのいくつかの文字列を追加すると、正しく挿入されますが、両端キューを反復すると、最後に追加されたオブジェクトのみが与えられます。すべてのオブジェクトではありません。不足しているものはありますか?

public class Deque<Item> implements Iterable<Item> {
  private Node first;
  private Node last;
  private int N;

  public Iterator<Item> iterator() { return new DequeIterator(); }

  private class Node {
    private Item item;
    private Node next;
  }

  public Deque() {
    first = null;
    last = null;
    N = 0;
  }

  public boolean isEmpty() { return first == null || last == null; }
  public int size() { return N; }

  public void addFirst(Item item) {
  if (null == item) { throw new NullPointerException("Can not add a null value"); }
  Node oldFirst = first;
  first = new Node();
  first.item = item;
  first.next = null;

  if (isEmpty()) {
    last = first;
  } else {
    oldFirst.next = first;
  }

  N++; 
 }

 private class DequeIterator implements Iterator<Item> {
   private Node current = first;

   public boolean hasNext() { return current != null; }
   public void remove() { throw new UnsupportedOperationException(); }

   public Item next() {
    if (!hasNext()) { throw new NoSuchElementException(); }
    Item item = current.item;
    current = current.next;
    return item;
   }

 }

 public static void main(String args[]) {
   Deque<String> deque = new Deque<String>();
   deque.addFirst("one");
   deque.addFirst("two");
   deque.addFirst("three");
   deque.addFirst("four");

   for (String s : deque) {
     System.out.println(s); // prints only "four"
   }
 }
}
4

2 に答える 2

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

public class Deque<Item> implements Iterable<Item> {

    private Deque.Node first;
    private Deque.Node last;
    private int N;

    public Iterator<Item> iterator() {
        return new Deque.DequeIterator();
    }

    private class Node {

        private Item item;
        private Deque.Node next;
    }

    public Deque() {
        first = null;
        last = null;
        N = 0;
    }

    public boolean isEmpty() {
        return first == null || last == null;
    }

    public int size() {
        return N;
    }

    public void addFirst(Item item) {
        if (null == item) {
            throw new NullPointerException("Can not add a null value");
        }
        if (first == null && last == null) {
            first = new Node();
            first.item = item;
            first.next = null;
            last = first;
        } else {
            Node node = new Node();
            node.item = item;
            node.next = first;
            first = node;
        }

        N++;
    }

    private class DequeIterator implements Iterator<Item> {

        private Deque.Node current = first;

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

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public Item next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Item item = (Item) current.item;
            current = current.next;
            return item;
        }
    }

    public static void main(String args[]) {
        Deque<String> deque = new Deque<String>();
        deque.addFirst("one");
        deque.addFirst("two");
        deque.addFirst("three");
        deque.addFirst("four");

        for (String s : deque) {
            System.out.println(s); // prints only "four"
        }
    }
}

出力:

four
three
two
one
于 2013-09-15T18:23:54.117 に答える