Cirlular Linked Queue
This is my assignment, and I hope someone can point me in the right direction. I have searched and tried several things, and nothing seems to get it to add to the queue. Maybe I am looking at it incorrectly. Here it is;
1.Implement the ADT queue by using a circular linked chain, as shown on Page# 525 Figure 23-11. Recall that this chain has only an external reference to its last node. Complete the following methods in the LinkedQueueCircular class. Look for the comment ADD CODE HERE TO COMPLETE THIS METHOD in the methods.
a.enqueue method: Adds a new entry to the back of the queue. You may get hint from the implementation of enqueue method in LinkedQueue class.
Here is what I have so far:
publicvoid enqueue(Object newEntry)
{
Node newNode =new Node(newEntry,null);
// ADD CODE HERE TO COMPLETE THIS METHOD
if (isEmpty())
lastNode = newNode;
else
lastNode.setNextNode(newNode);
lastNode = newNode;
}// end enqueue
I am not that great at this, so please help me.
Thanks,
rp
> > You may get hint from the implementation of enqueue
> method in
> > LinkedQueue class.
>
> Did you read that?
>
> And note that you didn't even explain your problem.
> If the part you don't understand is "all of it", I
> would very strongly suggest to go and talk to a peer
> or your teacher.
Yes I read that, and that is where I got what I have from, but it doesn't work. I think my problem is that I have four methods to work on, and so I work on one, and I see something else wrong with another, and anyway, it has gotten me confused. I have spoken with my teacher and a peer, and that hasn't helped. I'm not sure if anyone on this list will help, but I am stumped. Maybe someone can ask me a question that will point me in the right direction. Hopefully, that will help.
Thanks,
I have my answer. Thanks for your help. It is below.
class LinkedQueueCircular implements QueueInterface, java.io.Serializable
{
private Node lastNode; // references node for back of queue
public LinkedQueueCircular()
{
lastNode = null;
} // end default constructor
public void enqueue(Object newEntry)
{
Node newNode = new Node(newEntry, null);
// ADD CODE HERE TO COMPLETE THIS METHOD
if (isEmpty()){
{
lastNode = newNode;
lastNode.setNextNode(lastNode);
}
}
else
//newNode.setNextNode(lastNode);{
newNode.setNextNode(lastNode.getNextNode());
lastNode.setNextNode(newNode);
lastNode = newNode;
} // end enqueue
public Object dequeue()
{
Object front = null;
// ADD CODE HERE TO COMPLETE THIS METHOD
if (!isEmpty())
{
front = lastNode.getNextNode().getData();
lastNode.getNextNode().setData(null);
lastNode.setNextNode(lastNode.getNextNode().getNextNode());
if (lastNode == null)
lastNode = null;
} // end if
return front;
} // end dequeue
public Object getFront()
{
Object front = null;
// ADD CODE HERE TO COMPLETE THIS METHOD
if (!isEmpty())
front = lastNode.getNextNode().getData();
return front;
} // end getFront
public boolean isEmpty()
{
return lastNode == null;
} // end isEmpty
public void clear()
{
// ADD CODE HERE TO COMPLETE THIS METHOD
lastNode = null;
} // end clear
private class Node
{
private Object data; // data portion
private Node next; // next to next node
private Node(Object dataPortion)// PRIVATE or PUBLIC is OK
{
data = dataPortion;
next = null;
} // end constructor
private Node(Object dataPortion, Node nextNode)// PRIVATE or PUBLIC is OK
{
data = dataPortion;
next = nextNode;
} // end constructor
private Object getData()
{
return data;
} // end getData
private void setData(Object newData)
{
data = newData;
} // end setData
private Node getNextNode()
{
return next;
} // end getNextNode
private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
} // end LinkedQueueCircular