Skip to content

Instantly share code, notes, and snippets.

@zac-xin
Created November 26, 2012 03:02
Show Gist options
  • Save zac-xin/4146391 to your computer and use it in GitHub Desktop.
Save zac-xin/4146391 to your computer and use it in GitHub Desktop.
InsertCyclicSortedList
package Chapter2;
public class InsertCyclicSortedList {
public static void main(String args[]){
IntNode n1, n2, n3, n4, n5, n6,n7, n8, n9;
n9 = new IntNode(27, null);
n8 = new IntNode(16, n9);
n7 = new IntNode(14, n8);
n6 = new IntNode(12, n7);
n5 = new IntNode(8, n6);
n4 = new IntNode(6, n5);
n3 = new IntNode(4, n4);
n2 = new IntNode(3, n3);
n1= new IntNode(1, n2);
n9.link = n1;
IntNode m = n1;
for(int i = 0; i < 9 ; i++){
System.out.print(m.data+" ");
m = m.link;
}
System.out.println();
insert(n1, 3);
m = n1;
for(int i = 0; i < 10 ; i++){
System.out.print(m.data+" ");
m = m.link;
}
}
public static IntNode insert(IntNode start, int data){
IntNode p = start;
do{
if(data >= p.data && data < p.link.data)
break;
if(p.link.data < p.data &&(data <= p.link.data || data >= p.data))
break;
p = p.link;
}while(p != start);
IntNode n = new IntNode(data, null);
n.link = p.link;
p.link = n;
return start;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment