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: Sorting a doubly linked list

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

    Default Sorting a doubly linked list

    Hey,

    I'm trying to sort a doubly linked list using the "bubbles source" sort.

    It's not working good

    Someone can help me? What's I'm doing wrong?

    		boolean sorted = false;
    		car carA = list.getHead();
    		car carB = list.getHead().getNext();
     
    		while (!sorted) {
     
    			sorted = true;
    			while (carA != getLastcar(list).getPrev()) {
    				sorted = false;
     
    				if (carA.greaterThan(carB, sumEnum)) {
    					swap(list, carA, carB);				
    				}
     
    				car tempcarB = carB;
    				carB = carA.getNext();
    				carA = tempcarB.getNext();
    		}
     
     
    public static void swap(carList list, car carA, car carB) {
     
    	car carBPointed2 = carB.getNext();
    	car carAPreved2 = carA.getPrev();
     
    	carB.setNext(carA);
    	carA.setNext(carBPointed2);
     
    	carB.setPrev(carAPreved2);
    	carA.setPrev(carB);
     
    	carA.getNext().setPrev(carA);
     
    	if (carB.getPrev() == null) {
    		list.setHead(carB);
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Sorting a doubly linked list

    Can you define 'not working good'? What do you put in, and what do you get out? Are there any exceptions? In situations like this it helps to post a short, compilable, and runnable example which demonstrates the problem.

Similar Threads

  1. Help with linked list
    By joecool594 in forum Collections and Generics
    Replies: 3
    Last Post: November 28th, 2010, 12:33 PM
  2. Generic Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 23
    Last Post: October 23rd, 2010, 03:13 PM
  3. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 09:52 PM
  4. [SOLVED] Update on Doubly Linked List
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2010, 07:19 PM
  5. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM