SlideShare a Scribd company logo
LabProgram.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 ****/
/**
* 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) {
}
/**** ACCESSORS ****/
/**
* Returns the value stored in the first node
* @precondition <>
* @return the value stored at node first
* @throws NoSuchElementException <>
*/
public T getFirst() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
}
return first.data;
}
/**
* Returns the value stored in the last node
* @precondition <>
* @return the value stored in the node last
* @throws NoSuchElementException <>
*/
public T getLast() throws NoSuchElementException {
if (isEmpty()){
throw new NoSuchElementException("The list is empty");
}
return last.data;
}
/**
* 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 iterator.data;
}
/**
* Returns the current length of the LinkedList
* @return the length of the LinkedList from 0 to n
*/
public int getLength() {
return length;
}
/**
* Returns whether the LinkedList is currently empty
* @return whether the LinkedList is empty
*/
public boolean isEmpty() {
return length == 0;
}
/**
* Returns whether the iterator is offEnd, i.e. null
* @return whether the iterator is null
*/
public boolean offEnd() {
return iterator == null;
}
/**** MUTATORS ****/
/**
* Creates a new first element
* @param data the data to insert at the front of the LinkedList
* @postcondition <>
*/
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++;
}
/**
* Creates a new last element
* @param data the data to insert at the end of the LinkedList
* @postcondition <>
*/
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++;
}
/**
* 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
*/
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--;
}
/**
* removes the element at the end of the LinkedList
*/
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--;
}
/**
* 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
*/
public void clear() {
first = null;
last = null;
iterator = null;
length = 0;
}
/**
* Converts the LinkedList to a String, with each value separated by a blank
* line At the end of the String, place a new line character
* @return the LinkedList as a String
*/
@Override
public String toString() {
StringBuilder result = new StringBuilder();
Node temp = first;
while (temp != null){
result.append(temp.data + " ");
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{
}
/**
* 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;
}
}
Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins
at null, as defined by the LinkedList constructors. Take a moment now to update your default
constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider
now what will happen if the iterator is at the first or last Node when these methods are called.
Update the methods to handle these edge cases now. We will develop additional methods during
this lab to work with the iterator. Remember to fill in the pre-and postconditions for each
method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
the LabProgram creates a list of numbers and inserts each into a list. The main() method then
calls various iterator methods, displaying results of the method operation. Use Develop mode to
test your Linked List 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 positionIterator() Method
positionIterator() moves the iterator to the beginning of the list. public void positionIterator() //
fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method
offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement
getIterator() Method getIterator() returns the element in the Node where the iterator is currently
located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator
forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method
reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement
additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement
removeIterator() Method removeIterator () removes the Node currently referenced by the iterator
and sets the iterator to null.
t java.util.scanner; c class LabProgram { ublic static void main(string[] args) { LabProgram lab
= new LabProgram(); // Make and display List LinkedList Integer list = new LinkedList >(); for
(int i=1;i<4;i++){ list. addLast (i); } System.out.print("Created list: " + list.tostring()); //
tostring() has system. out.println("list.offEnd(): " + list.offEnd()); system.out.println("list.
positionIterator()"); list.positionIterator(); system.out.println("list.offend(): " + list.offEnd());
system.out.println("list.getiterator(): " + list.getiterator ()); list.advanceiterator();
System.out.println("list.getiterator (): " + list.getiterator ());
system.out.println("list.advanceIterator ( )n); list.advanceIterator();
System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list.
reverseIterator ()n); list. reverseiterator(); System.out.println("list.getiterator(): " +
list.getiterator()); system.out.println("list.additerator (42)"); list.addIterator (42);
System.out.println("list.getiterator(): " + list.getiterator()); system.out.print "list. tostring(): " +
list.tostring()); system.out.println("list.advanceIterator ( )n); list. advanceiterator();
system.out.println("list.advanceIterator ( )n); list.advanceiterator();
System.out.println("list.additerator (99)"); list.additerator (99); system.out.print ("list. tostring():
" + list.tostring()); system.out.println("list.removeIterator()"); list.removerterator();
system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.offend(): " +
list.offend()); system.out.println("list. positionIterator()"); list. positionIterator();
system.out.println("list. removeIterator()"); list.removerterator();
system.out.println("list.offend(): " + list.offend()); system. out.print("list. tostring(): " +
list.tostring()); system.out.println("list. positionIterator()"); list.positioniterator();
system.out.println("list. advanceIterator ()"); list.advanceiterator();
system.out.println("list.advanceIterator ( ()"); list.advanceiterator(); system.out.println("list.
removeIterator()"); list. removerterator(); system.out.print("list. tostring(): " + list.tostring());

More Related Content

PDF
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
PDF
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
How do I fix it in javaLinkedList.java Defines a doubl.pdf
PDF
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
PDF
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
File LinkedList.java Defines a doubly-l.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf

Similar to LabProgram.javaimport java.util.NoSuchElementException;public .pdf (20)

PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
please i need help Im writing a program to test the merge sort alg.pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
PDF
The LinkedList1 class implements a Linked list. class.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
package linkedLists- import java-util-Iterator- --- A class representi.pdf
PDF
Fix my codeCode.pdf
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Note             Given Code modified as required and required met.pdf
PDF
In this lab, we will write an application to store a deck of cards i.pdf
PPT
singly link list project in dsa.....by rohit malav
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
please i need help Im writing a program to test the merge sort alg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Implementation The starter code includes List.java. You should not c.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Hi,I have added the methods and main class as per your requirement.pdf
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The LinkedList1 class implements a Linked list. class.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
Fix my codeCode.pdf
Please help me to make a programming project I have to sue them today- (1).pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Note             Given Code modified as required and required met.pdf
In this lab, we will write an application to store a deck of cards i.pdf
singly link list project in dsa.....by rohit malav
Ad

More from fantasiatheoutofthef (20)

PDF
Introduction Pervasive computing demands the all-encompassing exploi.pdf
PDF
International projects are on the rise, so the need for project mana.pdf
PDF
Instructions On July 1, a petty cash fund was established for $100. .pdf
PDF
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
PDF
Integral ICreate a Python function for I-Importancedef monteca.pdf
PDF
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
PDF
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
PDF
Jefferson City has several capital projects that the mayor wants to .pdf
PDF
Instructions Create an Android app according to the requirements below.pdf
PDF
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
PDF
In this case study, we will explore the exercise of power in leaders.pdf
PDF
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
PDF
Ive already completed the Java assignment below, but it doesnt wor.pdf
PDF
In this assignment you will implement insert() method for a singly l.pdf
PDF
Introduction Pervasive computing demands the all-encompassing exploita.pdf
PDF
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
PDF
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
PDF
Indicate and provide a brief explanation for whether the following i.pdf
PDF
Lindsay was leaving a local department store when an armed security .pdf
PDF
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
Introduction Pervasive computing demands the all-encompassing exploi.pdf
International projects are on the rise, so the need for project mana.pdf
Instructions On July 1, a petty cash fund was established for $100. .pdf
Just C, D, E 7-1 Hoare partition correctness The version of PART.pdf
Integral ICreate a Python function for I-Importancedef monteca.pdf
IntroductionThe capstone project is a �structured walkthrough� pen.pdf
Industry Solution Descartes for Ocean Carriers Customer Success Stor.pdf
Jefferson City has several capital projects that the mayor wants to .pdf
Instructions Create an Android app according to the requirements below.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
In this case study, we will explore the exercise of power in leaders.pdf
James Santelli was staying at a motel owned by Abu Rahmatullah for s.pdf
Ive already completed the Java assignment below, but it doesnt wor.pdf
In this assignment you will implement insert() method for a singly l.pdf
Introduction Pervasive computing demands the all-encompassing exploita.pdf
Introduction (1 Mark)Body (7 Marks)1-Definition of Technolog.pdf
Integral IEstimator for Iwhere f(x) = 20 - x^2 and p(x) ~ U[a,.pdf
Indicate and provide a brief explanation for whether the following i.pdf
Lindsay was leaving a local department store when an armed security .pdf
Learning Team � Ethical Challenges Worksheet Your team of internat.pdf
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
RMMM.pdf make it easy to upload and study
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Structure & Organelles in detailed.
PDF
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Weekly quiz Compilation Jan -July 25.pdf
Complications of Minimal Access Surgery at WLH
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
RMMM.pdf make it easy to upload and study
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Structure & Organelles in detailed.
01-Introduction-to-Information-Management.pdf

LabProgram.javaimport java.util.NoSuchElementException;public .pdf

  • 1. LabProgram.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 ****/ /** * 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 */
  • 2. 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 { if (isEmpty()){ throw new NoSuchElementException("The list is empty"); } return first.data; } /** * Returns the value stored in the last node * @precondition <> * @return the value stored in the node last * @throws NoSuchElementException <> */ public T getLast() throws NoSuchElementException { if (isEmpty()){ throw new NoSuchElementException("The list is empty");
  • 3. } return last.data; } /** * 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 iterator.data; } /** * Returns the current length of the LinkedList * @return the length of the LinkedList from 0 to n */ public int getLength() { return length; } /** * Returns whether the LinkedList is currently empty * @return whether the LinkedList is empty */ public boolean isEmpty() { return length == 0; } /** * Returns whether the iterator is offEnd, i.e. null * @return whether the iterator is null */ public boolean offEnd() { return iterator == null; } /**** MUTATORS ****/ /** * Creates a new first element
  • 4. * @param data the data to insert at the front of the LinkedList * @postcondition <> */ 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++; } /** * Creates a new last element * @param data the data to insert at the end of the LinkedList * @postcondition <> */ public void addLast(T data) { Node newNode = new Node(data); if(isEmpty()){ first = newNode; last = newNode; } else{
  • 5. last.next = newNode; newNode.prev = last; last = newNode; } length++; } /** * 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 */ 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){
  • 6. iterator = null; } first = first.next; first.prev = null; } length--; } /** * removes the element at the end of the LinkedList */ 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--; } /** * removes the element referenced by the iterator * @precondition * @postcondition
  • 7. * @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 */ public void clear() { first = null; last = null; iterator = null; length = 0;
  • 8. } /** * Converts the LinkedList to a String, with each value separated by a blank * line At the end of the String, place a new line character * @return the LinkedList as a String */ @Override public String toString() { StringBuilder result = new StringBuilder(); Node temp = first; while (temp != null){ result.append(temp.data + " "); 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
  • 9. * 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{ } /** * 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; } } Within your LinkedList class, see the field: private Node iterator; Notice that the iterator begins at null, as defined by the LinkedList constructors. Take a moment now to update your default constructor to set iterator = null; Also, look back at removefirst () and removeLast(). Consider now what will happen if the iterator is at the first or last Node when these methods are called. Update the methods to handle these edge cases now. We will develop additional methods during this lab to work with the iterator. Remember to fill in the pre-and postconditions for each method, as needed. Also inspect the LabProgram. java file and notice that the main () method of
  • 10. the LabProgram creates a list of numbers and inserts each into a list. The main() method then calls various iterator methods, displaying results of the method operation. Use Develop mode to test your Linked List 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 positionIterator() Method positionIterator() moves the iterator to the beginning of the list. public void positionIterator() // fill in here } public void positionIterator() ( // fill in here } Step 3: Implement offEnd() Method offEnd () returns whether the iterator is off the end of the list, i.e. set to null. Step 4: Implement getIterator() Method getIterator() returns the element in the Node where the iterator is currently located. Step 5: Implement advanceIterator() Method advanceIterator () moves the iterator forward by one node towards the last node. Step 6: Implement reverseIterator( ) Method reverseIterator () moves the iterator back by one node towards the first node. Step 7: Implement additerator() Method addIterator() inserts an element after the iterator. Step 8: Implement removeIterator() Method removeIterator () removes the Node currently referenced by the iterator and sets the iterator to null. t java.util.scanner; c class LabProgram { ublic static void main(string[] args) { LabProgram lab = new LabProgram(); // Make and display List LinkedList Integer list = new LinkedList >(); for (int i=1;i<4;i++){ list. addLast (i); } System.out.print("Created list: " + list.tostring()); // tostring() has system. out.println("list.offEnd(): " + list.offEnd()); system.out.println("list. positionIterator()"); list.positionIterator(); system.out.println("list.offend(): " + list.offEnd()); system.out.println("list.getiterator(): " + list.getiterator ()); list.advanceiterator(); System.out.println("list.getiterator (): " + list.getiterator ()); system.out.println("list.advanceIterator ( )n); list.advanceIterator(); System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list. reverseIterator ()n); list. reverseiterator(); System.out.println("list.getiterator(): " + list.getiterator()); system.out.println("list.additerator (42)"); list.addIterator (42); System.out.println("list.getiterator(): " + list.getiterator()); system.out.print "list. tostring(): " + list.tostring()); system.out.println("list.advanceIterator ( )n); list. advanceiterator(); system.out.println("list.advanceIterator ( )n); list.advanceiterator(); System.out.println("list.additerator (99)"); list.additerator (99); system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.removeIterator()"); list.removerterator(); system.out.print ("list. tostring(): " + list.tostring()); system.out.println("list.offend(): " + list.offend()); system.out.println("list. positionIterator()"); list. positionIterator(); system.out.println("list. removeIterator()"); list.removerterator(); system.out.println("list.offend(): " + list.offend()); system. out.print("list. tostring(): " + list.tostring()); system.out.println("list. positionIterator()"); list.positioniterator();
  • 11. system.out.println("list. advanceIterator ()"); list.advanceiterator(); system.out.println("list.advanceIterator ( ()"); list.advanceiterator(); system.out.println("list. removeIterator()"); list. removerterator(); system.out.print("list. tostring(): " + list.tostring());