Please do parts labeled TODO
/**
* LinkedList.java
*
* Replace all //TODO tags with your code
*
* Note that below the "//TODO" tag there may be
* something like "return null;", "return 0;", etc.
* That line is just "stubbed in" so the class
* will compile. When you add your code (one or many
* statements), you will want to delete the "stubbed" line.
* By "stubbed in" we mean "mocked" or "faked in" temporarily.
*
* When testing, construct using the static factory methods:
LinkedList.newEmpty()
LinkedList.from(arrayElements)
*/
package model.list;
import java.lang.reflect.Array;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import model.linearpub.DynamicList;
import model.linearpub.StructureIterator;
//This class is NOT java.util.LinkedList
public class LinkedList implements DynamicList {
//---------------------------------
// Instance Variables
//TODO - declare instance variable(s)
//---------------------------------
// Private Constructor
/** Constructs and returns new LinkedList (no args constructor) */
private LinkedList() {
}
//-------------------- List Statistics ---------------------
/**
* Return number of elements in this list.
*/
@Override
public int size() {
//TODO
return 0;
}
/**
* Return true is this list contains no elements.
*/
@Override
public boolean isEmpty() {
//TODO
return false;
}
//------------------ Accessing Elements --------------------
/**
* Return element at given index.
* Throws IndexOutOfBoundsException if passed index is invalid.
*/
@Override
public E get(int index) {
//TODO
return null;
}
/**
* Return first element
* Throws RuntimeException if list is empty
*/
@Override
public E first() {
//TODO
return null;
}
/**
* Return last element
* Throws RuntimeException if list is empty
*/
@Override
public E last() {
//TODO
return null;
}
/**
* Return a new list containing the elements of this list
* between the given index "start" (inclusive) and
* the given index "stop" (exclusive).
* Throws IndexOutOfBoundsException if either passed index is invalid.
*/
@Override
public DynamicList subList(int start, int stop) {
//TODO
return null;
}
/**
* Return index of first matching element (where searchFct outputs true)
* Return -1 if no match
* Example usage (first list of integers, then employees):
* index = list.find(eaInteger -> eaInteger == 10);
* index = employeeList.find(employee -> employee .getFirstName().equals("Kofi"));
*/
@Override
public int findFirst(Function searchFct) {
//TODO
return 0;
}
/**
* Return index of last matching element (where searchFct outputs true)
* E.g., if searching for employee with name "Kofi" and there is a match
* at index=3 and index=8, findLast will return 8 (the last matching index).
* Hint: start search at end of list and work backwards through list.
* Return -1 if no match
*/
@Override
public int findLast(Function searchFct) {
//TODO
return 0;
}
//------------------- Setting Elements ---------------------
/**
* Insert passed arg "newElem" into position "index"
* Return previous (replaced) elem at "index"
* Valid "index" values are between 0 and "size - 1"
* If "index" is invalid, throws IndexOutOfBoundsException.
*/
@Override
public E set(int index, E newElem) {
//TODO
return null;
}
//------- Inserting, Appending & Replacing Elements --------
//------------------ (Dynamic Behaviors) ------------------
/**
* Add the passed element to start of list
*/
@Override
public void addFirst(E newElem) {
//TODO
}
/**
* Add the passed element to end of list
*/
@Override
public void addLast(E newElem) {
//TODO
}
/**
* Alias for "addLast" (same functionality)
*/
@Override
public void add(E newElem) {
//TODO
}
/**
* Add all elements from "otherDynList" into "this" list
*/
@Override
public void addAll(DynamicList otherDynList) {
//TODO
}
/**
* Add all elements from passed fixed array "this" list
*/
@Override
public void addAll(E[] array) {
//TODO
}
/**
* Shift to the right the element currently at "insertIndex" (if any) and all elements to the right
* Insert passed arg "newElem" into position "insertIndex"
* Valid "insertIndex" values are between 0 and "size"
* If index = "size" then it becomes a simple "add" operation
* If "insertIndex" is invalid, throws IndexOutOfBoundsException
*/
@Override
public void insert(int insertIndex, E newElem) {
//TODO
}
//------------------- Removing Elements --------------------
//------------------ (Dynamic Behaviors) ------------------
/**
* Remove first element
* Return removed element
* Throws RuntimeException if list is empty
*/
@Override
public E removeFirst() {
//TODO
return null;
}
/**
* Remove last element
* Return removed element
* Throws RuntimeException if list is empty
*/
@Override
public E removeLast() {
//TODO
return null;
}
/**
* Reset the list so it is empty.
* If list is already empty, then do nothing
* No action is performed on the elements.
*
*/
@Override
public void removeAll() {
//TODO
}
/**
* Remove elem at index
* Return the removed element
* Throws IndexOutOfBoundsException if passed index is invalid.
*/
@Override
public E removeIndex(int index) {
//TODO
return null;
}
/**
* Remove first matching element (where searchFct outputs true)
* Return the removed element
* If no match, return null
*/
@Override
public E removeFirstMatching(Function searchFct) {
//TODO
return null;
}
//----------------- Convenience Methods ------------------
/** Return this list as array
* This method requires imports of:
* java.lang.reflect.Array;
* java.util.concurrent.atomic.AtomicInteger;
*/
@Override
@SuppressWarnings("unchecked")
public E[] toArray() {
//This method is completed (no work needed)
if (this.isEmpty())
return (E[]) Array.newInstance(Object.class, 0);
StructureIterator iter = this.iterator();
E[] array = (E[]) Array.newInstance(iter.peek().getClass(), this.size());
AtomicInteger counter = new AtomicInteger(0);
this.forEach((each) -> array[counter.getAndIncrement()] = each);
return array;
}
/**
* Returns one-line user-friendly message about this object
* Helpful method especially for debugging.
*/
@Override
public String toString() {
//TODO
return null;
}
/** Prints all elements to console, with newline after each */
@Override
public void printAll() {
//TODO
}
/** Iterates over elements in "this" object. For each element,
* performs actionFct (passing element being iterated on)
* The generic type "? super E" means some type that is
* a superclass of E (inclusive)
*/
@Override
public void forEach(Consumer actionFct) {
//TODO
}
/** Return new list that is "this" list joined
* with "otherList" list (this list's elements are
* first followed by the "otherList" list)
*/
@Override
public DynamicList join(DynamicList otherList) {
//TODO
return null;
}
//----------------- Utility Methods ------------------
/**
* Returns new DynamicList with "new elements". Each new element
* is generated from mapFct invoked with an element from
* this list.
*/
@Override
public DynamicList map(Function mapFct) {
//TODO
return null;
}
/**
* Returns new DynamicList containing only elements that
* result in true when applied to selectFct
* Returns new DynamicList which is elements
* selected from this list via selectFct
*/
@Override
public DynamicList select(Function selectFct) {
//TODO
return null;
}
/**
* Returns new DynamicList which is this list
* with elements rejected via rejectFct
*/
@Override
public DynamicList reject(Function rejectFct) {
//TODO
return null;
}
/** Accumulate a value by iterating over the collection
* and accumulating during iteration.
* E.g., accumulate a "sum", or accumulate
* a new collection which is the accumulation
* of sub-collections obtained from elements (think
* of accumulating all players in a league by
* accumulating the players from each team
*/
@Override
public T accumulate(BiFunction fct, T initialValue) {
//TODO
return null;
}
//---------------------------------
// Public Constructors (Static Factory Constructor Methods)
// These two methods are completed for you
/** Return a new empty LinkedList */
public static DynamicList newEmpty() {
return new LinkedList<>();
}
/** Return a new LinkedList that contains all elements from the
* param "aFixedArray" */
public static DynamicList from(T[] aFixedArray) {
DynamicList list = LinkedList.newEmpty();
for (T nextNewElem: aFixedArray)
list.add(nextNewElem);
return list;
}
//----------------------------------------------------------
/*TODO - helper methods (optional -- coder's choice)
Helper method simply means any methods you
add (your choice) to make coding easier (i.e.,
methods that "help" other methods by doing some
of the work. The other methods call the "helper
methods".
*/
}

Please do parts labeled TODO LinkedList.java Replace.pdf

  • 1.
    Please do partslabeled TODO /** * LinkedList.java * * Replace all //TODO tags with your code * * Note that below the "//TODO" tag there may be * something like "return null;", "return 0;", etc. * That line is just "stubbed in" so the class * will compile. When you add your code (one or many * statements), you will want to delete the "stubbed" line. * By "stubbed in" we mean "mocked" or "faked in" temporarily. * * When testing, construct using the static factory methods: LinkedList.newEmpty() LinkedList.from(arrayElements) */ package model.list; import java.lang.reflect.Array; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import model.linearpub.DynamicList; import model.linearpub.StructureIterator; //This class is NOT java.util.LinkedList public class LinkedList implements DynamicList {
  • 2.
    //--------------------------------- // Instance Variables //TODO- declare instance variable(s) //--------------------------------- // Private Constructor /** Constructs and returns new LinkedList (no args constructor) */ private LinkedList() { } //-------------------- List Statistics --------------------- /** * Return number of elements in this list. */ @Override public int size() { //TODO return 0; } /** * Return true is this list contains no elements. */ @Override public boolean isEmpty() { //TODO return false; } //------------------ Accessing Elements -------------------- /** * Return element at given index.
  • 3.
    * Throws IndexOutOfBoundsExceptionif passed index is invalid. */ @Override public E get(int index) { //TODO return null; } /** * Return first element * Throws RuntimeException if list is empty */ @Override public E first() { //TODO return null; } /** * Return last element * Throws RuntimeException if list is empty */ @Override public E last() { //TODO return null; } /** * Return a new list containing the elements of this list * between the given index "start" (inclusive) and * the given index "stop" (exclusive). * Throws IndexOutOfBoundsException if either passed index is invalid. */ @Override public DynamicList subList(int start, int stop) {
  • 4.
    //TODO return null; } /** * Returnindex of first matching element (where searchFct outputs true) * Return -1 if no match * Example usage (first list of integers, then employees): * index = list.find(eaInteger -> eaInteger == 10); * index = employeeList.find(employee -> employee .getFirstName().equals("Kofi")); */ @Override public int findFirst(Function searchFct) { //TODO return 0; } /** * Return index of last matching element (where searchFct outputs true) * E.g., if searching for employee with name "Kofi" and there is a match * at index=3 and index=8, findLast will return 8 (the last matching index). * Hint: start search at end of list and work backwards through list. * Return -1 if no match */ @Override public int findLast(Function searchFct) { //TODO return 0; } //------------------- Setting Elements --------------------- /** * Insert passed arg "newElem" into position "index" * Return previous (replaced) elem at "index" * Valid "index" values are between 0 and "size - 1"
  • 5.
    * If "index"is invalid, throws IndexOutOfBoundsException. */ @Override public E set(int index, E newElem) { //TODO return null; } //------- Inserting, Appending & Replacing Elements -------- //------------------ (Dynamic Behaviors) ------------------ /** * Add the passed element to start of list */ @Override public void addFirst(E newElem) { //TODO } /** * Add the passed element to end of list */ @Override public void addLast(E newElem) { //TODO } /** * Alias for "addLast" (same functionality) */ @Override public void add(E newElem) { //TODO } /**
  • 6.
    * Add allelements from "otherDynList" into "this" list */ @Override public void addAll(DynamicList otherDynList) { //TODO } /** * Add all elements from passed fixed array "this" list */ @Override public void addAll(E[] array) { //TODO } /** * Shift to the right the element currently at "insertIndex" (if any) and all elements to the right * Insert passed arg "newElem" into position "insertIndex" * Valid "insertIndex" values are between 0 and "size" * If index = "size" then it becomes a simple "add" operation * If "insertIndex" is invalid, throws IndexOutOfBoundsException */ @Override public void insert(int insertIndex, E newElem) { //TODO } //------------------- Removing Elements -------------------- //------------------ (Dynamic Behaviors) ------------------ /** * Remove first element * Return removed element * Throws RuntimeException if list is empty */ @Override
  • 7.
    public E removeFirst(){ //TODO return null; } /** * Remove last element * Return removed element * Throws RuntimeException if list is empty */ @Override public E removeLast() { //TODO return null; } /** * Reset the list so it is empty. * If list is already empty, then do nothing * No action is performed on the elements. * */ @Override public void removeAll() { //TODO } /** * Remove elem at index * Return the removed element * Throws IndexOutOfBoundsException if passed index is invalid. */ @Override public E removeIndex(int index) { //TODO return null;
  • 8.
    } /** * Remove firstmatching element (where searchFct outputs true) * Return the removed element * If no match, return null */ @Override public E removeFirstMatching(Function searchFct) { //TODO return null; } //----------------- Convenience Methods ------------------ /** Return this list as array * This method requires imports of: * java.lang.reflect.Array; * java.util.concurrent.atomic.AtomicInteger; */ @Override @SuppressWarnings("unchecked") public E[] toArray() { //This method is completed (no work needed) if (this.isEmpty()) return (E[]) Array.newInstance(Object.class, 0); StructureIterator iter = this.iterator(); E[] array = (E[]) Array.newInstance(iter.peek().getClass(), this.size()); AtomicInteger counter = new AtomicInteger(0); this.forEach((each) -> array[counter.getAndIncrement()] = each); return array; } /** * Returns one-line user-friendly message about this object * Helpful method especially for debugging.
  • 9.
    */ @Override public String toString(){ //TODO return null; } /** Prints all elements to console, with newline after each */ @Override public void printAll() { //TODO } /** Iterates over elements in "this" object. For each element, * performs actionFct (passing element being iterated on) * The generic type "? super E" means some type that is * a superclass of E (inclusive) */ @Override public void forEach(Consumer actionFct) { //TODO } /** Return new list that is "this" list joined * with "otherList" list (this list's elements are * first followed by the "otherList" list) */ @Override public DynamicList join(DynamicList otherList) { //TODO return null; } //----------------- Utility Methods ------------------ /**
  • 10.
    * Returns newDynamicList with "new elements". Each new element * is generated from mapFct invoked with an element from * this list. */ @Override public DynamicList map(Function mapFct) { //TODO return null; } /** * Returns new DynamicList containing only elements that * result in true when applied to selectFct * Returns new DynamicList which is elements * selected from this list via selectFct */ @Override public DynamicList select(Function selectFct) { //TODO return null; } /** * Returns new DynamicList which is this list * with elements rejected via rejectFct */ @Override public DynamicList reject(Function rejectFct) { //TODO return null; } /** Accumulate a value by iterating over the collection * and accumulating during iteration. * E.g., accumulate a "sum", or accumulate * a new collection which is the accumulation
  • 11.
    * of sub-collectionsobtained from elements (think * of accumulating all players in a league by * accumulating the players from each team */ @Override public T accumulate(BiFunction fct, T initialValue) { //TODO return null; } //--------------------------------- // Public Constructors (Static Factory Constructor Methods) // These two methods are completed for you /** Return a new empty LinkedList */ public static DynamicList newEmpty() { return new LinkedList<>(); } /** Return a new LinkedList that contains all elements from the * param "aFixedArray" */ public static DynamicList from(T[] aFixedArray) { DynamicList list = LinkedList.newEmpty(); for (T nextNewElem: aFixedArray) list.add(nextNewElem); return list; } //---------------------------------------------------------- /*TODO - helper methods (optional -- coder's choice) Helper method simply means any methods you add (your choice) to make coding easier (i.e., methods that "help" other methods by doing some of the work. The other methods call the "helper methods".
  • 12.