Question

Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a...

Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a new polynomial. - It’s an instance method which called by a polynomial and takes second polynomial as the parameter pl. It should correctly multiply two polynomials together and returns this product polynomial.

//please see the given code below

public 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 head;
   public PolynomialLinkedlist(){
      head = null;
   }
   private void add(int c, int e){
      PNode tempn = new PNode(c, e, head);
      head = tempn;
   }
   public void removeDuplicate(){
      if (head == null) return;
      PNode lookUp, checkPrev;
      lookUp = checkPrev = head;
      while (lookUp.getNext() != null){
         while (checkPrev.getNext() != null){
             if (checkPrev.getNext().getExp() == lookUp.getExp()){//found a duplicate
                //lookUp.coe += checkPrev.next.coe;
                lookUp.setCoe(lookUp.getCoe() + checkPrev.getNext().getCoe());
                checkPrev.setNext(checkPrev.getNext().getNext());
             }
             else checkPrev = checkPrev.getNext();
         }
         lookUp = lookUp.getNext();
         checkPrev = lookUp;
      }              
   }
   public PolynomialLinkedlist add(PolynomialLinkedlist pl){
      PNode addTerm = this.head;
      PolynomialLinkedlist ans = new PolynomialLinkedlist();
      for (int i = 0; i < 2; i++){
         while (addTerm != null){
            ans.add(addTerm.getCoe(), addTerm.getExp());
            addTerm = addTerm.next;
         }
         addTerm = pl.head;
      }
      ans.removeDuplicate();
      return ans;        
   }
   public PolynomialLinkedlist multiply(PolynomialLinkedlist pl){
      PolynomialLinkedlist ans = new PolynomialLinkedlist();
      return ans;        
   }
   public void print(){
      if (head == null){
         System.out.println();
         return;
      }
      removeDuplicate();
      PNode temp = head;
      while (temp.getNext() != null){
         if (temp.getCoe() != 0){
            System.out.print("(" + temp.getCoe() + ")");
            if (temp.getExp() != 0){
               System.out.print("X^" + temp.exp);
            }
            System.out.print(" + ");
         }
         temp = temp.next;
      }
      if (temp != null){
         System.out.print(temp.coe);
         if (temp.exp != 0) System.out.print("X^" + temp.exp);
         System.out.println();
      }

   }
   public static void main(String argc[]){
      PolynomialLinkedlist pn1 = new PolynomialLinkedlist();
      PolynomialLinkedlist pn2 = new PolynomialLinkedlist();
      pn1.add(1, 2);
      pn1.add(2, 3);
      pn2.add(1, 0);
      pn2.add(5, 1);
      pn2.add(-6, 1);
      pn2.add(4, 2);
      System.out.print("Polynomial 1: ");
      pn1.print();
      System.out.print("Polynomial 2: ");
      pn2.print();
      PolynomialLinkedlist sum = pn1.add(pn2);
      PolynomialLinkedlist prod = pn1.multiply(pn2);
      System.out.print("Sum: ");
      sum.print();   
      System.out.print("Product: ");
      prod.print();   
   }
}

Homework Answers

Answer #1

Screenshot of the code that is to be added(rest the entire program is the same and untouched):

The Output Obtained:

The Code Used:

public 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 head;
    public PolynomialLinkedlist(){
        head = null;
    }
    private void add(int c, int e){
        PNode tempn = new PNode(c, e, head);
        head = tempn;
    }
    public void removeDuplicate(){
        if (head == null) return;
        PNode lookUp, checkPrev;
        lookUp = checkPrev = head;
        while (lookUp.getNext() != null){
            while (checkPrev.getNext() != null){
                if (checkPrev.getNext().getExp() == lookUp.getExp()){//found a duplicate
                    //lookUp.coe += checkPrev.next.coe;
                    lookUp.setCoe(lookUp.getCoe() + checkPrev.getNext().getCoe());
                    checkPrev.setNext(checkPrev.getNext().getNext());
                }
                else checkPrev = checkPrev.getNext();
            }
            lookUp = lookUp.getNext();
            checkPrev = lookUp;
        }
    }
    public PolynomialLinkedlist add(PolynomialLinkedlist pl){
        PNode addTerm = this.head;
        PolynomialLinkedlist ans = new PolynomialLinkedlist();
        for (int i = 0; i < 2; i++){
            while (addTerm != null){
                ans.add(addTerm.getCoe(), addTerm.getExp());
                addTerm = addTerm.next;
            }
            addTerm = pl.head;
        }
        ans.removeDuplicate();
        return ans;
    }
    public PolynomialLinkedlist multiply(PolynomialLinkedlist pl){
        PolynomialLinkedlist ans = new PolynomialLinkedlist();  //this will store our answer.
        PNode temp1 = this.head;  //this will be used to iterate through the first polynomial.
        while(temp1 != null)
        {
            PNode temp2 = pl.head; //this will be used to iterate through the second polynomial.
            //now when two polynomials are multiplied every term in the first polynomial is multiplied with every term with every other term
            //of the second polynomial. That's why we have used a nested loop.
            while(temp2!=null)
            {
                //Now when two independent terms of a polynomial are multiplied two things happens:
                //the coefficient gets multiplied.
                //and the exponents are added.
                ans.add(temp1.getCoe()*temp2.getCoe(),temp1.getExp()+temp2.getExp());
                temp2 = temp2.next;  //iterating through the second polynomial.
            }
            temp1 = temp1.next; //iterating through the first polynomial.
        }
        ans.removeDuplicate(); //removing the duplicates.
        return ans;
    }
    public void print(){
        if (head == null){
            System.out.println();
            return;
        }
        removeDuplicate();
        PNode temp = head;
        while (temp.getNext() != null){
            if (temp.getCoe() != 0){
                System.out.print("(" + temp.getCoe() + ")");
                if (temp.getExp() != 0){
                    System.out.print("X^" + temp.exp);
                }
                System.out.print(" + ");
            }
            temp = temp.next;
        }
        if (temp != null){
            System.out.print(temp.coe);
            if (temp.exp != 0) System.out.print("X^" + temp.exp);
            System.out.println();
        }

    }
    public static void main(String argc[]){
        PolynomialLinkedlist pn1 = new PolynomialLinkedlist();
        PolynomialLinkedlist pn2 = new PolynomialLinkedlist();
        pn1.add(1, 2);
        pn1.add(2, 3);
        pn2.add(1, 0);
        pn2.add(5, 1);
        pn2.add(-6, 1);
        pn2.add(4, 2);
        System.out.print("Polynomial 1: ");
        pn1.print();
        System.out.print("Polynomial 2: ");
        pn2.print();
        PolynomialLinkedlist sum = pn1.add(pn2);
        PolynomialLinkedlist prod = pn1.multiply(pn2);
        System.out.print("Sum: ");
        sum.print();
        System.out.print("Product: ");
        prod.print();
    }
}

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement Doubly Linked List get method which behaves like the operator[] of array. - It takes...
Implement Doubly Linked List get method which behaves like the operator[] of array. - It takes an integer parameter i as the index, it throw an Exception if the index i is illegal, returns the element at given index i, it traverse from the header of the list if index i is in the lower end of the list(less than half of the size), and traverse from the trailer of the list if index i is in the higher end...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
Hi, can you make me rotate method for this class just I need rotate method? this...
Hi, can you make me rotate method for this class just I need rotate method? this is Circularly Linked Lists code. public class Node {    int data;    Node next;    public Node(int data) {        this.data = data;    } } // this is implement class public class CircularLinkList {       public int size = 0;    public Node head = null;    public Node tail = null;       // add to the front of...
When I run the main method with the test case, it returns the list backwards. How...
When I run the main method with the test case, it returns the list backwards. How can I fix it so that it gives me all 6 correct actions? My guess is that the problem is in the getAction() function. public class StringDoublyLinkedList { /** * A private class to represent a link in the linked list. */ private class Node { String value; Node next; Node prev; Node(String value) { this.value = value; this.next = null; this.prev = null;...
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT