IN JAVAYou can represent an integer with any number of digit

***IN JAVA****You can represent an integer with any number of digits by storing the integer as a linked list of its digits. A more efficient representation will store a larger integer in each node. Design and implement a Java data type called UnboundedInteger for integer numbers in which a number is implemented as a linked list of integers. Each node will hold an integer less than or equal to 999. The number represented is the concatenation of the numbers in the nodes. For example, if there are four nodes with the four integers 23, 7, 999 and 0, then this represents the number 23,007,999,000. Note that the number in a node is always considered to be three digits long. If it is not three digits long, then leading zeros are added to make it three digits long. Include methods for the usual integer operators, including addition, subtraction, multipication and division, to work with your new class.

Solution

public class LinkedListNumber
{
   private class Node
   {
       Node next;
       int value;
   // empty constructor
       public Node()
       {
           next = null;
           value = -1;
       }
   // parametrized constructor
       public Node(int val, Node link)
       {
           next = link;
           value = val;
       }

       // tostring
       public String toString()
       {
           return \"\" + this.value;
       }
   }

   Node head;

  
   public LinkedListNumber()
   {
       head = null;
   }

   // copy constructor
   public LinkedListNumber(LinkedListNumber originalNode)
   {
       LinkedListNumber cloneList = new LinkedListNumber();
       if (originalNode.head == null)
       {
           this.head = null;
       }
       else
       {
           cloneList.head = new Node(originalNode.head.value, originalNode.head.next);
           Node tempval = cloneList.head.next;
           Node previous = cloneList.head;

           while(tempval != null)
           {
               Node newNode = new Node(tempval.value, tempval.next);
               previous.next = newNode;
               previous = newNode;
               tempval = tempval.next;
           }
          
           this.head = cloneList.head;

       }

   }

   // returns number of digits in linkedlist
   public int count()
   {
       if (head == null)
       {
           return 0;
       }
       else if (head.next == null)
       {
           return 1;
       }
       else
       {
           Node tempval = head;
           int count = 0;
           while (tempval != null)
           {
               count++;
               tempval = tempval.next;
           }
           return count;
       }
   }

   // check both lists are equal
   public boolean equals(LinkedListNumber num2)
   {
       if (this.count() != num2.count())
       {
           return false;
       }
       else if (this.head == null && num2.head == null)
       {
           return true;
       }
       else
       {
           Node link1 = this.head;
           Node link2 = num2.head;
           while (link1 != null)
           {
               if (link1.value != link2.value)
               {
                   return false;
               }
               link1 = link1.next;
               link2 = link2.next;
           }
           return true;
       }
   }

   //to string
   public String toString()
   {
       Node tempval = head;
       String s = \"\";
       while (tempval != null)
       {
           s += tempval.toString();
           tempval = tempval.next;
       }
       return s;
   }

   // adds left most value
   public void addMostSignificant(int val)
   {
       if( val < 0 || val > 9)
       {
           System.err.println(\"Invalid Digit Entry: \" + val +\" Please re-enter:\");
           System.exit(0); //Replace with Scanner
       }
       head = new Node(val, head);
   }

   // reset head value
   public void reset()
   {
       head = null;
   }

   public Object cloneList()
   {
       return new LinkedListNumber(this);
   }
  
   // adding two numbers
   public static LinkedListNumber add(LinkedListNumber num1, LinkedListNumber num2)
   {
       if (num1.count() != num2.count())
       {
           return null;
       }
       else
       {
           LinkedListNumber totalSum = new LinkedListNumber(num1);
           Node tempval = totalSum.head;
           Node tempval2 = num2.head;
          
           while(tempval != null)
           {
               tempval.value += tempval2.value;
               if (tempval.value > 9)
               {
                   tempval.value = 9;
               }
               tempval = tempval.next;
               tempval2 = tempval2.next;
           }
           return totalSum;
       }
   }

}

***IN JAVA****You can represent an integer with any number of digits by storing the integer as a linked list of its digits. A more efficient representation will
***IN JAVA****You can represent an integer with any number of digits by storing the integer as a linked list of its digits. A more efficient representation will
***IN JAVA****You can represent an integer with any number of digits by storing the integer as a linked list of its digits. A more efficient representation will
***IN JAVA****You can represent an integer with any number of digits by storing the integer as a linked list of its digits. A more efficient representation will

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site