views:

256

answers:

2

Hi stack overflow. I need to insert some objects contained in a Node class into a LinkedList class in a sorted over. The Node class looks like:

public class Node {
    private Card val;
    private Node next;
    public Node(Card v) {
        val = v;
        next = null;
}

where card implements the Comparator interface. I'm trying to write a function to insert these cards in sorted order (low to high) by card value in a LinkedList class (not the java.util.LinkedList class but a linked-list class I'm writing). Does anybody know how I could do this? Also it can't be a doubly-linked list.

+1  A: 

I think what you're looking is something like what I came up below (note I didn't compile, but it should give you an idea and hopefully helps you)

public class Node {
    private Card val;
    private Node next;

    public Node(Card v) {
        val = v;
        next = null;
    }
    public Card getVal() { return val; }
    oublic void setVal(Card v) { val = v; }
    public Node getNext() { return next; }
    public void setNext(Node n) { next = n; }
}

public class MyLinkList {
    private Node head = null;

    public void addNode(Card v) {
        if (head == null) {
            head = new Node(v);
        } else {
            Node  last = null;
            for (Node node = head; node != null && node.getVal().compareTo(v) < 0 ; ) {
                last = node;
                node = node.next;
            }

            Node newNode = new Node(v);
            last.setNext(newNode);
            newNode.setNext(node);
        }
    }
}
Khnle
I would remove the Node#setVal method, it isn't used and may just corrupt the ordering if you use it...
pgras
I added that at the last minute too, thinking just like you. Last minute, I just threw it in there.
Khnle
A: 

i have tried with this code.. its bit messy but can help you..

public static void main(String[] args) {
  Card[] cards = { new Card(10), new Card(5), new Card(34), new Card(2)};
  Node node = new Node(new Card(1));
  Node firstNode = node;
  Node tran = firstNode;
  for (Card c : cards) {
   Node newNode = new Node(c);
   int a=0;
   do {
    if(a==1) {
     tran = tran.getNode();
    }
    if ((tran.getCard().compareTo(c) == -1)) {
     if (tran.getNode() == null) {
      tran.setNode(newNode);
      firstNode = node;
      break;
     } else if (tran.getNode() != null && ran.getNode().getCard().compareTo(c) == +1) {
      newNode.setNode(tran.getNode());
      node.setNode(newNode);
      firstNode = node;
      break;
     }
    } else if ((tran.getCard().compareTo(c) == +1)) {
     newNode.setNode(tran.getNode());
     firstNode = newNode;
     break;
    } else  if(tran.getCard().compareTo(c) == 0){
     newNode.setNode(tran.getNode());
     firstNode = newNode;
     break;
    }
    a=1;
   } while (tran.getNode() != null);
   tran = firstNode;
  }
  Node traversalNode = firstNode;
  while (traversalNode.getNode() != null) {
   System.out.println(traversalNode.getCard());
   traversalNode = traversalNode.getNode();
  }
 }
Mrityunjay