如何在没有任何标准Java库的情况下根据linkedlist进行自己的设置



i基于linkedlist创建的集合并想要对节点进行排序,我添加了set的值,我可以在没有任何标准Java库的情况下做到这一点吗? 软件包ru.matevosyan;

    import java.util.Iterator;
    import java.util.NoSuchElementException;
/**
 * DynamicSetByLinkedListclass.

*创建于29.05.2017。 * @author Matevosyan Vardan * @version 1.0 */

public class DynamicSetByLinkedList<E> implements IDynamicSetByLinkedList<E>, Iterable<E> {
    private Node<E> last;
    private int size = 0;
    private Node<E> first;
    /**
     * Constructor.
     */
    public DynamicSetByLinkedList() {
    }
    /**
     * Create to add value to the set.
     * @param value is value that type which you declare in generics.
     */
    @Override
    public void add(E value) {
        if (!checkDuplicate(value)) {
            linkLast(value);
        }
    }
    private boolean checkDuplicate(E value) {
        Node<E> firstNode = first;
        boolean hasDuplicate = false;
        for (int i = 0; i < size & size > 1; i++) {
            firstNode = firstNode.next;
            if (firstNode == null) {
                break;
            }
            if (firstNode.item.equals(value)) {
                hasDuplicate = true;
            }
        }
        return hasDuplicate;
    }
    /**
     * linkLast assign LinkedList Node to the last Node in the list and if it is the first value assign as first too.
     * @param elementValue is value.
     */
    private void linkLast(E elementValue) {
        Node<E> lastNode = last;
        Node<E> newNode = new Node<>(lastNode, elementValue, null);
        last = newNode;
        if (lastNode != null) {
            lastNode.next = newNode;
        } else {
            first = newNode;
        }
        size++;
        if(size > 1) {
            sortByHashcode();
        }
    }
    private void sortByHashcode() {

**here going to be a sort algorithm**
        }
    }
    /**
     * For returning the size of set.
     * @return size
     */
    public int len() {
        return size;
    }
    /**
     * Class Node describe linkedList entry.
     * @param <E> parameter that defined when create an instance of a class.
     */
    private static class Node<E> {
        E item;
        Node<E> prev;
        Node<E> next;
        Node(Node<E> prev, E element, Node<E> next) {
            this.prev = prev;
            this.item = element;
            this.next = next;
        }
    }
    /**
     * Override iterator method from interface Iterable.
     * @return an instance of Iterator type.
     */
    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            int count = 0;
            @Override
            public boolean hasNext() {
                return (count < size) && (last != null);
            }
            @Override
            public E next() {
                count++;
                Node<E> nextNode = first;
                if (nextNode != null && count > 1) {
                    nextNode = nextNode.next;
                }
                if (nextNode == null | size < count) {
                    throw new NoSuchElementException("Does not exist");
                }
                return nextNode.item;
            }
        };
    }
}

由于您的集合将在每个add()方法之后进行排序,因此您需要在列表中找到新元素的位置。看,当您添加第一个元素时,您的集合肯定会分类,因此添加下一个元素,您只需要找到它们的特定位置即可。因此,您可以为此使用下一个实现:

private void sortByHashcode() {
    Node<E> node = first;
    while (node != null) {
        if (last.item.hashCode() < node.item.hashCode()) {
            Node<E> tmp = last.prev;
            last.next = node;
            last.prev = node.prev;
            node.prev = last;
            if (last.prev == null) {
                first = last;
            } else {
                last.prev.next = last;
            }
            tmp.next = null;
            last = tmp;
            break;
        }
        node = node.next;
    }
}

P.S。但是,我认为这是一个很奇怪的收藏。我认为您应该再考虑一下,您可以使用标准的Java集合之一(即与比较器一起使用Treeset(。您能解释一下要解决的问题吗?

如果链接列表包含可以使用:

排序的原始类型
Collections.sort(list,null);

如果链接列表包含您创建的类的对象

最新更新