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.
Constructor LinkedList(T[] array) converts the given array into a LinkedList.
Constructor LinkedList(LinkedList original) instantiates a new LinkedList by copying another
List.
Method equals() determines whether the given Object is another LinkedList, containing the same
data in the same order, returning whether there is equality.
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.
Method altLists() splices together two LinkedLists to create a third List which contains
alternating values from the original list and the given parameter.
*Below is my Lab3 LinkedList.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() {
first = null;
last = null;
iterator = null;
length = 0;
}
/**
* 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) {
}
public T getFirst() throws NoSuchElementException {
if (first == null) throw new NoSuchElementException();
return first.data;
}
public T getLast() throws NoSuchElementException {
if (last == null) throw new NoSuchElementException();
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
return iterator.data;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public boolean offEnd() {
return iterator == null;
}
public void addFirst(T data) {
Node newNode = new Node(data);
if (length == 0) {
first = newNode;
last = newNode;
} else .
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
1. 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.
Constructor LinkedList(T[] array) converts the given array into a LinkedList.
Constructor LinkedList(LinkedList original) instantiates a new LinkedList by copying another
List.
Method equals() determines whether the given Object is another LinkedList, containing the same
data in the same order, returning whether there is equality.
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.
Method altLists() splices together two LinkedLists to create a third List which contains
alternating values from the original list and the given parameter.
*Below is my Lab3 LinkedList.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;
2. private Node first;
private Node last;
private Node iterator;
/**** CONSTRUCTORS ****/
/**
* Instantiates a new LinkedList with default values
* @postcondition
*/
public LinkedList() {
first = null;
last = null;
iterator = null;
length = 0;
}
/**
* 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) {
}
public T getFirst() throws NoSuchElementException {
if (first == null) throw new NoSuchElementException();
return first.data;
}
public T getLast() throws NoSuchElementException {
3. if (last == null) throw new NoSuchElementException();
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
return iterator.data;
}
public int getLength() {
return length;
}
public boolean isEmpty() {
return length == 0;
}
public boolean offEnd() {
return iterator == null;
}
public void addFirst(T data) {
Node newNode = new Node(data);
if (length == 0) {
first = newNode;
last = newNode;
} else {
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
return;
}
4. public void addLast(T data) {
Node newNode = new Node(data);
if (length == 0) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
return;
}
public void addIterator(T data) throws NullPointerException{
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
Node newNode = new Node(data);
newNode.prev = iterator;
newNode.next = iterator.next;
if (iterator.next != null) {
iterator.next.prev = newNode;
}
iterator.next = newNode;
length++;
}
public void removeFirst() throws NoSuchElementException {
if (first == null) throw new NoSuchElementException();
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
5. first = first.next;
first.prev = first;
}
length--;
return;
}
public void removeLast() throws NoSuchElementException {
if (last == null) throw new NoSuchElementException();
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == last) {
iterator = null;
}
last = last.prev;
last.next = null;
}
length--;
return;
}
public void removeIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
if (iterator.prev != null) {
iterator.prev.next = iterator.next;
}
if (iterator.next != null) {
iterator.next.prev = iterator.prev;
}
6. if (iterator == first) {
first = iterator.next;
}
if (iterator == last) {
last = iterator.prev;
}
iterator = null;
length--;
}
public void positionIterator(){
iterator = first;
}
public void advanceIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
iterator = iterator.next;
}
public void reverseIterator() throws NullPointerException {
if (iterator == null) {
throw new NullPointerException("Iterator is off end.");
}
iterator = iterator.prev;
}
/**** ADDITIONAL OPERATIONS ****/
/**
* Re-sets LinkedList to empty as if the
* default constructor had just been called
*/
public void clear() {
}
@Override
7. public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null) {
result.append(temp.data).append(" ");
temp = temp.next;
}
return result.toString() + "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)
* @throws IllegalArgumentException when numMoves < 0
*/
public void spinList(int numMoves) throws IllegalArgumentException{
8. }
/**
* 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
* interlocking this and list
* @postcondition this and list are unchanged
*/
public LinkedList altLists(LinkedList list) {
return null;
}
}