Results 1 to 3 of 3
- 12-05-2010, 11:31 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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;
}
}
- 12-06-2010, 02:45 AM #2
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. ?
- 12-06-2010, 02:48 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Basic Circular Linked List - addFirst() method works improperly
By carlodelmundo in forum New To JavaReplies: 9Last Post: 11-04-2011, 04:09 AM -
Problem prioritizing a circular queue
By Metastar in forum New To JavaReplies: 1Last Post: 10-04-2010, 12:40 AM -
Implementing a singly linked list
By Onra in forum New To JavaReplies: 2Last Post: 04-12-2010, 10:19 PM -
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 06:10 PM -
Trouble Developing Singly Linked Circular List
By VinceGuad in forum New To JavaReplies: 14Last Post: 02-25-2009, 05:38 PM
Bookmarks