Solved

Sorting a Linked List?

Posted on 2003-10-24
3
374 Views
Last Modified: 2010-03-31
Here is the "driver" code:

public class L1014d {

      public static void main (String args[]) {
            // construct a view
            L1014v v = new L1014v();
            L1014m m = new L1014m(v);
            v.printList();            
      } // main method

}

Here is the "view" code:

public class L1014v {

   public L1014n br; // base address of list
   
   public void printList() {
           L1014n ln;
           ln = br; // sets local node to base;
           if (ln != null) {
              do {
                     System.out.println("Node is currently: " + ln.value);
                     ln = ln.nodeRef;
              } while (ln != null);
           } else {
                  System.out.println("No value in the node!");
           }
   }
   
   
   public void setRef(L1014n lr) {
           br = lr; // set base address of s1
   }

}

Here is the "model" code:

public class L1014m {

   L1014n baseRef;
   L1014n holdRef;
   L1014v viewRef;
   
   public L1014m(L1014v lf) {
           L1014n baseRef = new L1014n();
           L1014n holdRef = baseRef;
           L1014v viewRef = lf;
           for (int i = 0; i < 1000; i++) {
                  holdRef.nodeRef = new L1014n();
                  holdRef = holdRef.nodeRef;
           }
           lf.setRef(baseRef);
   }

}

and, here is the "node" code:

public class L1014n {

   L1014n nodeRef;
   int value = (int) (Math.random()*10000);

}

My question is, how do I get the linked-list and sort on the value in the L1014n class, but not by swapping the L1014n's value but just the L1014n nodeRef?

Help?
0
Comment
Question by:cms_it_tlv
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
Comment Utility
In this case it would probably actually be easier to just swap the values. However, if you want to swap the nodes themselves, you need to manage the linkages. Suppose you're doing something like a bubblesort, and find you need to swap the two elements a and b (which are both L1014n objects). You would also need to have retained a reference to the node that "points" to a (call it toa). You would then go about swapping them as follows:

L1014n tgt=b.nodeRef;
toa.nodeRef=b;
b.nodeRef=a;
a.nodeRef=tgt;

Now toa points to b, b points to a, and a points to the node that came after b; thus the two elements are swapped.

0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
Did that help?

If so, you should grade the answer.

If not, perhaps a clarifying question would help.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept imladris's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

objects
EE Cleanup Volunteer
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now