babacan
January 3rd, 2009, 05:16 PM
Greetings
I am trying to implement a sorted insertion function to circular singly linked list at java but I am not able to acheive it, any help will be appreciated.
void addElement(SingleNode input) {
// if head is null
if(head == null){
head = input;
head.next = head;
return;
}
SingleNode current = head;
SingleNode previous = null;
// if not null
do{
if ( input.value <current.value ){
if(previous == null){
input.next = head;
current.next= input;
head = input;
return;
}
else{
previous.next = input;
input.next = current;
return;
}
}
if(current.next == head){
current.next = input;
input.next = head;
return;
}
previous = current;
current = current.next;
}
while(current != head);
}
I am trying to implement a sorted insertion function to circular singly linked list at java but I am not able to acheive it, any help will be appreciated.
void addElement(SingleNode input) {
// if head is null
if(head == null){
head = input;
head.next = head;
return;
}
SingleNode current = head;
SingleNode previous = null;
// if not null
do{
if ( input.value <current.value ){
if(previous == null){
input.next = head;
current.next= input;
head = input;
return;
}
else{
previous.next = input;
input.next = current;
return;
}
}
if(current.next == head){
current.next = input;
input.next = head;
return;
}
previous = current;
current = current.next;
}
while(current != head);
}