Basic Circular Linked List - addFirst() method works improperly
Hello everyone!
I've been working on Linked Lists for about 4 hours now, and I'm stuck on my Circular Linked List's addFirst() method. Basically, my output should be:
Michael
Hannah
Jacob
However, it only prints out the first name: Michael. It appears there is something wrong that happens when I add a second Node (perhaps the Links aren't properly connected?) I believe there is something wrong with my logic in creating the addFirst() method. I have three total classes: Node, CircularLinkedList, and CircularLinkedListTest.
Below is the code for my addFirst() method in my CircularLinkedList class:
Code:
public void addFirst(Object o)
{
if (isEmpty())
{
tail = new Node(o, tail);
}
else // This is the part that's messing up.
{
Node current = new Node(o, tail.getNextNode());
// Creates a reference variable "current" which points
//to a new Node where parameter 'o' is an object, and
//tail.getNextNode() is a pointer (to the next Node object)
tail.setNextNode(current);
// Once the tail has established a link to the "tail"
//node's pointer, the nextNode of the "tail" is pointed
//to the Node that is being/referenced by "current"
}
}
Below is part of my Tester Class (the main() method portion):
Code:
public static void main(String[] args) {
// TODO Auto-generated method stub
CircularLinkedList cll = new CircularLinkedList();
cll.addFirst("Michael");
cll.addFirst("Hannah");
cll.addFirst("Jacob");
System.out.println(cll);
}
All of the three classes that I've written that may help you even further can be found at: carlodm[dot]com/cll/
Thank you for any help that you can give me.
Re: Basic Circular Linked List - addFirst() method works improperly
I have a question with your code, carlo. Is it really necessary to use the "this" keyword on the Node class?
Code:
public class Node {
private Object myValue;
private Node myNextNode;
public Node(Object aValue, Node aNextNode)
{
this.myValue = aValue;
this.myNextNode = aNextNode;
}
public Object getValue()
{
return myValue;
}
public Node getNextNode()
{
return this.myNextNode;
}
public void setValue(Object aValue)
{
this.myValue = aValue;
}
public void setNextNode(Node aNextNode)
{
this.myNextNode = aNextNode;
}
}
What's gonna happen if we don't use "this"? I'm still trying to get my head wrapped around this whole node stuff.
Re: Basic Circular Linked List - addFirst() method works improperly
I'm not the original poster but I can say that in this situation, it is not necessary to use "this".
Re: Basic Circular Linked List - addFirst() method works improperly
I see, thanks. I had a feeling all those "this" keywords are unnecessary. But the way it's written, I think we still have to use it in the constructor, right?
Code:
public Node(Object aValue, Node aNextNode)
{
this.myValue = aValue;
this.myNextNode = aNextNode;
}
Re: Basic Circular Linked List - addFirst() method works improperly
Quote:
Originally Posted by
shyronnie
I see, thanks. I had a feeling all those "this" keywords are unnecessary. But the way it's written, I think we still have to use it in the constructor, right?
Code:
public Node(Object aValue, Node aNextNode)
{
this.myValue = aValue;
this.myNextNode = aNextNode;
}
Try it with and without and you'll see that in this instance, "this" is not needed. Can you figure out and tell us why it's not needed here?
Re: Basic Circular Linked List - addFirst() method works improperly
Quote:
Originally Posted by
Fubarable
Try it with and without and you'll see that in this instance, "this" is not needed. Can you figure out and tell us why it's not needed here?
Ah yes, you're right. I didn't notice that the values passed in the constructor have different names. I was thinking of my own code, and I made it so the values passed on the constructor have the same name as the instance variables. But you're right, "this" is not needed in carlo's constructor.