Question

I am trying to implement a Deque in java using linked list. As a start I want to implement the method 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"
   }
 }
}
Was it helpful?

Solution

Change oldFirst.next = first to first.next = oldFirst in addFirst() and it should work.

Right now first.next after addFirst() call isn't pointing to anything, as you're setting it to null. This causes the hasNext() method to return false, resulting in invalid iteration.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top