/* LinkedList MergeSort */
package layout;
import java.util.Scanner;
/* Class Node */
class Node {
protected int data;
protected Node next, prev;
/* Constructor */
public Node() {
data = 0;
next = null;
}
/* Constructor */
public Node( int d, Node n ) {
data = d;
next = n;
}
/* Function to set link to next node */
public void setLinkNext( Node n ) {
next = n;
}
/* Funtion to get link to next node */
public Node getLinkNext() {
return next;
}
/* Function to set data to node */
public void setData( int d ) {
data = d;
}
/* Function to get data from node */
public int getData() {
return data;
}
}
/* Class linkedList */
class linkedList {
protected Node start, end;
public int size;
/* Constructor */
public linkedList() {
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty() {
return start == null;
}
/* Function to get size of list */
public int getSize() {
return size;
}
public Node getNodeStart() {
return start;
}
public Node getNodeEnd() {
return end;
}
/* Function to insert element */
public void insertHead( int val ) {
Node p = new Node( val, null );
if ( start == null ) {
start = p;
end = start;
}
else {
p.setLinkNext( start );
start = p;
}
size++;
}
void insertTail( int val ) {
Node p = new Node( val, null );;
if ( start == null ) {
start = p;
end = start;
}
else {
end.setLinkNext( p );
end = p;
}
p.setLinkNext( null );
size++;
}
void Output() {
Node p = start;
while ( p != null ) {
System.out.print( p.getData() + " <-> " );
p = p.getLinkNext();
}
}
/* Function to convert List to a String */
public String display() {
String ListString01 = "";
Node ptr = start;
ListString01 = ListString01 + ptr.getData() + ", ";
if ( size == 0 ) {
ListString01 = "List is empty";
return ListString01;
}
ptr = start.getLinkNext();
while ( ptr.getLinkNext() != null ) {
ListString01 = ListString01 + ptr.getData() + ", ";
ptr = ptr.getLinkNext();
}
return ListString01;
}
void initList( linkedList List ) {
List.start = List.end = null;
}
/* Function to MergeSort */
void appendList( linkedList List, linkedList List1 ) {
if ( List.start == null )
List = List1;
else {
List.end.setLinkNext( List1.start );
if ( List1.start != null )
List.end = List1.end;
}
}
int getDigit( int n, int t ) {
long temp = 1;
for ( int i = 0; i < n; i++ )
temp = temp * 10;
return (int) ( ( t / temp ) % 10 );
}
int countDigit( int n ) {
int count = 0;
while ( n > 0 ) {
n = n / 10;
count++;
}
return count;
}
int findMax( linkedList list ) {
Node p;
p = new Node();
p = list.start;
int max = p.data;
while ( p != null ) {
if ( max < p.data )
max = p.data;
p = p.getLinkNext();
}
return max;
}
int getMaxDigit( linkedList list ) {
int max = findMax( list );
return countDigit( max );
}
void sendToBox( linkedList list, int n, linkedList temp[] ) {
Node p;
p = new Node();
p = list.start;
while ( p != null ) {
temp[ getDigit( n, p.data ) ].insertTail( p.data );
p = p.getLinkNext();
}
}
void radixSort( linkedList list ) {
linkedList[] temp = null;
int i;
if ( list.start == list.end )
return;
for ( i = 0; i < 10; i++ )
temp[ i ] = new linkedList();
int num = getMaxDigit( list );
for ( i = 0; i < num; i++ ) {
sendToBox( list, i, temp );
for ( int j = 0; j < 10; j++ ) {
appendList( list, temp[ j ] );
initList( temp[ j ] );
}
}
}
@SuppressWarnings( "deprecation")
public static void main( String arg[] ) {
linkedList List01 = new linkedList();
int k = 10;
int a[] = { 12, 23, 34, 78, 91, 44, 52, 68, 79, 77 };
for ( int i = 0; i < k; i++ ) {
List01.insertHead( a[ i ] );
}
System.out.println( "oOo m01.Output(); oOo" );
List01.Output();
System.out.println( " " );
System.out.println( "oOo m01.display( ); oOo" );
String s01 = List01.display();
System.out.println( s01 );
List01.radixSort( List01 );
System.out.println( "oOo m01.display( ); oOo" );
String s02 = List01.display();
System.out.println( s02 );
}
}