Question
CODES IN JAVA 3.Implement Polynomial Linked List add method which adds two polynomials in standard form and returns sum polynomial which also has to be
CODES IN JAVA
3.Implement Polynomial Linked List add method which adds two polynomials in standard form and returns sum polynomial which also has to be in standard form.-Rewrite the instance method by merging the caller and parameterPolynomial Linked Listinto another sum Polynomial. Both caller and parameter Polynomials are expected to be in standard form, the method should return the sum Polynomial also in standard form. A standard form polynomial has no duplicate terms (each terms exponent is unique) and the exponent is in descending order. This method should be O(n), n as the number of terms in the sum polynomial.
4.Implement Polynomial Linked list multiply method, similar to add, it returns the result polynomial in standard form.
GIVEN CODES : FILL UP THE MISSING CODES
/** * * Student: * */ public class A1LinkedList{ public static void main(String argc[]){ LinkedListsl = new LinkedList<>(); DLinkedList dl = new DLinkedList<>(); PolynomialLinkedList sum = new PolynomialLinkedList(); PolynomialLinkedList prod = new PolynomialLinkedList(); for (int i = 40; i >= -40; i-=2) { sl.add(i*i); dl.add(i); } try { sl.insert(111, sl.getNode(38), sl.getNode(40)); if (sl.detectLoop()) System.out.println("Loop! Wrong"); else System.out.println("No loop."); sl.insert(123, sl.getNode(38), sl.getNode(36)); if (sl.detectLoop()) System.out.println("Loop!"); else System.out.println("No loop. Wrong"); sl.insert(321, sl.getNode(38), sl.getNode(37)); if (sl.detectLoop()) System.out.println("Loop!"); else System.out.println("No loop. Wrong"); dl.print(); System.out.println("Index 20, has 0? " + dl.get(20)); System.out.println("Index 21, has -2? " + dl.get(21)); } catch(Exception e){ e.printStackTrace(); } PolynomialLinkedList p1, p2, p3, p4; p1 = new PolynomialLinkedList(2,3); p2 = new PolynomialLinkedList(3,2); p3 = p1.add(p2); p1 = new PolynomialLinkedList(1,0); p4 = p2.add(p1); sum = p3.add(p4); // extra credit //prod = p3.multiply(p4); p3.print(); p4.print(); sum.print(); //prod.print(); } } class LinkedList { private static class Node { private E element; private Node next; public Node(E e, Node n){ element = e; next = n; } public E getElement(){ return element; } public Node getNext(){ return next; } public void setElement(E e){ element = e; } public void setNext(Node n){ next = n; } } private Node head; public LinkedList(){ head = null; } public void add(E e){ Node temp = new Node<>(e, head); head = temp; } public void insert(E e, Node p, Node n){ p.setNext(new Node<>(e, n)); } public Node getNode(int i) throws Exception{ Node temp = head; while (i > 0){ if (temp == null) throw new Exception("Out of bound"); temp = temp.getNext(); i--; } return temp; } public boolean detectLoop(){ //implement this method return true; } } class DLinkedList { private static class DNode { private E element; private DNode prev; private DNode next; public DNode(E e){ this(e, null, null); } public DNode(E e, DNode p, DNode n){ element = e; prev = p; next = n; } public E getE(){ return element; } public DNode getPrev(){ return prev; } public DNode getNext(){ return next; } public void setE(E e){ element = e; } public void setPrev(DNode p){ prev = p; } public void setNext(DNode n){ next = n; } } private DNode header; private DNode trailer; private int size; public DLinkedList(){ header = new DNode (null); trailer = new DNode (null, header, null); header.setNext(trailer); size = 0; } public void add(E e){ DNode oLast = trailer.getPrev(); DNode temp = new DNode<>(e, oLast, trailer); oLast.setNext(temp); trailer.setPrev(temp); size++; } public E get(int i) throws Exception{ if (i < 0 || i >= size) throw new Exception("Bad Index"); DNode ansNode; if (i < size / 2){//lower end ansNode = header.getNext(); //loop - traversal from header while (i > 0){ ansNode = ansNode.getNext(); i--; } } //complete this method return null; } public void print(){ DNode temp = header.getNext(); while (temp != trailer){ System.out.print(temp.getE().toString() + ", "); temp = temp.getNext(); } System.out.println(); } } class PolynomialLinkedList{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public PNode(int c, int e, PNode n){ coe = c; exp = e; next = n; } public void setCoe(int c){ coe = c;} public void setExp(int e){ exp = e;} public void setNext(PNode n){ next = n;} public int getCoe(){ return coe;} public int getExp(){ return exp;} public PNode getNext(){ return next;} } private PNode first; private PNode last; public PolynomialLinkedList(){ first = last = null; } public PolynomialLinkedList(int c, int e){ PNode tempn = new PNode(c, e); first = last = tempn; } public void print(){ if (first == null){ System.out.println(); return; } PNode temp = first; String ans = ""; while (temp != null){ if (temp.getCoe() > 0) { if (temp != first) ans = ans + " + "; ans = ans + temp.getCoe(); } else if (temp.getCoe() < 0) ans = ans + " - " + temp.getCoe() * -1; if (temp.getExp() != 0){ ans = ans + "X^" + temp.getExp(); } temp = temp.getNext(); } System.out.println(ans); } public PolynomialLinkedList add(PolynomialLinkedList s){ PolynomialLinkedList sum = new PolynomialLinkedList(); //implement this method **********************************MISSING CODES**********HERE**************** return sum; } public PolynomialLinkedList multiply(PolynomialLinkedList s){ PolynomialLinkedList product = new PolynomialLinkedList(); //implement this method for extra credit return product; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started