简体   繁体   English

Deque实现中的addFirst()方法

[英]addFirst() method in Deque implementation

I am trying to implement a Deque in java using linked list. 我试图使用链表在java中实现Deque。 As a start I want to implement the method addFirst() . 首先,我想实现addFirst()方法。 Here is the problem I am getting -- when I add few Strings, for example, "one", "two" and "three", it is inserting correctly, but when iterating the deque, it is only giving the last added object, not all the objects. 这是我得到的问题 - 当我添加几个字符串,例如,“一个”,“两个”和“三个”时,它正确插入,但是当迭代双端队列时,它只给出最后添加的对象,不是所有的对象。 Is there anything I am missing? 有什么我想念的吗?

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

Change oldFirst.next = first to first.next = oldFirst in addFirst() and it should work. addFirst() oldFirst.next = first更改为first.next = oldFirst ,它应该可以正常工作。

Right now first.next after addFirst() call isn't pointing to anything, as you're setting it to null . 眼下first.nextaddFirst()调用没有指向任何东西,因为你将其设置为null This causes the hasNext() method to return false, resulting in invalid iteration. 这会导致hasNext()方法返回false,从而导致迭代无效。

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

output : 输出:

four
three
two
one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM