Re: Ordering Nodes within a Linked List......need help
From: Martin (mbierwirth_at_gmx.de)
Date: 03/17/04
- Next message: Arnaud Denagbe: "Re: JPEG"
- Previous message: Sudsy: "Re: JNI"
- In reply to: Newbie: "Ordering Nodes within a Linked List......need help"
- Next in thread: Ken Campbell: "Re: Ordering Nodes within a Linked List......need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Arnaud Denagbe: "Re: JPEG"
- Previous message: Sudsy: "Re: JNI"
- In reply to: Newbie: "Ordering Nodes within a Linked List......need help"
- Next in thread: Ken Campbell: "Re: Ordering Nodes within a Linked List......need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|