Question: LinkedLinkst.java file : need help with this. import java.util.*; import java.io.*; public class LinkedList{ int n; ListNode start; ListNode rear; public LinkedList(){ n= 0; start=
LinkedLinkst.java file : need help with this.
import java.util.*;
import java.io.*;
public class LinkedList{
int n;
ListNode start;
ListNode rear;
public LinkedList(){
n= 0;
start= null;
rear= null;
}
public LinkedList(int size, ListNode first, ListNode last){
n= size;
start= first;
rear= last;
}
/*This routine reads in one big integer and returns its linked list representation.
The readBigInteger method should return null if there is no further input remaining.
The way that a big integer is given for input is that first, an integer n_digit
representing the number of digits is specified followed by n_digit digits
which should be in the range 0 to 9. For example, the integer 2385 is input as
4 2 3 8 5*/
public static LinkedList readBigInteger(Scanner in){
LinkedList x;
// Question 1 code goes here.
x= new LinkedList(); // You can move this statement.
return(x);
}
/*The printBigInteger method should reverse the list using the reverse method,
then traverse the list to print the big integer
without leading zeroes and without spaces between the digits,
and then should restore the original list by calling reverse again.*/
public void printBigInteger(){
// Question 2 code goes here.
}
/*The reverse must operate recursively. First the list should be split into two lists,
one with the first floor of n/2 entries and the other with the rest. You can use two new LinkedList
objects each time you split the list in half but do not make any new ListNode objects. Then each of
these lists should be reversed recursively. Then concatenate together the answers appropriately to
get the final answer. The variable level is for debugging and represents the level of the recursive call.
The initial call should be at level 0. If reverse is called from level k then the resulting invocation is
at level k+1.*/
public void reverse(int level){
Test.checkList(this); // Do not remove or move this statement.
// Question 3 code goes here.
}
/*This method operates in-place and has the effect of adding one to the current integer value.
You should not create any new list nodes except in the case that one more list node is required
to represent the answer. For example, if 99 is incremented, the original list has two cells, each
with a digit value of 9. After the increment, the data values are changed to 0 in the first two
cells and a new cell with data value 1 is added to the end of the list.*/
public void plus_plus(){
// Question 4 code goes here.
}
/*If x, y, and z are LinkedList objects representing big integers, then calling this routine like this:
z = x.plus(y)
sets z to be big integer which is the sum of the big integers stored in x and y.*/
public LinkedList plus(LinkedList y){
LinkedList z;
z= new LinkedList();
// Question 5 code goes here.
return(z);
}
// You can use these routines for this assignment:
// Tries to read in a non-negative integer from the input stream.
// If it succeeds, the integer read in is returned.
// Otherwise the method returns -1.
public static int readInteger(Scanner in){
int n;
try{
n= in.nextInt();
if (n >=0) return(n);
else return(-1);
}
catch(Exception e)
{
// We are assuming legal integer input values are >= zero.
return(-1);
}
}
// Use this for debugging only.
public void printList(){
ListNode current;
int count=0;
current= start;
while (current != null){
count++;
System.out.println("Item " + count + " in the list is "
+ current.data);
current= current.next;
}
}
}
Test.java File: This class has a checkList routine- fill in code here to check the integrity of your lists (n, start and rear) to aid in debugging
import java.util.*; import java.io.*;
// It is up to the student to ensure // that the input is read in properly and that their program // computes the correct results. public class Test{ // Insert code here to test the integrity of a list // (the values for n, start and rear should be correct). // I would normally place checkList in the LinkedList class // but we put it here so we can replace your code with // our own when doing the automated testing. static void checkList(LinkedList x) { // Insert your code here. // Print an error message and stop if there is anything // wrong with the list. } public static void main(String args[]) { int n_times; int xv; LinkedList x, y, z; int problem_number; int i; // First test the increment method.
x= new LinkedList(); x.start= new ListNode(0, null); x.rear= x.start; x.n=1; checkList(x); xv=0; System.out.println("Test the increment method:"); for (n_times= 1; n_times <= 10000; n_times*= 10 ) { test_plus_plus(n_times, xv, x); xv+= n_times; }
// Now test the addition method.
problem_number=0; Scanner in = new Scanner(System.in);
x= LinkedList.readBigInteger(in); while (x != null) { checkList(x); problem_number++;
y= LinkedList.readBigInteger(in); if (y == null) { throw new RuntimeException("Error- failed to read in second Big Integer y"); } checkList(y);
z= x.plus(y); checkList(z);
System.out.println("Problem " + problem_number + ":"); x.printBigInteger(); System.out.print(" + "); y.printBigInteger(); System.out.print(" = "); z.printBigInteger(); System.out.println();
checkList(x); checkList(y); checkList(z); x= LinkedList.readBigInteger(in); } } public static void test_plus_plus(int num_times, int xv, LinkedList x) { int i; xv+= num_times; for (i=0; i < num_times; i++) { x.plus_plus(); checkList(x); } System.out.print("Value should be " + xv + " is "); x.printBigInteger(); System.out.println(); } }
ListNode.java file:
class ListNode{ public int data; public ListNode next; public ListNode(int x, ListNode ptr){ data= x; next= ptr; } }
This is a text file called itest.txt: This is one file of inputs you can use to test your final submission, java Test < itest.txt > otest.txt
1 3 1 4 1 9 1 9 1 0 1 0 3 8 5 9 3 7 2 3 5 9 9 9 9 9 8 8 8 8 8 8 8 8 8 6 0 0 0 0 0 1 4 0 0 9 9 5 1 2 3 4 5 9 1 2 3 4 5 6 7 8 9 16 9 8 7 6 5 4 3 2 9 8 7 6 5 4 3 2 24 9 8 7 6 5 4 3 2 9 8 7 6 5 4 3 2 9 8 7 6 5 4 3 2 16 9 8 7 6 5 4 3 2 9 8 7 6 5 4 3 2 16 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 16 9 8 7 6 5 4 3 1 9 8 7 6 5 4 3 2 16 1 0 0 0 0 0 0 0 0 1 2 3 4 5 6 8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
