Click here to Skip to main content
16,015,504 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I am trying to do a Doubly linked list implementation of the Deque and I keep getting this error:

MSIL
15 120
Exception in thread "main" EmptyDequeException: Deque is empty.
    at ListDeque.getFirst(ListDeque.java:83)
    at ListMain.main(ListMain.java:14)


I went over my code 100 times and can't figure out what's wrong..can someone please help.. my code is copied below...thanks in advance


MSIL
import java.lang.*;
public class ListDeque
{
   protected DNode header, trailer;  // dummy nodes
   protected int size;    // number of elements
   public ListDeque()     // constructor: initialize an empty deque
   {
      header = new DNode( 0, null, null );
      trailer = new DNode( 0, null, null );
      header.setNext(trailer);  // make header point to trailer
      trailer.setPrev(header);  // make trailer point to header
      size = 0;
   }

    /**
     * Display the content of the deque
     *
     */
    public void printDeque()
    {
    for ( DNode p = header.getNext(); p != trailer; p = p.getNext() )
        System.out.print( p.getElement() + " " );
    System.out.println();
    }

   // ***************************************
   // DO NOT MODIFY THE CODE ABOVE THIS LINE.
   // ADD YOUR CODE BELOW THIS LINE.
   //
   // ***************************************
   /**
     * Returns the number of items in this collection.
     * @return the number of items in this collection.
     */
    public int size()
    {
      return size; // replace this line with your code
    }

    /**
     * Returns true if this collection is empty.
     * @return true if this collection is empty.
     */
    public boolean isEmpty()
    {
        if(size==0)
        {
             return true;
        }
        return false;
        //return header.getNaxt()== trailer;

    }

    /**
     * Returns the first element of the deque
     *
     */
    public int getFirst() throws EmptyDequeException
    {
        if(isEmpty())
        {
            throw new EmptyDequeException("Deque is empty.");
        }
        return header.getNext().getElement();
    }

    /**
     * Returns the last element of the deque
     *
     */
    public int getLast() throws EmptyDequeException
    {
        // COMPLETE THIS METHOD
        if(isEmpty())
        {
            throw new EmptyDequeException("Deque is empty.");
        }
        return trailer.getPrev().getElement(); // replace this line with your code
    }

    /**
     * Inserts e at the beginning (as the first element) of the deque
     *
     */
    public void insertFirst(int e)
    {
        DNode newNode = new DNode(e, header, header.getNext());
        header.getNext().setPrev(newNode);
        header.setNext(newNode);
        //addAfter(header, e);
    }

    /**
     * Inserts e at the end (as the last element) of the deque
     *
     */
    public void insertLast( int e )
    {
        DNode newNode = new DNode(e, trailer, trailer.getPrev());
        trailer.getPrev().setNext(newNode);
        trailer.setPrev(newNode);
        //addBefore(trailer, e);
    }

    /**
     * Removes and returns the first element of the deque
     *
     */
    public int removeFirst() throws EmptyDequeException
    {
      if(isEmpty())
      {
        throw new EmptyDequeException("Deque is empty.");
      }
      DNode first= header.getNext();
      int o = first.getElement();
      DNode second = first.getNext();
      header.setNext(second);
      second.setPrev(header);
      size--;
      return o;   // replace this line with your code
    }

    /**
     * Removes and returns the last element of the deque
     *
     */
    public int removeLast() throws EmptyDequeException
    {
      if(isEmpty())
      {
        throw new EmptyDequeException("Deque is empty.");
      }
      DNode last = trailer.getPrev();
      int o = last.getElement();
      DNode secondtolast = last.getPrev();
      trailer.setPrev(secondtolast);
      secondtolast.setNext(trailer);
      size--;
      return o;
    }

} // end class
Posted
Updated 4-Mar-11 3:07am
v3
Comments
Richard MacCutchan 4-Mar-11 9:14am    
Forgive me for pointing out the obvious, but your code throws that exception if the queue is empty when you call getFirst.
bowww 4-Mar-11 9:24am    
yes but the test cases are:

q.insertFirst(120);
q.insertFirst(15);
q.printDeque();
System.out.println("first=" + q.getFirst());
System.out.println("last=" + q.getLast());

so there should be something in the deque..
Richard MacCutchan 4-Mar-11 10:03am    
See Fredrik's answer.
Fredrik Bornander 4-Mar-11 10:44am    
Could you let me know if this fixed it?
Fredrik Bornander 4-Mar-11 13:38pm    
An accept would be cool...

I can't see any place where you're increasing the size variable.
You're relying on it in the isEmpty method but it's not incremented in the insertFirst method.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Richard MacCutchan 4-Mar-11 10:02am    
Well spotted.
Sergey Alexandrovich Kryukov 4-Mar-11 12:47pm    
Agree, my 5.
--SA
removeLast must be like:

Java
if (!isEmpty()) {
  Node<T> last = trailer.getPrev();
  T o = last.getElement();
  Node<T> secondtolast = last.getPrev();
  trailer.setPrev(secondtolast);
  secondtolast.setNext(trailer);
  size--;
  return o;
  } else {
    throw new DequeEmptyException("Deque is empty!");
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900