Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:58:10+00:00 2026-06-12T17:58:10+00:00

I am trying to create a data structure to hold polynomials. So far i

  • 0

I am trying to create a data structure to hold polynomials. So far i have created a Node class to store the data like so : (Coefficient, exponent, link to next node)
I am running into problems when trying to add two lists together though. My method adds the first values in each of the lists but then terminates.

here is my code below:

    public class Node{

//Fields
public int data1;
public int data2;
public Node next;



//constructors
public Node(){
    data1 = 0;
    data2 = 0;
    next = null;
}


public Node(int d1){
    data1 = d1;
    data2 = 0;
    next = null;
}

public Node(int d1, Node n){
    data1 = d1;
    data2 = 0;
    next = n;
}

public Node(int d1, int d2){
    data1 = d1;
    data2 = d2;
    next = null;
}
public Node(int d1,int d2, Node n){
    data1 = d1;
    data2 = d2;
    next = n;
}

//Methods

//Fetch data
public int getData1(){
    return data1;
}

public int getData2(){
    return data2;
}

//store Data
public void setData1(int d){
    data1 = d;
}

public void setData2(int d){
    data2 = d;
}

public void addData1(Node n2){
    data1 = data1 + n2.data1;
}

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

//Get data of next node
public int getNextData1(){
    return next.data1;
}

public int getNextData2(){
    return next.data2;
}

//Store Link
public void setNext(Node n){
    next = n;
}

public boolean containsLink(){
    if(this.next != null){
        return true;
    }else{
        return false;
    }
}

public static void displayAll(Node head){
    for( ; head != null; head = head.next ){
        System.out.printf("%d, %d\n", head.data1, head.data2);
    }
}

public static int numOfNonZeroData1(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data1 != 0){
            count++;
        }
    }
    return count;
}

public static int numOfNonZeroData2(Node n){
    int count = 0; 
    for( ; n != null ; n = n.next ){
        if(n.data2 != 0){
            count++;
        }
    }
    return count;
}

public static int listLength(Node head){
    int counter = 0;
    for(; head!=null; head = head.next){
        counter++;
    }
    return counter;
}

public static void toPolynomial(Node head){
    int order = Node.listLength(head);
    int i = 0;
    int increment = Node.numOfNonZeroData2(head);
    //sortList(head);
    for( ; head != null; head = head.next){
        if(head.data2 != 0){
            if(i >= 0 && i < order){
                System.out.printf("%dx^%d", head.data1, head.data2);
                i++;
                if(i < increment && head.data1 >= 0){
                    System.out.print("+");      //case integer is positive
                }else if(i != increment && head.data1 <= 0){
                    System.out.println(" ");    //case integer is negative
                }
            }

        }
    }
    System.out.println();
}




public static Node mergeLists(Node n1, Node n2){
    if ( n1 == null) 
        return n2;
    else if ( n2 == null) 
        return n1;
    else {
        n1.next = mergeLists( n1.next, n2 );     // Note how we exchange p and q here
        return n1;
    }
}









public static Node addPolynomials(Node n1, Node n2){
    for( ; n1 != null; n1 = n1.next){
        for(; n2 != null; n2 = n2.next){
            if(n1.getData2() == n2.getData2()){
                n1.addData1(n2);
                System.out.println("added " + (n1.data1 - n2.data1) + " and " + n2.data1 );
            }

        }

    }
    return n1;
}

public static void subtractPolynomials(Node n1, Node n2){

}

public static void multiplyPolynomials(Node n1, Node n2){

}
 }

and the main method:

    public class LinkedListsMain {


public static void main(String[] args) {

    Node head;
    Node head2;

    head = new Node(5,  1, new Node(7, 4,  new Node(9 ,5, new Node(12, new Node (15)))));
    head2 = new Node(3, 1, new Node(1, 4, new Node(29, 5)));
    Node.displayAll(head);
    Node.displayAll(head2);
    System.out.println("Length: " + Node.listLength(head));
    System.out.println("Length: " + Node.listLength(head2));
    Node.toPolynomial(head);
    Node.toPolynomial(head2);
    //Node.mergeLists(head, head2);
Node.addPolynomials(head, head2);
    System.out.println("Add completed");
    Node.toPolynomial(head);
}

}

Any ideas?

To give more information, I am using linked lists as a more efficient data structure. I am inputting two linked lists and trying to add the corresponding elements together. I will enclose my entire code!

for example: if the input is
5x^1+7x^4+9x^5 + 3x^1+1x^4+29x^5

i would like the program to output
8x^1+8x^4+38^5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:58:11+00:00Added an answer on June 12, 2026 at 5:58 pm

    From what I got from your question, the problem is that when your outer loop is executed 2nd time, then n2 has already reached its last position.

    Because, you haven’t re-initialized n2 in inner loop again.
    So, it is null as from the first iteration. So your outer loop would run only once.

    Try changing your method with the below one: –

    public static Node addPolynomials(Node n1, Node n2) {
        Node x = n1;
        Node y = n2;
    
        for(x = n1; x != null; x = x.next){
            for(y = n2; y != null; y = y.next){
                if(x.getData2() == y.getData2()){
                    x.addData1(y);
                    System.out.println("added " + (x.data1 - y.data1) + " and " + y.data1);
                }
    
            }
        }
    
        return x;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a data structure CityPosition , in which to have
I'm trying to create a matrix data structure in C. I have a struct
I'm trying to implement a generic notification system.. I have a data structure like
The task: I am trying to create a custom data type and have it
I am trying to create a CvMat data structure using cvMat() in OpenCV. The
I am trying to have a data structure with multiple string keys. To do
In VB.NET I would like to create a complicated data structure with multiple types
I am trying to create a deep copy of my binary tree data structure
Today i was told to create tree data structure with the below class, public
I am trying to create a data structure called Disjoint Set. Looking at theory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.