Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help me to convert C++ code to Java, Algorithm QuickSort LinkedList.

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me to convert C++ code to Java, Algorithm QuickSort LinkedList.

    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 );
    	}  
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Please help me to convert C++ code to Java, Algorithm QuickSort LinkedList.

    it return wrong results
    Can you post the command prompt contents that shows what happened?

    Add some comments describing what is wrong with the output.

    How are you trying to debug the program? Add some print statements that print out the values of the variables as they are used so you can see what the code is doing. If you understand the algorithm the printout will help you find the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Understanding the mechanics of swapping variables for the quicksort algorithm
    By SamJava_the_Hut in forum Algorithms & Recursion
    Replies: 7
    Last Post: May 30th, 2019, 12:37 PM
  2. Understanding the mechanics of swapping variables for the quicksort algorithm
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 30th, 2019, 12:37 PM
  3. please i need help to convert fecn algorithm pseudocode to java code
    By shalini raj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 26th, 2014, 02:05 PM
  4. Quicksort algorithm displaying strange behaviour
    By Mathew.Williams in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2011, 12:56 PM
  5. Convert C/C++ code to Java
    By cristianll in forum What's Wrong With My Code?
    Replies: 13
    Last Post: November 14th, 2009, 02:43 AM