title: Creating a dequeue method using a Circlular linked Queue in java
body: <p>I'm having trouble figuring out how to finish my dequeue method. I'm only allowed a rear pointer instance variable. I am getting the first two entries delelted from the queue but then it doesn't delete the rest. My code is below. I'm super confused what is going on in my method. Thank you</p>
  
  <pre><code>public class CircularLinkedQueue&lt;T&gt; implements QueueInterface&lt;T&gt;
  {
  private Node lastNode;
  
  @Override
  public void enqueue(Object newEntry) 
  {
      Node newNode = new Node(newEntry, null);
      if(lastNode == null)
          newNode.setNextNode(newNode);
      else
      {
          newNode.setNextNode(lastNode.getNextNode());
          lastNode.setNextNode(newNode);
      }
      lastNode = newNode;
  }
  
  @SuppressWarnings("unchecked")
  @Override
  public T dequeue() 
  {
      T result = null;
      if(!isEmpty())
      {
          result = (T) lastNode.getNextNode().getData();
          lastNode = lastNode.getNextNode();
  
          if(lastNode.getNextNode() == null)
              lastNode = null;
          else
              lastNode.getNextNode().setNextNode(null);
      }
      return  result;
  }
  
  @Override
  public T getFront() 
  {
      T results = null;
      if(!isEmpty())
          results = (T) lastNode.getNextNode().getData();
      return results;
  }
  
  @Override
  public boolean isEmpty() 
  {
      if(lastNode == null)
          return true;
      else return false;
  }
  
  @Override
  public void clear() 
  {
      lastNode = null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My driver program for the dequeue should be like this.</p>
  
  <pre><code>    public static void main(String[] args) {
  
      System.out.println("Create a queue: ");
      QueueInterface&lt;String&gt; myQueue = new CircularLinkedQueue&lt;String&gt;();
      myQueue.enqueue("Ann");
      myQueue.enqueue("Bill");
      myQueue.enqueue("Carol");
      myQueue.enqueue("David");
      myQueue.enqueue("Edgar");
      myQueue.enqueue("Fred");
  
      while (!myQueue.isEmpty()) {
          Object front = myQueue.getFront();
          System.out.println("\t" + front + " is at the front of the queue.");
  
          front = myQueue.dequeue();
          System.out.println("\t" + front + " is removed from the front of the queue.");
      } 
  }  
  }
  </code></pre>
  
  <p>Output should look like this</p>
  
  <pre><code>Ann is at the front of the queue.
  
  Ann is removed from the front of the queue.
  
  Bill is at the front of the queue.
  
  Bill is removed from the front of the queue.
  
  Carol is at the front of the queue.
  
  Carol is removed from the front of the queue.
  
  David is at the front of the queue.
  
  David is removed from the front of the queue.
  
  Edgar is at the front of the queue.
  
  Edgar is removed from the front of the queue.
  Fred is at the front of the queue.
  
  Fred is removed from the front of the queue.
  </code></pre>
  
  <p>My output looks like this</p>
  
  <pre><code>Ann is removed from the front of the queue.
  
  Bill is at the front of the queue.
  
  Bill is removed from the front of the queue.
  </code></pre>