Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Multiplication of two polynomials using linked list in java

Have Programming Question? Ask it here!
 
 
 


Rajesh
Multiplication of two polynomials using linked list in java
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners


I am doing a program "Multiplication of two polynomials using linked list in java" for our curriculum, can you please help me in this.

Rajesh


Advertisement
View Answers

January 26, 2012 at 10:20 AM


//Class for polynomial linked list
import java.util.Scanner;

class Node {
public int numC;
public int numE;
public Node next;

// elementary constructor
public Node() {
numC = 0;
numE = 0;
next = null;
}

// constructor with two argument
public Node(int c, int e) {
numC = c;
numE = e;
next = null;
}

// primary constructor
public Node(int c, int e, Node n) {
numC = c;
numE = e;
next = n;
}

// accessor method
public int getCo() {
return numC;
}

public int getEx(){
return numE;
}

// accessor method
public Node getNext() {
return next;
}

// mutator method
public void setCof(int c){
numC = c;
}

public void setExp(int e){
numE = e;
}
// mutator method for next
public void setNext(Node nextNode) {
next = nextNode;
}
} // end of class Node



class Polynomial {
private Node front;
public Polynomial(){
front = null;
}

//read coefficient fx
public int readCoFx(){
Scanner scan = new Scanner(System.in);
int coef = 0;

System.out.print("Enter Coefficient or 0 to skip to next equation: ");
coef = scan.nextInt();
return coef;
}
//read coefficient gx
public int readCoGx(){
Scanner scan = new Scanner(System.in);
int coef = 0;

System.out.print("Enter Coefficient or 0 to end program: ");
coef = scan.nextInt();
return coef;
}
//read exponent
public int readEx(){
Scanner scan = new Scanner(System.in);
int exp = 0;

System.out.print("Enter Exponent: ");
exp = scan.nextInt();
return exp;
}

//insert Node
public void insert(int coef, int exp){
    Node temp = new Node(coef, exp, null);
    //insert temp to 1st if empty
    if (front == null)
        front = temp;
    else {

        Node p = null;
        Node q = front;
    //insert in front if exponent is higher
        if (temp.getEx() > q.getEx()){
            temp.setNext(front);
            front = temp;
        } else 
            {//insert at middle or end
    while (q != null && q.getEx() > temp.getEx()){

        p = q;
        q = q.getNext();
       }

    p.setNext(temp);
    temp.setNext(q);

    }
    }
}
public void mulInsert(int coef, int exp){
    //Node temp = new Node(coef, exp, null);
    //insert temp to 1st if empty
        System.out.println("coef="+coef+"exp="+exp);
        if (front == null)
                front = new Node(coef,exp,null);
        else {

                    Node p = null;
                    Node q = front;
        //insert in front if exponent is higher
                if (exp > q.getEx()){
                        Node temp = new Node(coef,exp,null);
                        temp.setNext(front);
                        front = temp;
                } else 
                    {//insert at middle or end
                        while (q != null && q.getEx() >= exp){
                            if (q.getEx() == exp)
                            {
                                q.numC += coef;
                                q=q.getNext();
                                continue;
                            }
                            p = q;
                            q = q.getNext();
                        }

                   Node temp = new Node(coef,exp,null);
                    p.setNext(temp);
                    temp.setNext(q);

                }
    }

}

//print function for the equations
public void print(){
Node p = front;
while(p != null){
System.out.print("("+p.getCo()+ "x" + p.getEx()+")");
p = p.getNext();
}
System.out.println("");
}


//function to add polynomials
public static Polynomial addPoly(Polynomial poly1, Polynomial poly2){
Polynomial tempPoly = new Polynomial();
//if poly1 exp is > poly 2 exp then power = poly 1 exp else power = poly 2 exp
int power = (poly1.front.getEx() > poly2.front.getEx()) ? poly1.front.getEx() : poly2.front.getEx();

while (power >=0){
    Node n1 = poly1.front;
    while (n1 != null){
        if (n1.getEx() == power)
            break;
        n1 = n1.getNext();
    }

    Node n2 = poly2.front;
    while (n2 != null){
    if (n2.getEx() == power)
        break;
    n2 = n2.getNext();
    }

    if((n1 != null) && (n2 != null))
        tempPoly.insert((n1.getCo() + n2.getCo()), n1.getEx());
    else if (n1 != null)
        tempPoly.insert(n1.getCo(), n1.getEx());
    else if (n2 != null)
        tempPoly.insert(n2.getCo(), n2.getEx());
    power--;
}
return tempPoly;
}

public static Polynomial mulPoly(Polynomial poly1, Polynomial poly2){
Polynomial tempPoly = new Polynomial();

Node n1 = poly1.front;


while (n1 != null ){
Node n2 = poly2.front;
while(n2 != null){
        tempPoly.mulInsert((n1.getCo()*n2.getCo()),(n1.getEx() + n2.getEx()));
    n2 = n2.getNext();
}
n1= n1.getNext();
}
return tempPoly;
}
}

//driver
public class PolyDrive {
public static void main(String [] args){
Polynomial poly1 = new Polynomial();
Polynomial poly2 = new Polynomial();
Polynomial poly3 = new Polynomial();
Polynomial poly4 = new Polynomial();
int co;
int ex;

do {
co = poly1.readCoFx();
if(co != 0){
ex = poly1.readEx();
poly1.insert(co, ex);
}
//poly1.print();
} while (co != 0);

do {
co = poly2.readCoGx();
if(co != 0){
ex = poly1.readEx();
poly2.insert(co, ex);
}
//poly2.print();
} while (co != 0);
poly1.print();
poly2.print();
//poly3 = Polynomial.addPoly(poly1,poly2);
//poly3.print();
poly4 = Polynomial.mulPoly(poly1,poly2);
poly4.print();
}
}


Related Tutorials/Questions & Answers:
Multiplication of two polynomials using linked list in java
Multiplication of two polynomials using linked list in java  I am doing a program "Multiplication of two polynomials using linked list in java... for polynomial linked list import java.util.Scanner; class Node { public int
stack using linked list
stack using linked list  how to implement stack using linked list
Advertisements
write a java pogram to add elements to linked list using keyboard and display it
write a java pogram to add elements to linked list using keyboard and display it  write a java pogram to add elements to linked list using keyboard and display
Core java linked list example
Core java linked list example  What is the real time example for linked list
How to create a Student data base using Linked List in java
How to create a Student data base using Linked List in java  I want a program by using linked list in java. The data stored must... Record2 sandhya 22 5apr By using linked list I hve
linked list in java - Java Beginners
linked list in java  Hi, how to implement linked list in java using... information. http://www.roseindia.net/java/beginners/linked-list-demo.shtml...(); for (int i = 0; i < N; i++) { // look up a value in the list // using
Linked list
Linked list  what is difference btw linked list in datastructure and linked list in java
linked list
linked list  program for single linked list
linked list
://www.roseindia.net/java/beginners/linked-list-demo.shtml...linked list  Hi i have a problem with linked list ! how and where i can use linked list? please give me some example.   Please visit
linked list
linked list  hi i have basal problem what is the linked list
Linked List
in the following logical linked list represents Computer Number. 76(head) 98 54...? to ?66?. The segment should also display the new contents of the linked list. ii. Write a program segment to copy all ?Dell? computers to a new linked list
creating java linked list - Java Beginners
creating java linked list  how can one create a sorted linked list...("Size of list: " + queue.size()); System.out.println("Queue head using peek : " + queue.peek()); System.out.println("Queue head using element
linked list
linked list   how to write a program using a linked list...); System.out.println("Add elements to LinkedList: "); LinkedList<String> list=new...); } Collections.reverse(list); System.out.println("List of names in reverse order
linked list example problem - Java Beginners
linked list example problem  Q: Create your own linked list (do... by repeatedly using your add function to populate a new instance of your linked list... two operations If you are using jdk1.4 or before
linked list example problem - Java Beginners
linked list example problem  Q: Create your own linked list (do... by repeatedly using your add function to populate a new instance of your linked list... two operations If you are using jdk1.4 or before
linked list
linked list   how to add student and mark and number from file in linked list and print them also how to make search function plz can help me sooon
Queue implementation using linked list.
Description: The advantage of using linked list is that there is no size limit. The size of queue grow and shrink as per insertion and deletion takes place. Code: # include <stdio.h> # include <stdlib.h> struct node
delete a node from singly linked list in java
delete a node from singly linked list in java  Write a program(in java), if given a pointer to a node (not the tail node) in a singly linked list, delete that node from the linked list.   could you tell your question
linked list
program to manage the registration details for the institute. 1. Use a linked list to manage the details of all registered students. a. Create your own linked list...linked list  Data Structures An English institute has a different
Concatenate two linked lists
Concatenate two linked lists  hello, How to concatenate two linked lists?   hii, You can change null pointer of the first linked list to point the header of the second linked list
code for multiplication of matrix in java using methods
code for multiplication of matrix in java using methods  code for multiplication of matrix in java using methods
Linked list implementation
Linked list implementation  How to create linkedlist by using array in java? and also How to manipulate
HELP Generic linked list
HELP Generic linked list  How to create Generic linked list example program in Java
Explain Linked List
Explain Linked List   hello, What is Linked List ?   hello, Linked List is one of the fundamental data structures. It consists of a sequence of nodes, each containing arbitrary data fields pointing to the next
How to read bytes from a Linked list - Java Beginners
How to read bytes from a Linked list  i have stored byte array into a linked list. How to read the bytearray dta byte after byte from a linked list.Thanking u in advance Sameer
two linked combobox
two linked combobox  give jsp example of two combo box when i select state in one combobox in second combo box cities will display according to state which i select
Create a deque linked list of Entry Objects - Java Beginners
Create a deque linked list of Entry Objects  I need to know if I have successfully created a Square Matrix List of Entry Objects. The detail desciption and my java code solution follows: A) Description 1) The List ADT
about linked list
about linked list   hey can u help me soon ?? i want to add student... in linked list   import java.util.*; class StudentData{ int id...; LinkedList<StudentData> list=new LinkedList<StudentData>(); Scanner
Overview of Linked List
Linked List A linked list is a data structure which consists of data record...-defined. There are mainly three kinds of linked lists: Singly linked list Doubly linked list Circular linked list.      
Linked List Example In Java
Linked List Example In Java LikedList implements List interface which... list. LinkedList(Collection c) : Create a linked list which initialize... manipulate and access the element. Some of the element of linked list
Simple Linked List Exercise 1
Java Notes: Simple Linked List Exercise 1 Name... and puts them in a doubly linked list. 1 2 3 4 5 6 7 8... of list. Elem2 back = __________; // Last element of list
Write a MoveFirst() for Circular Linked List
Write a MoveFirst() for Circular Linked List  write a MoveFirst(T elt... to the element before that one. so if i have " one two three four) and the moveFirst(two); it should read "two three four one
Linked List Example
of List interface. We are using two classes ArrayList and LinkedList... Linked List Example     ... LinkedListExample Linked List Example! Linked list data: 11 22 33 44 Linked
Circular Linked List
Description: In the circular linked list the link of lat node is connected to the first node. Code: # include <stdio.h> # include <stdlib.h>... = p; printf("The data in the list are :\n"); if (p!= NULL
Issue with tutorial realted to insertion in middle in Linked List
Issue with tutorial realted to insertion in middle in Linked List  insert in middle in Linked list will take O(N) instead as displayed as O(1
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
multiplication algorithms in java divide and conquer
multiplication algorithms in java divide and conquer  I need multiplication algorithms in java divide and conquer ask from user input two numbers in binary then the program multiply two number use multiplication algorithm in java
Singly Linked List
data record in singly link list.  Code: # include <stdio.h>... ) { printf("The data values of your list are\n"); while (p!= NULL...;The created single link list is\n"); printlist ( start ); } Output
Doubly Linked List
*p , struct dnode **q, int n) { struct dnode *temp; if(p==NULL) { p... = p->next =NULL; *q =p; } else { temp = (struct dnode *)malloc...\n"); exit(0); } temp->data = n; temp->prev = (*q
java program_big digits multiplication..
java program_big digits multiplication..  i want program about big digits multiplication program using java..plz tel me answer
Matrix multiplication in java
Matrix multiplication in java In this section we will learn about multiplication of two matrices. In java this is a simple program to multiply two matrices... two-dimensional array. Here we are going to develop a Java code for matrices
Parallel multiplication matrix in java
Parallel multiplication matrix in java  hello dear I need parallel multiply matrix in java algorithm to account speed up and efficiency great wishes
Multiplication problem - Java Beginners
Multiplication problem  I am facing a peculiar problem in java regarding a multiplication. Please see below: 19300 * 0.001 = 19.3 19400 * 0.001 = 19.400000000000002 (why is this ??) 19500 * 0.001 = 19.5 Can anybody help
multiplication
multiplication  multiplication of two number numbers entered by user minimum 100digits using GUI   Java Multiplication of two 100 digits number import java.awt.*; import java.math.*; import javax.swing.*; import
multiplication
multiplication  multiplication of two number numbers entered by user minimum 100digits using GUI   Java Multiplication of two 100 digits number import java.awt.*; import java.math.*; import javax.swing.*; import
java string multiplication
java string multiplication  Is there any simple method of string multiplication in java ..?   public class StringMultiplication { public static void main(String[] args) { String number1 = "17"; String number2
Multiplication of two Matrix
Multiplication of Two Matrix       This is a simple Java multidimensional array program.... The Java two dimensional array  program is operate to the two matrix number
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
 

 

 

DMCA.com