Labprogram.java
LinkedList.java
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 ****/
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 (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator != null) {
return iterator.data;
} else {
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
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 (isEmpty()) {
first = newNode;
last = newNode;
} else {
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
}
public void addIterator(T data) throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("addIterator Iterator is off end.");
}
if (iterator == last) {
addLast(data);
} else {
Node newNode = new Node(data);
Node next = iterator.next;
newNode.next = next;
newNode.prev = iterator;
iterator.next = newNode;
next.prev = newNode;
length++;
}
}
public void removeFirst() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == first) {
iterator = null;
}
first = first.next;
first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == last) {
iterator = null;
}
last = last.prev;
last.next = null;
}
length--;
}
public void removeIterator() throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("Iterator is off the end opf the list.");
}
if (iterator .
1. Labprogram.java
LinkedList.java
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 ****/
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) {
}
2. /**
* 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 (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
return first.data;
}
public T getLast() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
return last.data;
}
public T getIterator() throws NullPointerException {
if (iterator != null) {
return iterator.data;
} else {
throw new NullPointerException("Iterator is off the end opf the list.");
}
}
public int getLength() {
return length;
}
3. public boolean isEmpty() {
return length == 0;
}
public boolean offEnd() {
return iterator == null;
}
public void addFirst(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
first = newNode;
last = newNode;
} else {
newNode.next = first;
first.prev = newNode;
first = newNode;
}
length++;
}
public void addLast(T data) {
Node newNode = new Node(data);
if (isEmpty()) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
newNode.prev = last;
last = newNode;
}
length++;
}
4. public void addIterator(T data) throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("addIterator Iterator is off end.");
}
if (iterator == last) {
addLast(data);
} else {
Node newNode = new Node(data);
Node next = iterator.next;
newNode.next = next;
newNode.prev = iterator;
iterator.next = newNode;
next.prev = newNode;
length++;
}
}
public void removeFirst() throws NoSuchElementException {
if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == first) {
iterator = null;
}
first = first.next;
first.prev = null;
}
length--;
}
public void removeLast() throws NoSuchElementException {
5. if (isEmpty()) {
throw new NoSuchElementException("The list is empty");
}
if (length == 1) {
first = null;
last = null;
iterator = null;
} else {
if (iterator == last) {
iterator = null;
}
last = last.prev;
last.next = null;
}
length--;
}
public void removeIterator() throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("Iterator is off the end opf the list.");
}
if (iterator == first) {
removeFirst();
} else if (iterator == last) {
removeLast();
} else {
Node prev = iterator.prev;
Node next = iterator.next;
prev.next = next;
next.prev = prev;
iterator = null;
length--;
}
}
public void positionIterator() {
6. iterator = first;
}
public void advanceIterator() throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("Iterator is off the end opf the list.");
}
iterator = iterator.next;
}
public void reverseIterator() throws NullPointerException {
if (offEnd()) {
throw new NullPointerException("Iterator is off the end opf the list.");
}
iterator = iterator.prev;
}
public void clear() {
first = null;
last = null;
iterator = null;
length = 0;
}
public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null) {
result.append(temp.data + " ");
temp = temp.next;
}
return result.toString() + "n";
}
public boolean equals(Object obj) {
return false;
7. }
public void spinList(int numMoves) throws IllegalArgumentException {
}
public LinkedList altLists(LinkedList list) {
return null;
}
} 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 origina1) instantiates
a new LinkedList by copying another List. public LinkedList (LinkedList T original) { ) 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]
ic class LabProgram {. oublic 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); //
tostring() has (n LinkedList abcList = new LinkedList > (new String[] {"A","B","Cn});
System.out.print ("Created abcList: " + abcList); LinkedList String emptyList = new LinkedList
langlerangle() ; System.out.print("Created emptyList: " + emptyList); string[] array = null;