Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
5.1 kB
2
Indexable
Never
import java.util.Locale;

public class GelinkteListe {

    private int size;

    private ListItem firstItem;

    private ListItem lastItem;

    public GelinkteListe() {
        this.size = 0;
        this.firstItem = null;
        this.lastItem = null;
    }

    public void addAll(Object[] objects) {
        for (Object object : objects) {
            add(object);
        }
    }

    public void add(Object object) {
        ListItem listElement = new ListItem(object, lastItem, null);
        if (this.lastItem != null) {
            // insert into chain
            this.lastItem.next = listElement;
            listElement.previous = this.lastItem;
        }

        if (size == 0) {
            this.firstItem = listElement;
        }
        this.lastItem = listElement;
        this.size++;
    }

    public int getSize() {
        return size;
    }

    public Object[] getContent() {
        if (size == 0) {
            return new Object[]{};
        }
        Object[] ret = new Object[size]; // stuff into array and return
        ListItem item = firstItem;
        for (int i = 0; i < size; i++) {
            ret[i] = item.content;
            item = item.next;
        }

        return ret;
    }

    public void clear() {
        this.size = 0;
        this.firstItem = null;
        this.lastItem = null;
    }

    public void remove(int index) {
        if (index > size) {
            return;
        }
        ListItem removeItem = this.firstItem;
        for (int i = 0; i < index; i++) {
            removeItem = removeItem.next;
        }

        ListItem previous = removeItem.previous;
        if (previous != null) {
            previous.next = removeItem.next;
        } else {
            // item without previous = first in list deleted
            this.firstItem = removeItem.next;
        }
        ListItem next = removeItem.next;
        if (next != null) {
            next.previous = removeItem.previous;
        } else {
            // item wihout next = last in list deleted
            this.lastItem = removeItem.previous;
        }

        size--;
        if (size == 0) {
            // list empty = reset
            this.firstItem = null;
            this.lastItem = null;
        }
    }


    public void insertionSort(ValueCompare valueCompare) {
        if (size == 0) {
            return;
        }
        Object[] content = getContent();
        Object temp;
        for (int i = 1; i < content.length; i++) {
            temp = content[i];
            int j = i;
            for (j = i; j > 0 && valueCompare.isBigger(content[j - 1], temp); j--) {
                content[j] = content[j - 1];
            }
            content[j] = temp;
        }
        // Add sorted array into list
        clear();
        addAll(content);
        return;
    }

    public static void main(String[] args) {

        GelinkteListe gelinkteListe = new GelinkteListe();

        gelinkteListe.add("BBB");
        gelinkteListe.add("bbb");
        gelinkteListe.add("xyz");
        gelinkteListe.add("aca");
        gelinkteListe.add("aba");
        gelinkteListe.add("aab");


        // Remove Item 2
        //gelinkteListe.remove(1);

        gelinkteListe.insertionSort(((object1, object2) -> {
            String s1 = ((String) object1).toLowerCase();
            String s2 = ((String) object2).toLowerCase();
            int min = s1.length() < s2.length() ? s1.length() : s2.length();
            for (int i = 0; i < min; i++) {
                if (s1.charAt(i) > s2.charAt(i)) {
                    return true;
                }
            }
            return false;
        }));


        // Print out
        Object[] contents = gelinkteListe.getContent();
        for (Object content : contents) {
            System.out.println((String) content);
        }

        /*
        Result:
        aab
        aba
        aca
        BBB
        bbb
        xyz
        */

    }

    interface ValueCompare {
        boolean isBigger(Object object1, Object object2);
    }

    class ListItem {
        private ListItem previous;

        private ListItem next;

        public Object content;

        public ListItem(Object item, ListItem previous, ListItem next) {
            this.previous = previous;
            this.next = next;
            this.content = item;
        }

        public ListItem getPrevious() {
            return previous;
        }

        public void setPrevious(ListItem previous) {
            this.previous = previous;
        }

        public ListItem getNext() {
            return next;
        }

        public void setNext(ListItem next) {
            this.next = next;
        }

        public Object getContent() {
            return content;
        }

        public void setContent(Object content) {
            this.content = content;
        }
    }

}