Question

public class Ch3Ex2 {
static class Node {
    Node(Object value) {
        this.value = value;
    }
    Object value; // Comparator for this field required
    Node next;
}


static class Stack {
    Node Top;
    Node Min;
    void push(Object value) {
        Node node = new Node(value);
        if(Top == null) {
            Min = node;
        } else {
            Min = (Min.value < node.value) ? Min : node;  // Comparator Needed to be used here
        }
        node.next = Top;
        Top = node; 
    }
Was it helpful?

Solution 2

I think you want to declare your Node class like:

class Node<T extends Comparable<T>> {
    T value;
    Node next;
    Node(T value) {
        this.value = value;
    }
}

Then your Stack would look like:

class Stack<T extends Comparable<T> {
    Node<T> Top;
    Node<T> Min;
    void push(T value) {
        Node<T> node = new Node<T>(value);
        if(Top == null) {
            Min = node;
        } else {
            Min = Min.value.compareTo(node.value) < 0 ? Min : node;
        }
        node.next = Top;
        Top = node; 
    }
}

UPDATE

I think what you are actually asking for is something like:

class Node<T> {
    T value;
    Node next;
    Node(T value) {
        this.value = value;
    }
}

class Stack<T> {
    Node<T> Top;
    Node<T> Min;
    final Comparator<T> valueComparer;
    public Stack(Comparator<T> valueComparer) { this.valueComparer = valueComparer; }
    void push(T value) {
        Node<T> node = new Node<T>(value);
        if(Top == null) {
            Min = node;
        } else {
            Min = valueComparer.compare(Min.value, node.value) < 0 ? Min : node;
        }
        node.next = Top;
        Top = node; 
    }
}

This means your value class does not need to implement Comparable, as long as the class constructing the Stack object knows how to compare the values by passing in a Comparator.

OTHER TIPS

I am not sure but try this,

Have the class implement the Comparable interface, which gives the compareTo method. You can then use the value of the number (-1 for less, 1 for more, 0 for equals) in your if statements.

If you want to put these objects in lists (say, for sorting) you should also @Override the .equals method.

For example :

import java.util.Comparable;

public class BlockOffset implements Comparable<BlockOffset>
{
   private int blockNumber;
   private int offset;

   @Override
   public int compareTo(BlockOffset instance2) {
   if (this.blockNumber < instance2.blockNumber) return -1;
   if (this.blockNumber > instance2.blockNumber) return 1;
   if (this.offset < instance2.offset) return -1;
   if (this.offset > instance2.offset) return 1;

   return 0;
 }   
}

For more details have a look at this link.

The only logical way i could see is to implement Comparable for my Node class. Implementing a comparing protocol for only 'value' instance member seems illogical.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top