Question: please explain the code in details please. } if (c1 > 0) answer.AddTail(c1); return answer; } public LinkedList addMultiply(LinkedList list1, LinkedList list2){ Node f1 =
please explain the code in details please.




} if (c1 > 0) answer.AddTail(c1); return answer; } public LinkedList addMultiply(LinkedList list1, LinkedList list2){ Node f1 = list1.head; Node s2 = list2.head; LinkedList answer = new LinkedList(); int currentRes; int c1 = 0; int lastDigit;
while(f1!=null || s2!=null){ if (f1==null){ Node nmp = new Node(0); f1 = nmp; } if (s2==null) { Node nmp = new Node(0); s2 = nmp; }
currentRes = f1.value + s2.value + c1; c1 = currentRes / 10; lastDigit = currentRes % 10;
answer.AddTail(lastDigit); Node t = new Node(lastDigit); t.next = head; head = t;
f1 = f1.next; s2 = s2.next; } if (c1 > 0) { answer.AddTail(c1); Node t = new Node(c1); t.next = head; head = t; } return answer; }
public void multiplyTwoLists(LinkedList list1, LinkedList list2){ Node f1 = list1.head; Node s2 = list2.head;
int addZero = 0; LinkedList answer = new LinkedList(); LinkedList t = new LinkedList(); answer.AddHead(0);
Node h1 = f1; Node h2 = s2; while(h2!=null){ LinkedList nmp = new LinkedList(); nmp = singleMultiply(h2.value, list1); for (int i = 0; i
h2 = h2.next; } Node HeadAns = answer.head; while(HeadAns!=null){ Node t1 = new Node(HeadAns.value); t1.next = head; head = t1;
HeadAns = HeadAns.next; }
Node hZeros = answer.head; int c = 0; while(hZeros!=null){ if (hZeros.value == 0) c++; hZeros = hZeros.next; }
if (c == answer.Length()){ DeleteAll(); AddHead(0); } } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("to quit press Exit"); while(true){
LinkedList n1L = new LinkedList(); LinkedList n2L = new LinkedList(); LinkedList output = new LinkedList(); String n1s, n2s, method; System.out.print("Enter the first number: "); n1s = in.next(); if (n1s.equalsIgnoreCase("exit")){ System.exit(0); } System.out.print("Enter the second number: "); n2s = in.next(); if (n2s.equalsIgnoreCase("exit")){ System.exit(0); } n1L.ChangeToNum(n1s); n2L.ChangeToNum(n2s); System.out.print("choose between (+, -, *) : "); method = in.next();
if(method.equals("+")){ System.out.print("output: "); output.add(n1L, n2L); output.Print(); System.out.println(""); } else if (method.equals("-")){ System.out.print("output: "); output.subtract(n1L, n2L); output.Print(); System.out.println(""); } else if (method.equals("*")){ System.out.print("output: "); output.multiplyTwoLists(n1L, n2L); output.Print(); System.out.println(""); } else if (method.equalsIgnoreCase("exit")){ System.exit(0); } else { System.out.println("this input is not allowed."); System.out.println(""); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
