How would I make my custom generic type linked list in Java sorted?

China☆狼群 提交于 2019-12-02 07:21:26

Does your implementation extend the java.util.List interface?

Can you simply add the object to the list, then sort the list using Collections.sort()?

Farmer Joe

You mention using generics but then mention sorting them alphabetically. Generics are not necessarily character strings, they are used to represent any type, while a sort property like alphabetically implies alphabetic characters. My Answer assumes you are expecting generic objects of type T which have an alphabetic nature to them. In my example I exclusively use a String

You can set you code up to search for the position to add itself instead of providing it.

public void add(T item) {
    Node<T> addThis = new Node<T>(item);
    Node<T> itr = head;

    while (itr.hasNext()) {
        if (addThis.compareTo(itr.getNext()) <= 0) { // itr > addThis
            addThis.setNext(itr.getNext());
            itr.setNext(addThis);
            return;
        }
        itr = itr.getNext();
    }
    addThis.setNext(null);
    itr.setNext(addThis);
    return;
} // end add

Then in your Node class, you can implement the Interface Comparable . I'm assuming you store a string since you asked about alphabetizing. This Question Explains comparing strings alphabetically.

class Node implements Comparable<Node> {


    String value;  // ASSUMING YOU ARE USING A STRING AS YOUR GENERIC TYPE T

    @Override
    public int compareTo(Node otherNode) {
        int i;
        String thisString = this.getValue();
        String otherString = otherNode.getValue();
        int minSize = ( otherString.length() > thisString.length() ? thisString.length() : otherString.length() );
        for (i = 0; i < minSize; i++) {
             if (thisString.charAt(i) > otherString.charAt(i)) {
                 return 1;
             } else if (thisString.charAt(i) < otherString.charAt(i)) {
                 return -1;
             }
        }
        if (otherString.length() > thisString.length()) {
            return 1;
        } else if (otherString.length() < thisString.length()) {
            return -1;
        } else {
            return 0;
        }
    }

    // OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
}

In order to do this with simply generics, you would need to implement you Node class with the type T implementing Comparable like so:

class NodeNode<T extends Comparable<T>> implements Comparable {


    T value;

    @Override
    public int compareTo(Node otherNode) {
        return this.getValue().compareTo(otherNode.getValue());
    }

    // OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
}

I finally figured it out by using an insertion sort:

public void add(Dvd item) {
  DvdNode addThis = new DvdNode(item);
  if(head == null) {
    head = addThis;
  } else if(item.getTitle().compareToIgnoreCase(head.getItem().getTitle()) < 0) {
      addThis.setNext(head);
      head = addThis;
    } else {
        DvdNode temp;
        DvdNode prev;
        temp = head.getNext();
        prev = head;
        while(prev.getNext() != null && item.getTitle().compareToIgnoreCase
            (prev.getNext().getItem().getTitle()) > 0) {
          prev = temp;
          temp = temp.getNext();
        }
        addThis.setNext(temp);
        prev.setNext(addThis);
      }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!