?
Solved

Sorting a Linked List?

Posted on 2003-10-24
3
Medium Priority
?
394 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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 …
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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