Why does this deque not produce the expected result for addLast

I’m writing code for a deque(implemented as a doubly linked list), when I’m running the test client (the main method), the results are not as I expected. Can anyone explain why this is occurring, and possibly, how to fix this bug? I assume it either has to do with addLast

Expected Result:

Task: Begin Elementary Sorts
Task: Finish Implementing RandomizedQueue
Task: Finish Implementing Deque
Task: Testing
Task: Testing
Task: Testing
Task: Testing

Result Obtained:

Task: Begin Elementary Sorts
Task: Finish Implementing RandomizedQueue
Task: Finish Implementing Deque
Task: null
Task: Testing
Task: Testing
Task: Testing

Code

// Implemented via a Doubly Linked-List.
public class Deque<Item> implements Iterable<Item> { 
    private int size;
    private Node first, last;

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

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

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

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

    // construct an empty deque
    public Deque()
    {
        Node n = new Node();
        first = n;
        last = first;
    }

    // is the deque empty?
    public boolean isEmpty(){ return size == 0; }

    // return the number of items on the deque
    public int size() { return size; }

    // add the item to the front
    public void addFirst(Item item)
    {
        if (item == null) throw new IllegalArgumentException();
        Node n = new Node();
        Node oldFirst = first;
        n.item = item;
        n.next = oldFirst;
        first = n;
        oldFirst.previous = n;
        size++;
        return;
    }

    // add the item to the back
    public void addLast(Item item)
    {
        if (item == null) throw new IllegalArgumentException();
        Node n = new Node();
        last.next = n;
        n.previous = last;
        n.item = item;
        last = n;
        size++;
        return;
    }

    // remove and return the item from the front
    public Item removeFirst()
    {
        if (size == 0) throw new NoSuchElementException();

        Node oldFirst = first;
        first = first.next;
        oldFirst.next = null;
        first.previous = null;
        size--;
        return oldFirst.item;
    }

    // remove and return the item from the back
    public Item removeLast()
    {
        if (size == 0) throw new NoSuchElementException();
        Node oldLast = last;
        last = last.previous;
        last.next = null;
        oldLast.previous = null;
        size--;
        return oldLast.item;
    }

    // return an iterator over items in order from front to back
    public Iterator<Item> iterator(){ return new DequeIterator(); }

    // unit testing (required)
    public static void main(String[] args)
    {
        Deque<String> todo = new Deque<String>();
        todo.addFirst("Finish Implementing Deque");
        todo.addFirst("Finish Implementing RandomizedQueue");
        todo.addFirst("Begin Elementary Sorts");
        todo.addLast("Testing");
        todo.addLast("Testing");
        todo.addLast("Testing");
        todo.addLast("Testing");

        // Iterator<String> tasks = todo.iterator();

        // while (tasks.hasNext())
        // {
        //     String task = tasks.next();
        //     StdOut.println(task);
        // }

        // System.out.println(todo.removeFirst());
        // System.out.println(todo.removeLast());
        // System.out.println("After Removal:");
        // if (!todo.isEmpty() && todo.size() != 0)
            for (String task: todo)
                System.out.println("Task: " + task);
    }
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.