Greetings,
I am trying to implement the circular single linked list algorithm to Java and I have done the most of the core part, just wondering if this would be an acceptable way of doing it?
Code:
class SingleLinkedNode{
int value;
SingleLinkedNode next;
SingleLinkedNode(int a){
value=a;
next=null;
}
}
class SingleLinkedUnsortedCircularList{
SingleLinkedNode head;
SingleLinkedNode tail;
SingleLinkedUnsortedCircularList(){
head=null;
tail=null;
}
void addElement(SingleLinkedNode input){
if (head==null){
head=input;
}
if(tail != null){
tail.next=input;
}
tail=input;
}
}
class SingleLinkedUnsortedCircularListTest{
public static void main(String[] args){
SingleLinkedUnsortedCircularList TestList = new SingleLinkedUnsortedCircularList();
SingleLinkedNode a = new SingleLinkedNode(1);
SingleLinkedNode b = new SingleLinkedNode(2);
SingleLinkedNode c = new SingleLinkedNode(3);
SingleLinkedNode d = new SingleLinkedNode(4);
SingleLinkedNode e = new SingleLinkedNode(5);
SingleLinkedNode f = new SingleLinkedNode(6);
TestList.addElement(a);
TestList.addElement(b);
TestList.addElement(c);
System.out.println("After inserting 1, 2, 3 to the testlist:");
System.out.println("head:" + (TestList.head).value );
System.out.println("tail:" + (TestList.tail).value );
TestList.addElement(d);
TestList.addElement(e);
TestList.addElement(f);
System.out.println("After inserting 1, 2, 3, 4, 5, 6 to the testlist:");
System.out.println("head:" + (TestList.head).value );
System.out.println("tail:" + (TestList.tail).value );
}
}
secondly, what exactly is a "sorted" linked list is about? While adding new elements will the adding function also checking the sorting of the list and putting them in a correct order?
Cheers
Bookmarks