Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
14 kB
14
Indexable
Never
package cs1302.list;

import cs1302.listadt.StringList;

/** Represents an array-based {@link cs1302.listedadt.StringList}.
 */
public class LinkedStringList implements StringList {

    // Instance variable for a String linked named list
    private StringList.Node node;
    private int size;

    /**
     * This is the default constructor for an Linked-based string list.
     * The initial size of a list object constructed using this constructor is zero,
     * regardless of the list's underlying storage.
     */
    public LinkedStringList() {
        this.node = new StringList.Node();
        this.size = 0;
    } // LinkedStringList

    /**
     * This is a copy constructor. The list object that is constructed using this constructer is
     * a deep copy of the list reffered to by the constructor's parameter. It will contain the same
     * elements in the same order.
     * {@inheritDoc}
     */
    public LinkedStringList(StringList other) {
        if (other == null) {
            throw new NullPointerException("The specified list is null.");
        } //if
        this.size = other.size();

        StringList.Node tempList = new StringList.Node();
        for (int i = 0; i < other.size(); i++) {
            add(other.get(i));
        }

    } // LinkedStringList copy constructor

    /**
     * Appends the strings in a string list to the end of the list.
     * {@inheritDoc}
     */
    public boolean add(StringList sl) {
        boolean success = false;
        if (sl == null) {
            throw new NullPointerException("The specified list is null");
        } else if (sl.size() == 0) {
            return success;
        }
        success = true;

        StringList.Node tempNode = node;
        StringList.Node newNode = new StringList.Node();
        for (int i = 0; i < sl.size(); i++) {
            newNode.setNext(new StringList.Node(sl.get(i)));
            newNode = newNode.getNext();
        }

        for (int i = 0; i < size(); i++) {
            tempNode = tempNode.getNext();
        }
        tempNode.setNext(newNode);

        size += sl.size();
        return success;
    }

    /**
     * Inserts a specified string at the specified position in this list. Shifts the string
     * currently at that position and any subsequent strings to the right.
     * {@inheritDoc}
     */
    public boolean add(int index, String s) {
        boolean success = false;
        if (s == null) {
            throw new NullPointerException("The specified string is null.");
        } else if (s.isEmpty()) {
            throw new IllegalArgumentException("The specified string is empty.");
        } else if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("The index is out of range");
        } else {
            success = true;

            StringList.Node tempNode = node;
            StringList.Node newNode = new StringList.Node(s);
            for (int i = 0; i < index; i++) {
                tempNode = tempNode.getNext();
            }

            newNode.setNext(tempNode.getNext());
            tempNode.setNext(newNode);

            size++;
            return success;
        }
    } // add

    /**
     * Inserts the strings contained in the specified list at the position in this list,
     * in the order in which they appear in the specified list.
     * {@inheritDoc}
     */
    public boolean add(int index, StringList sl) {
        boolean success = false;
        if (sl == null) {
            throw new NullPointerException("The specified list is null");
        } else if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("The index is out of range");
        } else if (sl.size() == 0) {
            return success;
        }
        success = true;

        StringList.Node remainingNode = node;
        StringList.Node tempNode = node;
        StringList.Node newNode = new StringList.Node();
        for (int i = 0; i < sl.size(); i++) { //Puts all elements of sl into newNode
            newNode.setNext(new StringList.Node(sl.get(i)));
            newNode = newNode.getNext();
        }

        for (int i = 0; i < index; i++) {
            tempNode = tempNode.getNext();
        }

        newNode.setNext(tempNode.getNext());
        tempNode.setNext(newNode);


        size += sl.size();
        return success;

    } // add with StringList

    /**
     * Appends the string to the end of the list.
     * {@inheritDoc}
     */
    public boolean add(String s) {
        boolean success = false;
        if (s == null) {
            throw new NullPointerException("The specified string is null");
        } else if (s.equals("")) {
            throw new IllegalArgumentException("The specified string is empty");
        }
        success = true;

        StringList.Node tempNode = node;
        StringList.Node newNode = new StringList.Node(s);
        if (size == 0) {
            node.setStr(s);
        }

        while (tempNode.getNext() != null) {
            tempNode = tempNode.getNext();
        }
        tempNode.setNext(newNode);

        size++;
        return success;

    } // add with string only

    /**
     * Clear all of the elements of the list.
     * {@inheritDoc}
     */
    public void clear() {
        node = new StringList.Node();
        size = 0;
    } // clear

    /**
     * Returns a true if list contains a specified string.
     * {@inheritDoc}
     */
    public boolean contains(String o) {
        boolean contains = false;
        if (o == null) {
            throw new NullPointerException("The specified string is null");
        } else if (o.equals("")) {
            throw new IllegalArgumentException("The specified string is empty");
        }
        for (int i = 0; i < size(); i++) {
            if (get(i).equals(o)) {
                contains = true;
                return contains;
            }
        }
        return contains;
    } // contains

    /**
     * Returns true if list contains a specified string regardless of case.
     * {@inheritDoc}
     */
    public boolean containsIgnoreCase(String o) {
        boolean contains = false;
        if (o == null) {
            throw new NullPointerException("The specified string is null");
        } else if (o.equals("")) {
            throw new IllegalArgumentException("The specified string is empty");
        }
        for (int i = 0; i < size(); i++) {
            if (get(i).equalsIgnoreCase(o)) {
                contains = true;
                return contains;
            }
        }
        return contains;
    } // containsIgnoreCase

    /**
     * Returns true if list contains a specified substring.
     * {@inheritDoc}
     */
    public boolean containsSubstring(String o) {
        boolean contains = false;
        if (o == null) {
            throw new NullPointerException("The specified substring is null");
        } else if (o.equals("")) {
            throw new IllegalArgumentException("The specified substring is empty");
        }
        for (int i = 0; i < size(); i++) {
            if (get(i).contains(o)) {
                contains = true;
                return contains;
            }
        }
        return contains;
    } // containsSubString

    /**
     * Builds and returns a StringList from the list without any duplicate strings.
     * {@inheritDoc}
     */
    public StringList distinct() {
        StringList distinct = new LinkedStringList();
        for (int i = 0; i < size(); i++) {
            if (!distinct.contains(get(i))) {
                distinct.add(get(i));
            }
        }
        return distinct;
    } // distinct

    /**
     * Returns a string at a specific position.
     * {@inheritDoc}
     */
    public String get(int index) {
        if (index < 0 || index >= size()) {
            throw new IndexOutOfBoundsException("Index is out of range");
        } else {

            String resultString = "";
            StringList.Node tempNode = node;
            for (int i = 0; i <= index; i++) {
                tempNode = tempNode.getNext();
            }
            resultString = tempNode.getStr();
            return resultString;
        }
    } // get

    /**
     * Returns the index of the specified string.
     * {@inheritDoc}
     */
    public int indexOf(String s) {
        if (s == null) {
            throw new NullPointerException("The specified string is null");
        } else if (s.equals("")) {
            throw new IllegalArgumentException("The specified string is empty");
        }
        int indexOf = -1;
        for (int i = 0; i < size(); i++) {
            if (get(i).equals(s)) {
                indexOf = i;
            }
        }
        return indexOf;
    } // indexOf

    /**
     * Returns the index of the specified string, ignoring case.
     * {@inheritDoc}
     */
    public int indexOfIgnoreCase(String s) {
        if (s == null) {
            throw new NullPointerException("The specified string is null");
        } else if (s.equals("")) {
            throw new IllegalArgumentException("The specified string is empty");
        }
        int indexOf = -1;
        for (int i = 0; i < size(); i++) {
            if (get(i).equalsIgnoreCase(s)) {
                indexOf = i;
            }
        } // for
        return indexOf;
    } // indexOfIgnoreCase

    /**
     * Checks if the linked is empty or not.
     * {@inheritDoc}
     */
    public boolean isEmpty() {
        boolean isEmpty = false;
        if (size() == 0) {
            isEmpty = true;
            return isEmpty;
        }
        return isEmpty;
    } // isEmpty

    /**
     * Returns a string representation of the list.
     * {@inheritDoc}
     */
    public String makeString() {
        String madeString = "";
        for (int i = 0; i < size(); i++) {
            madeString += get(i);
        }
        return madeString;
    } // makeString

    /**
     * Returns a string representation of the list with a seperator string in between each element.
     * {@inheritDoc}
     */
    public String makeString(String sep) {
        String madeString = "";
        for (int i = 0; i < size(); i++) {
            madeString += get(i) + sep;
        }
        return madeString;
    } // makeString with String seperator

    /**
     * Returns a string representation of the list with a start, seperator, and end string.
     * {@inheritDoc}
     */
    public String makeString(String start, String sep, String end) {
        String madeString = "";
        String middleString = "";

        for (int i = 0; i < size(); i++) {
            if ((i != 0) && (i != size() - 1)) {
                middleString += get(i) + sep;
            }
        }

        madeString = start + get(0) + sep + middleString + get(size() - 1) + end;
        return madeString;
    }

    /**
     * Removes a string at a certain input and returns the string that was removed.
     * {@inheritDoc}
     */
    public String remove(int index) {
        String removedString = get(index);
        if (index < 0 || index >= size()) {
            throw new IndexOutOfBoundsException("The index is out of bounds");
        }

        StringList.Node tempNode = node;

        for (int i = 0; i < index; i++) {
            tempNode = tempNode.getNext();
        }
        tempNode.setNext(tempNode.getNext().getNext());

        size--;
        return removedString;
    } // remove

    /**
     * Reverses the linked elements and puts it into a StringList.
     * {@inheritDoc}
     */
    public StringList reverse() {
        StringList reversedList = new LinkedStringList();
        for (int i = size() - 1; i >= 0; i--) {
            reversedList.add(get(i));
        }
        return reversedList;
    } // reverse

    /**
     * Replaces the string at a specific index and returns the replaced string.
     * {@inheritDoc}
     */
    public String set(int index, String s) {
        String removedString = get(index);
        if (s == null) {
            throw new NullPointerException("The specified string is null.");
        } else if (s.equals("")) {
            throw new IllegalArgumentException("The specified string is empty.");
        } else if (index < 0 || index >= size()) {
            throw new IndexOutOfBoundsException("The index is out of range.");
        } else {

            StringList.Node tempNode = node;
            for (int i = 0; i <= index; i++) {
                tempNode = tempNode.getNext();
            }
            removedString = tempNode.getStr();
            tempNode.setStr(s);

            return removedString;
        } // else
    } // set

    /**
     * Returns the number of elements in this list.
     * @return the number of elements in this list.
     */
    public int size() {
        if (size > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        }
        return size;
    } // size

    /**
     * Creates and returns a new StringList with elements from a specific index to a specific index.
     * {@inheritDoc}
     */
    public StringList splice(int fromIndex, int toIndex) {
        if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex) {
            throw new IndexOutOfBoundsException("Illegal endpoint index value");
        } else {
            StringList newList = new LinkedStringList();
            for (int i = fromIndex; i < toIndex; i++) {
                newList.add(get(i));
            }
            return newList;
        }
    } // splice

    /**
     * Returns an linked containing all of the strings in the list.
     * {@inheritDoc}
     */
    public String[] toArray() {
        String[] newLinked = new String[size()];
        for (int i = 0; i < size(); i++) {
            newLinked[i] = get(i);
        }
        return newLinked;
    } // toLinked

} // LinkedStringList