Question: I am working on a java linked list problem which asks me to deal with large integers, which the integer is too large to fit

I am working on a java linked list problem which asks me to deal with large integers, which the integer is too large to fit in primitive data types.

There are two java files. The first is LargeInteger.java which I have to work on. The second is LLNode.java which provides the linked list.

Here is LargeInteger.java:

package largeinteger;

import largeinteger.LLNode;

/** The LargeInteger class

* This class represents a large, non-negative integer using a linked list.

* Each node stores a single digit. The nodes represent all digits in *reverse* order:

* the least significant digit is the first node, and the most significant the last node.

* For example, 135642 is represented as 2->4->6->5->3->1 in that order.

*/

public class LargeInteger {

private LLNode head; // head of the list

private int size; // size (i.e. number of digits)

// Returns size

public int size() { return size; }

// Returns the linked list (used only for JUnit test purpose)

public LLNode getList() { return head; }

public LargeInteger() {

head = null; size = 0;

}

/** Constructor that takes a String as input and constructs the linked list.

* You can assume that the input is guaranteed to be valid: i.e. every character

* in the string is between '0' and '9', and the first character is never '0'

* (unless '0' is the only character in the string). You can use input.charAt(i)-'0'

* to convert the character at index i to the integer value of that digit.

* Remember: the list nodes must be in reverse order as the characters in the string.

*/

public LargeInteger(String input) {

// TODO

}

/** Divide *this* large integer by 10 and return this.

* Assume integer division: for example, 23/10 = 2, 8/10 = 0 and so on.

*/

public LargeInteger divide10() {

// TODO

return null;

}

/** Multiply *this* large integer by 10 and return this.

* For example, 23*10 = 230, 0*10 = 0 etc.

*/

public LargeInteger multiply10() {

// TODO

return null;

}

/** Returns a *new* LargeInteger object representing the sum of this large integer

* and another one (given by that). Your code must correctly handle cases such as

* the two input integers have different sizes (e.g. 2+1000=1002), or there is a

* carry over at the highest digit (e.g. 9999+2=10001).

*/

public LargeInteger add(LargeInteger that) {

// TODO

return null;

}

/** Returns a new LargeInteger object representing the result of multiplying

* this large integer with a non-negative integer x. You can assume x is either

* a positive integer or 0. Hint: you can use a loop and call the 'add' method

* above to accomplish the 'multiply'.

*/

public LargeInteger multiply(int x) {

// TODO

return null;

}

/** Recursive method that converts the list referenced by curr_node back to

* a string representing the integer. Think about what's the base case and

* what it should return. Then think about what it should return in non-base case.

* Hint: refer to the 'printing a list backwards' example we covered in lectures.

*/

private String toString(LLNode curr_node) {

// TODO

return null;

}

/** Convert this list back to a string representing the large integer.

* This is a public method that jump-starts the call to the recursive method above.

*/

public String toString() {

return toString(head);

}

// Recursive method to compute factorial

public static LargeInteger factorial(int n) {

if(n==0) return new LargeInteger("1");

return factorial(n-1).multiply(n);

}

// Recursive method to compute power

public static LargeInteger pow(int x, int y) {

if(y==0) return new LargeInteger("1");

return pow(x, y-1).multiply(x);

}

}

Here is LLNode.java:

package largeinteger;

/** Generic LLNode class

* Note that both data members are public

* so you can access them directly without

* having to use the getters and setters.

*/

public class LLNode {

public T data;

public LLNode link;

public LLNode() {

this(null, null);

}

public LLNode(T data, LLNode link) {

this.data = data;

this.link = link;

}

}

Please help me on this one and I will rate.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!