Results 1 to 3 of 3
  1. #1
    somewierdguy is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Trouble with circular singly linked queue

    I'm in the process of working on a lab and have hit a wall and can't quite figure out what's going on with this. The requirements are that I have a circular queue with one external pointer to track the rear of it. It seems to enque fine, however I keep getting null pointer exceptions when I deque and I'm not sure what I'm doing wrong. If anyone could help I'd really appreciate it.


    Things fall apart with it in the dequeue method when it checks if it's empty since apparnetly it's not traversing properly and keeps hitting null.



    public class CircLinkedUnbndQueue implements UnboundedQueueInterface
    {
    protected LLObjectNode rear; // reference to the rear of this queue

    public CircLinkedUnbndQueue()
    {

    rear = null;
    }

    public void enqueue(Object element)
    // Adds element to the rear of this queue.
    {
    LLObjectNode newNode = new LLObjectNode(element);
    if (rear == null)
    rear = newNode;

    else
    rear.setLink(newNode);

    rear = newNode;

    }

    public Object dequeue()
    // Throws QueueUnderflowException if this queue is empty;
    // otherwise, removes front element from this queue and returns it.
    {
    //Error seems to point to this area//
    if (isEmpty())
    throw new QueueUnderflowException("Dequeue attempted on empty queue.");
    else
    {
    Object element;
    element = rear.getInfo();
    rear = rear.getLink();

    if (element == null)
    rear = null;

    return element;
    }
    }

    public boolean isEmpty()
    // Returns true if this queue is empty; otherwise, returns false.
    {
    if (rear == null)
    return true;
    else
    return false;
    }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    if the linked list is to be cyclic, then when you have only one node in the list, you need to have it's next pointer, point back to itself right. ?

  3. #3
    somewierdguy is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    yeah, I assumed I had that already coded, since before the dequeue operation it seemed to be spitting out whatever i put in?

Similar Threads

  1. Replies: 9
    Last Post: 11-04-2011, 03:09 AM
  2. Problem prioritizing a circular queue
    By Metastar in forum New To Java
    Replies: 1
    Last Post: 10-03-2010, 11:40 PM
  3. Implementing a singly linked list
    By Onra in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 09:19 PM
  4. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 05:10 PM
  5. Trouble Developing Singly Linked Circular List
    By VinceGuad in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •