# Singly Circular Linked List Error

• March 5th, 2012, 03:45 PM
clydefrog
Hi,

I'm trying to make a singly circular linked list, however i happen to be getting nullpoiterexception in my code. I've never worked with circular linked list, so im not to certain if my approach is even correct. I have a feeling its not a big error and it can be remedied quickly, but i just cant see the solution.

I think i know what the problem is, but i cant figure out a way to solve it:

-basically at line 26 (i've marked it with comments), i have a while loop that should iterate through the linked list and stop just before the 'head' (remember this is a circular list, so the last node points back to the first which is the 'head')
-originally the head is 'null' so it adds the first node, and sets the current node to point to head
-when it attempts to add the second node, im guessing while loop flips out, because it says "current.getNext() != head" but current is equal to head...

so yeah, thats about as far as my debugging has gone. But i know it has something to do with the fact that its circular. because it works as a linear linked list. if you want to try it out as a linear linked list, i've included a few comments to tell you which lines to modify; there are 3 in total.

I would be very grateful if someone could take a look and help me out :)

Code :

package project3;

class Hex
{

Hex()
{
}

public void insert(int n)
{
Node temp = new Node(n);

//if the list is empty, then the new node equal to head
{
System.out.println("hit");
}
else
{
//Starting from head, move through the list until you get to head again
while(current.getNext() != head) //<------- ***Error***  (if want linear, change this line to, while(current.getNext() != null)
{
current = current.getNext();
}
//set the last node's pointer to the new node
current.setNext(temp);
//set the pointer of the last node back to the first node
temp.next = head; //if you want linear, delete this line
}
}

//node class
class Node
{
int element;
Node next;

Node()
{

}

Node(int element)
{
this.element = element;
next = null;
}

Node(int element, Node next)
{
this.element = element;
this.next = next;
}

public int getElement()
{
return element;
}

public void setElement(int element)
{
this.element = element;
}

public Node getNext()
{
return next;
}

public void setNext(Node next)
{
this.next = next;
}
}

public String toString()
{
String output = "";
while(current != head) //if want this linear, change this line to while(current.getNext() != null)
{
output += "[" + current.getElement() + "]";
current = current.getNext();
}
return output;

}

}

public class Project3 {
public static void main(String[] args)
{
Hex h1 = new Hex();

h1.insert(1);
h1.insert(2);
h1.insert(3);
h1.insert(4);
h1.insert(5);
h1.insert(6);

System.out.println(h1);
}

}

Error:

Code :

at project3.Hex.insert(Project3.java:26)
at project3.Project3.main(Project3.java:102)

• March 5th, 2012, 04:05 PM
Sean4u
Re: Singly Circular Linked List Error
When you create the head with insert(), to what node should its next reference refer?
• March 5th, 2012, 04:10 PM
clydefrog
Re: Singly Circular Linked List Error
Thank you for the prompt reply!

hmmm...

so you mean somethng like this?

Code :

{
System.out.println("hit");

}

if i put that. the error goes away, but it doesnt add anything aside from the first entry i think.
• March 5th, 2012, 04:17 PM
copeg
Re: Singly Circular Linked List Error
Step through your code and write out on paper what is happening. On first insert you create a node:

Node {next = null}

When you loop through upon second insertion, you end up setting current to the next value, which is null
• March 5th, 2012, 04:35 PM
clydefrog
Re: Singly Circular Linked List Error
well i tried tracing it out on paper, but i'm not sure i did it correctly. From what i see on my paper, is the issue here the fact that the first node is constantly pointing to itself?

Not sure how to explain it but:

- So we make our first node, and its pointer is null.
- i make that node my head, give it a value of 1, and set its pointer to point to itself
- when i try to add another node, it never hits the while loop because the current is already pointing the head.

Is this correct? How can i remedy this?
• March 5th, 2012, 04:39 PM
Sean4u
Re: Singly Circular Linked List Error
Quote:

Originally Posted by clydefrog
How can i remedy this?

Your code might be working. Your toString() method won't: you set current = head and then while(current != head). Consider changing that so that the loop executes at least once for the head node if it exists
• March 5th, 2012, 05:00 PM
copeg
Re: Singly Circular Linked List Error
Quote:

is the issue here the fact that the first node is constantly pointing to itself?
I see nowhere in your original code in which you define the first node pointing to itself. See post #3
• March 5th, 2012, 07:17 PM
clydefrog
Re: Singly Circular Linked List Error
Quote:

Originally Posted by Sean4u
Your code might be working. Your toString() method won't: you set current = head and then while(current != head). Consider changing that so that the loop executes at least once for the head node if it exists

Sorry i didnt reply sooner, i had to leave the house for a few hours.

wtf? Yeah it was the toString() method, it appears to be working correctly now. lol thank you so much.

Now just another quick question: From what i can tell, it appears as though my circular linked list is implemented correctly. How can test to see if its actually circular?