Re: Ordering Nodes within a Linked List......need help

From: Martin (mbierwirth_at_gmx.de)
Date: 03/17/04


Date: 17 Mar 2004 00:39:28 -0800

You say you want to compare the key (which you should, of course), but
then your method for sorting is working on objects.

What I would do:
Store an instance-variable 'key' for each node as well as the
getter-method for it. Then put a compare-method in the node-class that
has argument 'int key' and which compares its own key (that is, the
instance-variable of this node) with the int-argument (which will be
called with getKey() of other nodes).

Hope this help,
Martin

each
"Newbie" <newbie@hotmail.com> wrote in message news:<c3865k$uvk$1@news.netins.net>...
> Subject says it all... I've to sort into order a list (using a linked list).
>
> I'm trying to do this completely generically so that any object with a
> key(int) internal to the object, can be put onto the list and ordered
> appropriately.
>
> The problem I'm having is the compareT0 function.
>
> How can I compare the (key) of the incoming object to the (key) of an object
> within a node?
>
> The is driving me insane... please assist me if you can and have the time.
>
> Thanks
>
> >>>>>>>>>>>>>>>>>CODE<<<<<<<<<<<<<<<<<<<<
> public class SList
> {
> protected Node header;
> protected Node previous;
> protected Node current;
>
> public SortList() //initialize nodes
> {
> header = null;
> previous = null;
> current = null;
> }
>
> public int compare(Object x)
> {
> int val;
>
> val = x.compareTo ( obj );
> }
>
> //node class
> protected class Node
> {
> protected Object obj;
> protected Node next;
>
> public Node(Object obj) {
> this.obj = obj;
> next = null;
> }
>
> }//end node class
> }//end SList class



Relevant Pages

  • Re: fastest sorted list type?
    ... I'm not aware of any scenario you've mentioned in which what you're sorting is "just an int". ... Yes, you have a scenario in which you are sorting a list of ints, but when you actually compare two entries in that list you aren't comparing the integers themselves, but rather the data in an object to which the int refers. ... I did have a look on google and wiki lists al the sort algorothms nicely, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sorting algorithm when comparison is heavy
    ... asking about sorting things with very expensive compare operations. ... single digit of the long value. ... sorted pile after handling the most significant digit. ...
    (comp.programming)
  • Re: calling qsort
    ... int mycmp(const void *a, const void *b) ... the compare function. ...
    (comp.lang.c)
  • Re: Sorting algorithm when comparison is heavy
    ... asking about sorting things with very expensive compare operations. ... so speed things up by using your method to sort them in, ... single digit of the long value. ...
    (comp.programming)
  • Re: Is the following code OK?
    ... I have to compare an address of structure with an absolute address. ... int main ... First, as you suspect, it is not possible to compare a pointer ... integer to a pointer by writing (struct t*)0x120000, ...
    (comp.lang.c)