gpt-4-0314· openai
Suzhen/CodeChat
java code for add,substruction and multiple two ploynomail use doublelinkedlist ask the…
- User
java code for add,substruction and multiple two ploynomail use doublelinkedlist ask the user to enter the two ploynomial
- Assistant
Below is the Java code to add, subtract and multiply two polynomials using a DoubleLinkedList. It prompts the user to enter the two polynomials.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first polynomial (space separated): "); String polynomial1Str = scanner.nextLine(); System.out.print("Enter the second polynomial (space separated): "); String polynomial2Str = scanner.nextLine(); scanner.close(); DoubleLinkedList polynomial1 = createPolynomialList(polynomial1Str); DoubleLinkedList polynomial2 = createPolynomialList(polynomial2Str); DoubleLinkedList sum = polynomial1.add(polynomial2); DoubleLinkedList difference = polynomial1.subtract(polynomial2); DoubleLinkedList multiplication = polynomial1.multiply(polynomial2); System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); System.out.println("Multiplication: " + multiplication); } private static DoubleLinkedList createPolynomialList(String polynomialStr) { DoubleLinkedList polynomialList = new DoubleLinkedList(); String[] polynomialArr = polynomialStr.split(" "); for (String coeff : polynomialArr) { polynomialList.add(Integer.parseInt(coeff)); } return polynomialList; } } class DoubleLinkedList { Node head, tail; static class Node { int data; Node prev, next; Node(int data) { this.data = data; this.prev = this.next = null; } } public void add(int data) { Node newNode = new Node(data); if (head == null) { head = tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } public DoubleLinkedList add(DoubleLinkedList other) { DoubleLinkedList result = new DoubleLinkedList(); Node ptr1 = this.head; Node ptr2 = other.head; while (ptr1 != null && ptr2 != null) { result.add(ptr1.data + ptr2.data); ptr1 = ptr1.next; ptr2 = ptr2.next; } while (ptr1 != null) { result.add(ptr1.data); ptr1 = ptr1.next; } while (ptr2 != null) { result.add(ptr2.data); ptr2 = ptr2.next; } return result; } public DoubleLinkedList subtract(DoubleLinkedList other) { DoubleLinkedList result = new DoubleLinkedList(); Node ptr1 = this.head; Node ptr2 = other.head; while (ptr1 != null && ptr2 != null) { result.add(ptr1.data - ptr2.data); ptr1 = ptr1.next; ptr2 = ptr2.next; } while (ptr1 != null) { result.add(ptr1.data); ptr1 = ptr1.next; } while (ptr2 != null) { result.add(-ptr2.data); ptr2 = ptr2.next; } return result; } public DoubleLinkedList multiply(DoubleLinkedList other) { DoubleLinkedList result = new DoubleLinkedList(); Node ptr1 = this.head; int index1 = 0; while (ptr1 != null) { Node ptr2 = other.head; int index2 = 0; while (ptr2 != null) { result.addCoefficient(index1 + index2, ptr1.data * ptr2.data); ptr2 = ptr2.next; index2++; } ptr1 = ptr1.next; index1++; } return result; } private void addCoefficient(int index, int coefficient) { Node ptr = head; for (int i = 0; i < index; i++) { if (ptr == null) { add(0); ptr = tail; } else{ ptr = ptr.next; } } if (ptr == null) { add(coefficient); } else { ptr.data += coefficient; } } @Override public String toString() { StringBuilder sb = new StringBuilder(); Node ptr = head; while (ptr != null) { sb.append(ptr.data).append(" "); ptr = ptr.next; } return sb.toString().trim(); } }To run the code, copy and paste everything into a file called
Main.javaand then compile and run it using thejavacandjavacommands.Example input and output:
Enter the first polynomial (space separated): 3 2 5 Enter the second polynomial (space separated): 5 1 2 Sum: 8 3 7 Difference: -2 1 3 Multiplication: 15 13 29 9 10