Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting a Linked List?

Posted on 2003-10-24
3
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

604 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