File: LinkedList.java
/**
* Defines a doubly-linked list class
*/
import java.util.NoSuchElementException;
public class LinkedList {
private class Node {
private T data;
private Node next;
private Node prev;
public Node(T data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private int length;
private Node first;
private Node last;
private Node iterator;
/**** CONSTRUCTORS ****/
/**
* Instantiates a new LinkedList with default values
* @postcondition
*/
public LinkedList() {
}
/**
* Converts the given array into a LinkedList
* @param array the array of values to insert into this LinkedList
* @postcondition
*/
public LinkedList(T[] array) {
}
/**
* Instantiates a new LinkedList by copying another List
* @param original the LinkedList to copy
* @postcondition a new List object, which is an identical,
* but separate, copy of the LinkedList original
*/
public LinkedList(LinkedList original) {
}
/**** ACCESSORS ****/
/**
* Returns the value stored in the first node
* @precondition
* @return the value stored at node first
* @throws NoSuchElementException
*/
public T getFirst() throws NoSuchElementException {
return null;
}
/**
* Returns the value stored in the last node
* @precondition
* @return the value stored in the node last
* @throws NoSuchElementException
*/
public T getLast() throws NoSuchElementException {
return null;
}
/**
* Returns the data stored in the iterator node
* @precondition
* @return the data stored in the iterator node
* @throw NullPointerException
*/
public T getIterator() throws NullPointerException {
return null;
}
/**
* Returns the current length of the LinkedList
* @return the length of the LinkedList from 0 to n
*/
public int getLength() {
return -1;
}
/**
* Returns whether the LinkedList is currently empty
* @return whether the LinkedList is empty
*/
public boolean isEmpty() {
return false;
}
/**
* Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return false;
}
/**** MUTATORS ****/
/**
* Creates a new first element
* @param data the data to insert at the front of the LinkedList
* @postcondition
*/
public void addFirst(T data) {
return;
}
/**
* Creates a new last element
* @param data the data to insert at the end of the LinkedList
* @postcondition
*/
public void addLast(T data) {
return;
}
/**
* Inserts a new element after the iterator
* @param data the data to insert
* @precondition
* @throws NullPointerException
*/
public void addIterator(T data) throws NullPointerException{
return;
}
/**
* removes the element at the front of the LinkedList
* @precondition
* @postcondition
* @throws NoSuchElementException
*/
public void removeFirst() throws NoSuchElementException {
return;
}
/**
* removes the element at the end of the LinkedList
* @precondition
* @postcondition
* @throws NoSuchElementException
*/
public void removeLast() throws NoSuchElementException {
return;
}
/**
* removes the element referenced by the iterator
* @precondition
* @pos.
Z Score,T Score, Percential Rank and Box Plot Graph
File LinkedList.java Defines a doubly-l.pdf
1. File: LinkedList.java
/**
* Defines a doubly-linked list class
*/
import java.util.NoSuchElementException;
public class LinkedList {
private class Node {
private T data;
private Node next;
private Node prev;
public Node(T data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private int length;
private Node first;
private Node last;
private Node iterator;
/**** CONSTRUCTORS ****/
/**
* Instantiates a new LinkedList with default values
* @postcondition
*/
2. public LinkedList() {
}
/**
* Converts the given array into a LinkedList
* @param array the array of values to insert into this LinkedList
* @postcondition
*/
public LinkedList(T[] array) {
}
/**
* Instantiates a new LinkedList by copying another List
* @param original the LinkedList to copy
* @postcondition a new List object, which is an identical,
* but separate, copy of the LinkedList original
*/
public LinkedList(LinkedList original) {
}
/**** ACCESSORS ****/
/**
* Returns the value stored in the first node
* @precondition
* @return the value stored at node first
* @throws NoSuchElementException
*/
public T getFirst() throws NoSuchElementException {
return null;
}
/**
* Returns the value stored in the last node
* @precondition
* @return the value stored in the node last
* @throws NoSuchElementException
*/
public T getLast() throws NoSuchElementException {
return null;
}
3. /**
* Returns the data stored in the iterator node
* @precondition
* @return the data stored in the iterator node
* @throw NullPointerException
*/
public T getIterator() throws NullPointerException {
return null;
}
/**
* Returns the current length of the LinkedList
* @return the length of the LinkedList from 0 to n
*/
public int getLength() {
return -1;
}
/**
* Returns whether the LinkedList is currently empty
* @return whether the LinkedList is empty
*/
public boolean isEmpty() {
return false;
}
/**
* Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return false;
}
/**** MUTATORS ****/
/**
* Creates a new first element
* @param data the data to insert at the front of the LinkedList
* @postcondition
*/
4. public void addFirst(T data) {
return;
}
/**
* Creates a new last element
* @param data the data to insert at the end of the LinkedList
* @postcondition
*/
public void addLast(T data) {
return;
}
/**
* Inserts a new element after the iterator
* @param data the data to insert
* @precondition
* @throws NullPointerException
*/
public void addIterator(T data) throws NullPointerException{
return;
}
/**
* removes the element at the front of the LinkedList
* @precondition
* @postcondition
* @throws NoSuchElementException
*/
public void removeFirst() throws NoSuchElementException {
return;
}
/**
* removes the element at the end of the LinkedList
* @precondition
* @postcondition
* @throws NoSuchElementException
*/
public void removeLast() throws NoSuchElementException {
5. return;
}
/**
* removes the element referenced by the iterator
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void removeIterator() throws NullPointerException {
}
/**
* places the iterator at the first node
* @postcondition
*/
public void positionIterator(){
}
/**
* Moves the iterator one node towards the last
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void advanceIterator() throws NullPointerException {
}
/**
* Moves the iterator one node towards the first
* @precondition
* @postcondition
* @throws NullPointerException
*/
public void reverseIterator() throws NullPointerException {
}
/**** ADDITIONAL OPERATIONS ****/
/**
* Re-sets LinkedList to empty as if the
* default constructor had just been called
6. */
public void clear() {
}
/**
* Converts the LinkedList to a String, with each value separated by a
* blank space. At the end of the String, place a new line character.
* @return the LinkedList as a String
*/
@Override
public String toString() {
return "n";
}
/**
* Determines whether the given Object is
* another LinkedList, containing
* the same data in the same order
* @param obj another Object
* @return whether there is equality
*/
@SuppressWarnings("unchecked") //good practice to remove warning here
@Override public boolean equals(Object obj) {
return false;
}
/**CHALLENGE METHODS*/
/**
* Moves all nodes in the list towards the end
* of the list the number of times specified
* Any node that falls off the end of the list as it
* moves forward will be placed the front of the list
* For example: [1, 2, 3, 4, 5], numMoves = 2 -> [4, 5, 1, 2 ,3]
* For example: [1, 2, 3, 4, 5], numMoves = 4 -> [2, 3, 4, 5, 1]
* For example: [1, 2, 3, 4, 5], numMoves = 7 -> [4, 5, 1, 2 ,3]
* @param numMoves the number of times to move each node.
* @precondition numMoves >= 0
* @postcondition iterator position unchanged (i.e. still referencing
* the same node in the list, regardless of new location of Node)
7. * @throws IllegalArgumentException when numMoves < 0
*/
public void spinList(int numMoves) throws IllegalArgumentException{
}
/**
* Splices together two LinkedLists to create a third List
* which contains alternating values from this list
* and the given parameter
* For example: [1,2,3] and [4,5,6] -> [1,4,2,5,3,6]
* For example: [1, 2, 3, 4] and [5, 6] -> [1, 5, 2, 6, 3, 4]
* For example: [1, 2] and [3, 4, 5, 6] -> [1, 3, 2, 4, 5, 6]
* @param list the second LinkedList
* @return a new LinkedList, which is the result of
* alternating this and list
* @postcondition this and list are unchanged
*/
public LinkedList altLists(LinkedList list) {
return null;
}
}
Created intlist: 12334 Created abclist: A B C Created emptyList: Created nullist: Created
intcopy: 1234 Created abcCopy: A B C Created emptycopy: Created nullcopy: intList.equals
(null): false intlist.equals (emptyList): false intlist.equals(abclist): false abclist.equals (intList):
false intList.equals (intList): true abclist.equals (abcList) : trued abcList.equals (abcCopy) : true
nullist.equals (nullcopy): true emptyList. equals (emptycopy): true emptyList.equals (nullist) :
true abcList.spinList (0): A B C abcList.spinList(1): C A B abcList.spinList(2): A B C
abcList.spinList(3): A B C intList.spinList(3): 2341 intList.spinList(5): 1234 emptyList. spinList
(1): nullist.spinList (1): Created list123: 123 Created list456: 456 list123.altLists(list456):
142536 Created list1234: 1234 Created list56: 56 list1234.altLists(list56): 152634 Created list12:
12 Created list3456: 3456 list12.altists(list3456): 132456 abcList.altLists (emptyList): A B C
abclist.altists(nullist): A B C
8. // Test spinlist abcList.spinList ( ); System.out.print ("abcList.spinList( ) : ");
abclist.displayList(); abcList.spinList(1); System.out.print ("abcList.spinList(1): ");
abcList.displaylist(); abcList.spinList(2); System.out.print("abcList.spinList(2): ");
abcList.displaylist(); abcList.spinList(3); System.out.print ("abcList.spinList(3): ");
abcList.displayList(); intList.spinList(3); System.out.print("intList.spinList(3): ");
intList.displaylist(); intList.spinList(5); System.out.print ("intList.spinList(5): ");
intList.displayList(); emptyList.spinList(1); System.out.print ("emptyList.spinList(1): ");
emptyList.displaylist(); nullList.spinList(1); System.out.print("nullList.spinList(1): ");
nullList.displayList(); // Test altLists LinkedList> list123 = new LinkedList >( new Integer [ ]
{1,2,3}); System.out.print("Created list123: "); list123.displayList(); LinkedList> list456 = new
LinkedList >( new Integer [ ] {4,5,6}); System.out.print("Created list456: ");
list456.displayList(); System.out.print("list123.altLists(list456): ");
list123.altLists(list456).displayList(); LinkedList> list1234 = new LinkedList >( new
Integer[]{1,2,3,4}); System.out.print("Created list1234: "); list1234.displayList();
2:Unit test all assigned methods 0/35 New constructors, equals(), spinList(), altList()
java.lang.NullPointerException
Step 1: Copy your LinkedList Copy your completed LinkedList class from Lab 3 into the
LinkedList. java file below. During this lab we will add more constructors, an equals method and
more challenging methods as listed below. Also inspect the LabProgram. java file and notice that
the main() method of the LabProgram copies and compares two linked lists. The main () method
also calls the more challenging methods. Use Develop mode to test your LinkedList iterator code
as you develop it. In Submit mode you will need to complete all lab steps to pass all automatic
tests. Step 2: Implement clear() Method clear() re-sets the LinkedList to empty as if the default
constructor had just been called. Step 3: Implement LinkedList(T[ ] array) Constructor
LinkedList ( T[] array) converts the given array into a LinkedList. public LinkedList (T[] array)
& } Step 4: Implement the copy constructor Constructor LinkedList (LinkedList T> original)
instantiates a new LinkedList by copying another List. public LinkedList (LinkedList original) {
9. } Step 5: Implement boolean equals(object o) Method equals() determines whether the given
object is another LinkedList, containing the same data in the same order, returning whether there
is equality. Step 6: Implement spinList(int numMoves) Method spinList (int numMoves) moves
all nodes in the list towards the end of the list the number of times specified. Any node that falls
off the end of the list as it moves forward will be placed at the front of the list. Ex: [A,B,C],
numMoves =1[C,A,B] Ex: [1,2,3,4], numMoves =3[2,3,4,1] Ex: [2,3,4,1], numMoves
=5[1,2,3,4] Step 7: Implement LinkedList T altLists (LinkedList T list) Method altLists() splices
together two LinkedLists to create a third List which contains alternating values from the original
list and the given parameter. Ex: [1,2,3] and [4,5,6][1,4,2,5,3,6] Ex: [1,2,3,4] and
[5,6][1,5,2,6,3,4]
LinkedList list56 = new LinkedList<>(new Integer[] {5,6}); System.out.print("Created list56: ");
list56.displayList(); System.out.print("list1234.altLists(list56): ");
list1234.altLists(list56).displayList(); LinkedList list12 = new LinkedList<>(new Integer[] {1,2
} ); System.out.print("Created list12: "); list12.displayList(); LinkedList list3456 = new
LinkedList<>(new Integer[] {3,4,5,6 }); System.out.print("Created list3456: ");
list3456.displayList(); System.out.print("list12.altLists(list3456): ");
list12.altLists(list3456).displayList(); System.out.print("abcList.altLists(emptyList): ");
abcList.altLists(emptyList).displayList(); System.out.print("abcList.altLists(nullList): ");
abcList.altLists(nullList).displayList(); LinkedList Integer > list5 = new LinkedList >( new
Integer [ ]{5,6}); System.out.print("Created list56: "); list56.displayList();
System.out.print("list1234.altLists(list56): "); list1234.altLists(list56).displayList(); LinkedList>
list12 = new LinkedList <>( new Integer[ ] {1,2}); System.out.print("Created list12: ");
list12.displayList(); LinkedList list3456 = new LinkedList<>(new Integer[ ]{3,4,5,6});
System.out.print ("Created list3456: "); list3456.displayList();
System.out.print("list12.altLists(list3456): "); list12.altLists(list3456).displayList();
System.out.print("abcList.altLists(emptyList): "); abcList.altLists (emptyList).displayList();
System.out.print("abcList.altLists(nullList): "); abcList.altLists(nullList).displayList(); }
lic class LabProgram T[{] public static void main(String[] args) { LabProgram lab = new
LabProgram(); // Make and display lists using array constructor LinkedList Integer > intList =
new LinkedList >( new Integer [ ] {1,2,3,4}); System.out.print("created intList: ");
intList.displaylist(); LinkedList abcList = new LinkedList<>(new String[] { "A", "B", "C" } );
System.out.print("created abcList: "); abclist.displaylist(); LinkedList String > emptyList = new
LinkedList langlerangle() ; System.out.print ("Created emptyList: "); emptyList.displaylist();
String[] array = new String[0];// Initialize with an empty array LinkedList String > nullList =