OOP Generische Datenstruktur, die eine Liste von Werten speichert erstellen

Kart0

Mitglied
Ich soll in einer Aufgabe eine spezielle interne Datenstruktur zur Speicherung von Konten überarbeiten (MinimalList). Die Datenstruktur MinimalList muss zu einer generischen Datenstruktur, die eine Liste von Werten speichert, erweitert werden. Somit kann ich mit der Klasse MinimalList nicht nur für die Klasse Account sondern für beliebige Datentypen verwenden.

MinimalList zur Speicherung von Konten (Accounts):
Java:
/**
 * Custom implementation of a linked list.
 * @version 1.0
 * 
 */
public class MinimalList {
    private MinimalListNode head = null;
   
    /**
     * Creates a new empty MinimalList.
     */
    public MinimalList() { }

    /**
     * Adds the given account to the end of this list.
     * 
     * @param account
     *            the account to add
     */
    public void add(Account account) {
        MinimalListNode newNode = new MinimalListNode(account, null);

        /* check if the list is empty */
        if (isEmpty()) {
            this.head = newNode;
        } else {
            getLastNode().next = newNode;
        }
    }

    /**
     * Inserts the account at the given index inside the list or at the end if
     * {@code index} is not existent.
     * 
     * @param account
     *            the account to add
     * @param index
     *            the index at which {@code account} will be added if it exists
     */
    public void add(Account account, int index) {
        if (index < 0) {
            /* handle the exception here... */
            // index  = 0;     // assuming it was an usage error... this is not fail-fast compilant.
        }
       
        /* insert at the beginning */
        if (index == 0) {
            MinimalListNode temp = this.head;
            this.head = new MinimalListNode(account, temp);
        } else {
            MinimalListNode cNode = getNode(index - 1);
            /* if element before index exists, it is very simple to insert... */
            if (cNode != null) {
                MinimalListNode temp = cNode.next;
                cNode.next = new MinimalListNode(account, temp);
            }

            /* otherwise we add at the end */
            else {
                add(account);
            }
        }
    }

    /**
     * Removes the node at given index if it exists.
     * 
     * @param index
     *            the index
     * @return true if removal successful, false if node not existent
     */
    public boolean remove(int index) {
        if (isEmpty() || index < 0) {
            return false;
        }

        /* deleting first element is handled separately */
        if (index == 0) {
            this.head = (this.head.next != null) ? this.head.next : null;
            return true;
        }

        MinimalListNode cNode = getNode(index - 1);
        if (cNode != null && cNode.next != null) {
            cNode.next = (cNode.next.next != null) ? cNode.next.next : null;
            return true;
        }

        return false;
    }
   
    /**
     * Returns the index of the given account inside the list.
     * @param accountNumber the account to find
     * @return the index or -1 if {@code account} does not exist in this list
     */
    public int locate(int accountNumber) {
        if (isEmpty()) {
            return -1;
        }

        MinimalListNode cNode = this.head;
        int cIndex = 0;

        do {
            if (cNode.getContent().getAccountNumber() == accountNumber) {
                return cIndex;
            }

            cNode = cNode.next;
            cIndex++;
        } while (cNode != null);

        return -1;        
    }

    /**
     * Returns whether the given account exists in this list.
     * 
     * @param account
     *            the account to find
     * @return true if the account exists, false otherwise
     */
    public boolean contains(Account account) {
        if (isEmpty()) {
            return false;
        }

        MinimalListNode cNode = this.head;

        do {
            // this is only applicable for accounts inside the same bank, otherwise use always equals if implemented!!
            if (cNode.getContent().compareTo(account) == 0) {
                return true;
            }

            cNode = cNode.next;
        } while (cNode != null);

        return false;
    }

    /**
     * Returns the list size, i.e. the number of elements in this list.
     * 
     * @return the number of elements in this list
     */
    public int size() {
        if (isEmpty()) {
            return 0;
        }

        MinimalListNode cNode = this.head;
        int cSize = 0;

        do {
            cSize++;
            cNode = cNode.next;
        } while (cNode != null);

        return cSize;
    }

    /**
     * Returns the account at given index.
     * 
     * @param index
     *            the index
     * @return the account at given index
     */
    public Account get(int index) {
        MinimalListNode cNode = getNode(index);

        return (cNode == null) ? null : cNode.getContent();
    }

    /**
     * Returns the first account in this list.
     * 
     * @return first element
     */
    public Account getFirst() {
        MinimalListNode cNode = this.head;
        if (cNode == null) {
            return null;
        }

        return cNode.getContent();
    }

    /**
     * Returns the last account in this list.
     * 
     * @return last element
     */
    public Account getLast() {
        MinimalListNode cNode = getLastNode();
        if (cNode == null) {
            return null;
        }

        return cNode.getContent();
    }

    private MinimalListNode getNode(int index) {
        if (index < 0) {
            return null;
        }
       
        MinimalListNode cNode = this.head;
        int cIndex = 0;
        while (cNode != null && cNode.next != null && cIndex < index) {
            cNode = cNode.next;
            cIndex++;
        }
       
        /* if index does not exist return null */
        if (cIndex < index) {
            return null;
        }

        return cNode;
    }

    private boolean isEmpty() {
        return getLastNode() == null;
    }

    private MinimalListNode getLastNode() {
        MinimalListNode cNode = this.head;
        while (cNode != null && cNode.next != null) {
            cNode = cNode.next;
        }

        return cNode;
    }

    private class MinimalListNode {
        private MinimalListNode next = null;
        private Account content = null;

        /**
         * Creates a new node.
         * 
         * @param content
         *            the account
         * @param next
         *            the reference for the next list element
         */
        public MinimalListNode(Account content, MinimalListNode next) {
            this.content = content;
            this.next = next;
        }

        /**
         * Returns the node's content, i.e. the account object.
         * 
         * @return the account object stored in this node
         */
        public Account getContent() {
            return content;
        }

    }
}

Wie muss der code geändert werden, damit ich die MinimalList für beliebige Datentypen verwenden kann ?
 

klauskarambulut

Bekanntes Mitglied
public class MinimalList
erweitern zu
public class MinimalList<T>

Jedes vorkommen von Account mit T ersetzen.

Und in der Bank-Klasse in der das ganze verwendet wird die Liste so erzeugen.
MinimalList<Account> accounts = new MinimalList<>();
 

Neue Themen


Oben