Question

I keep getting a null pointer error when i dequeue in my main method. I know the Enqueue(push) method works fine, but it will only not give me an error when i pop if i do it from the opposite one. For example, if i push 3 values in from the begin, i can only pop 1 from the back. If i try to pop from front it gives me an error. And vice versa(i hope i made it clear). So is this the proper way to dequeue using a deque? this is dequeueing from the front, the other similar method goes from the back. Am i missing something?

public T dequeueBegin()
{
     T number = begin.getValue();
     if(isEmpty())
        throw new RuntimeException("Can't dequeue");

     if(begin.getNext() == null)
         end = null;

     begin = begin.getNext();
     size--;
     return number; 
}

No correct solution

OTHER TIPS

You might want to consider using a LinkedList rather than an ArrayList, if you're removing from both the front & the end of your List Collection LinkedList handles this better & offers a whole load of useful methods to do exactly what it looks like you're trying to achieve. LinkedList is often used to implement Queue functionality.

Check out the Javadoc here

http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

methods such as addFirst, addLast, removeFirst, removeLast etc look good for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top