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

纵然是瞬间 提交于 2019-12-20 04:11:06

问题


I am writing my own linked list in java that is of generic type instead of using the java collections linked list. The add method for the linked list is made up of the following code:

public void add(T item, int position) {
  Node<T> addThis = new Node<T>(item);
  Node<T> prev = head;
  int i;

  if(position <= 0) {
    System.out.println("Error: Cannot add element before position 1.");
  }

  else if(position == 1) {
    addThis.setNext(head);
    head = addThis;
  } else {
    for(i = 1; i < position-1; i++) {
      prev = prev.getNext();
      if(prev == null) {
        System.out.println("Cannot add beyond end of list");
      }
    } // end for
    addThis.setNext(prev.getNext());
    prev.setNext(addThis);
  }
} // end add

How would I make it so that when I add a new item, the item is compared to another item and is inserted alphabetically? I have looked into using compareTo but I cannot figure out how to do it.

Thanks

EDIT: I have various classes: I have a class called Dvd which has methods and variables for a title(string) and number of copies of that title(int). I also have a linked list class, a listinterface, a node class, and a main class.


回答1:


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()?




回答2:


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
}



回答3:


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);
      }
}


来源:https://stackoverflow.com/questions/19802104/how-would-i-make-my-custom-generic-type-linked-list-in-java-sorted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!