Please help me to convert C++ code to Java, Algorithm QuickSort LinkedList.
This is the C++ Code which was tested
/* QuickSort Double LinkedList with Class 02 */
#include <iostream> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> using namespace std; class LinkedList { protected: class Node { public: int Data; Node *Next; Node *Prev; Node() { Data = 0; Next = NULL; Prev = NULL; }; ~Node() { if ( Next != NULL ) delete Next; }; }; protected: Node *Head; Node *Tail; public: LinkedList(); ~LinkedList(); void Output(); void initList(); void addList( int &x ); void addList( int *x, int &n ); void quickSort( Node *Left, Node * Right ); void quickSort(); }; //----------------------------------------------------- LinkedList::LinkedList() { Head = Tail = NULL; } LinkedList::~LinkedList() { if ( Head != NULL ) delete Head; } void LinkedList::Output() { Node *p = Head; int i = 0; while ( p != NULL ) { printf( "x[%d] = %d \n", i, p->Data ); p = p->Next; i++; } } void Random( int *x, int &n ) { srand( time( 0 ) ); for ( int i = 0; i < n; i++ ) x[ i ] = rand() % 500; } void LinkedList::addList( int &x ) { Node *p = new Node; p->Data = x; if ( Head == NULL ) { Head = Tail = p; p->Next = p->Prev = NULL; } else { Tail->Next = p; /* Insert new Node p next after the old Node Tail */ /* Head,... Tail, Node p*/ p->Prev = Tail; Tail = p; /* New Node p will become new Tail of LinkedList */ /* Head,... Tail = Node p, NULL*/ p->Next = NULL; } } void LinkedList::addList( int *x, int &n ) { for ( int i = 0; i < n; i++ ) addList( x[ i ] ); } //----------------------------------------------------- void LinkedList::quickSort( Node *Left, Node *Right ) { Node *Start, *Current; int n1; /* n1 is a temp variable used to store integer data from Node in swap function */ if ( Left == Right ) return; Start = Left; Current = Start->Next; while ( 1 ) { if ( Start->Data < Current->Data ) { // Swap the items n1 = Current->Data; Current->Data = Start->Data; Start->Data = n1; } if ( Current == Right ) break; Current = Current->Next; } /* Swap data between Node Left and Node Current */ n1 = Left->Data; Left->Data = Current->Data; Current->Data = n1; /* Node Current = Node Left */ Node *OldCurrent = Current; /* Node OldCurrent is used to save the previous data of Node Current which will be changed in the recursion algorithm */ Current = Current->Prev; /* Moving Node Current into the Left */ if ( Current != NULL ) { if ( ( Left->Prev != Current ) && ( Current->Next != Left ) ) quickSort( Left, Current ); /* The Recursion Loop will stop if it reach Node Left is right after Node Current (... Current, Node Left,...) */ } Current = OldCurrent; Current = Current->Next; /* Moving Node Current into the Right */ if ( Current != NULL ) { if ( ( Current->Prev != Right ) && ( Right->Next != Current ) ) quickSort( Current, Right ); /* The Recursion Loop will stop if it reach Node Current is right after Node Right (... Node Right, Current...) */ } } void LinkedList::quickSort() { quickSort( Head, Tail ); } //----------------------------------------------------- int main() { int n = 14; LinkedList list; int x[] = { 12, 23, 34, 78, 91, 44, 52, 68, 79, 77, 33, 37, 99, 109 }; list.addList( x, n ); list.Output(); list.quickSort(); printf( "List after sorting: \n" ); list.Output(); getch(); return 0; }[COLOR="Silver"]
--- Update ---
but when I convert this code into Java, it return wrong results
And how to put code into Java code in this forum?
/* Double 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; prev = 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; } public void setLinkPrev( Node n ) { next = n; } /* Funtion to get link to next node */ public Node getLinkNext() { return next; } public Node getLinkPrev() { return prev; } /* 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 addList ( int value ) { Node p = new Node( ); // Set Integer value p.setData( value ); // Add to list. If this is the first item added if ( start == null ) { start = end = p; p.setLinkNext( null ); p.setLinkPrev( null ); } else { // Add item to the end of the list end.setLinkNext( p ); p.setLinkPrev( end ); end = p; p.setLinkNext( null ); } size++; } void addList( int value[ ], int n ) { for ( int i = 0; i < n; i++ ) addList( value[ i ] ); } 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 quickSort( Node Left, Node Right ) { Node Start01, Current; int n1; /* n1 is a temp variable used to store integer data from Node in swap function */ if ( Left == Right ) return; Start01 = Left; Current = Start01.getLinkNext( ); while ( true ) { if ( Start01.getData( ) < Current.getData( ) ) { // Swap the items n1 = Current.getData( ); Current.setData( Start01.getData( ) ); Start01.setData( n1 ); } if ( Current == Right ) break; Current = Current.getLinkNext( );; } /* Swap data between Node Left and Node Current */ n1 = Left.getData( ); Left.setData( Current.getData( ) ); Current.setData( n1 ); /* Node Current = Node Left */ Node OldCurrent = Current; /* Node OldCurrent is used to save the previous data of Node Current which will be changed in the recursion algorithm */ Current = Current.getLinkPrev( );; /* Moving Node Current into the Left */ if ( Current != null ) { if ( ( Left.getLinkPrev( ) != Current ) && ( Current.getLinkNext( ) != Left ) ) quickSort( Left, Current ); /* The Recursion Loop will stop if it reach Node Left is right after Node Current (... Current, Node Left,...) */ } Current = OldCurrent; Current = Current.getLinkNext( ); /* Moving Node Current into the Right */ if ( Current != null ) { if ( ( Current.getLinkPrev( ) != Right ) && ( Right.getLinkNext( ) != Current ) ) quickSort( Current, Right ); /* The Recursion Loop will stop if it reach Node Current is right after Node Right (... Node Right, Current...) */ } } void quickSort( linkedList List02 ) { quickSort( List02.start, List02.end ); } @SuppressWarnings("deprecation") public static void main ( String arg[] ) { linkedList List01 = new linkedList( ); int k = 14; int a[] = { 12, 23, 34, 78, 91, 44, 52, 68, 79, 77, 33, 37, 99, 109 }; for ( int i = 0; i < k; i++ ) { List01.addList( 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.quickSort( List01 ); System.out.println( "oOo m01.display( ); oOo" ); String s02 = List01.display( ); System.out.println( s02 ); } }