create LinkedListjava and Runnerjava u coded all the linked
create LinkedList.java and Runner.java. u coded all the linked list operations, including iteration of the linked list, in the Runner.java file. In the new assignment you will write the basic linked list operations in the LinkedList.java file. You will use a LinkedList object in the Runner.java file as your linked list.
1. read the text file provided by the client and create a linked li
The skeleton of LinkedList.java is provided below.
public class LinkedList {
private Box head; LinkedList(){
}
/* Add b as the last node of the linked list.*/
void addNode(Box b){ // WRITE YOUR CODE HERE.
}
/* Insert b in position pos. If insertion is successful * return true, otherwise return false. */
boolean insertNode(Box b, int pos){
// WRITE YOUR CODE HERE
}
/**Print width, height, length, and volume of each of the boxes in * this linked list. */
void printAllBoxes(){
// WRITE YOUR CODE HERE. }
/** Remove the box in position pos. Return true if removal * is successful, otherwise return false.*/
boolean removeNode(int pos){ // WRITE YOUR CODE HERE. }
/** Return the box in position pos. Return null when pos is * invalid.*/
Box getBox(int pos){
// WRITE YOUR CODE HERE. }
/**Print width, height, length, and volume of each of the boxes in * this linked list in reverse order.*/
void printReverse(){ // WRITE YOUR CODE HERE. }
/**Initiate the iterator variable*/ void initiateIterator(){
// WRITE YOUR CODE HERE. }
/** * Return the box in the current iterator position. */
Box getNextBox(){ // WRITE YOUR CODE HERE. } }
Solution
You can implement the Linked List as follows
public class LinkedList {
private Box head;
LinkedList(){
}
/* Add b as the last node of the linked list.*/
void addNode(Box b){
Box itr = head;
while(itr.next!=null)
itr = head.next;
itr.next = b;
}
/* Insert b in position pos. If insertion is successful * return true, otherwise return false. */
boolean insertNode(Box b, int pos){
int count=0;
Box itr = head;
while(itr.next!=null || count++<=pos){
itr = head.next;
}
if(itr==null)
return false;
else{
b.next = itr.next;
itr.next = b;
return true;
}
}
/**Print width, height, length, and volume of each of the boxes in * this linked list. */
void printAllBoxes(){
while(head.next!=null){
System.out.println(head.data);
head = head.next;
}
}
/** Remove the box in position pos. Return true if removal * is successful, otherwise return false.*/
boolean removeNode(int pos){
int count=0;
Box itr = head;
while(itr.next!=null || count++<=pos){
itr = head.next;
}
if(itr == null){
return false;
}
else{
itr.next = itr.next.next;
return true;
}
}
/** Return the box in position pos. Return null when pos is * invalid.*/
Box getBox(int pos){
int count=0;
Box itr = head;
while(itr.next!=null || count++<=pos){
itr = head.next;
}
if(itr == null){
return null;
}
else{
return itr;
}
}
Assuming that you already have created the Box class or the Node class which will represent the Individual node i the list.
In the above case the node class should have the of the File that you are trying to read which can be stored as a String.
public class Box {
String data;
Box next;
}
Now, In the Runner class you can intantiate the List with hard coded data to test the functions of the List.


