Solved

Sorting a Linked List?

Posted on 2003-10-24
3
377 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
ID: 9614602
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
ID: 9627911
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
ID: 10215383
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
network + 7 80
Understanding websocket example in spring 1 57
@SBGen Method 3 38
eclipse compiler vs Installed JREs option 3 77
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

920 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