gpt4 book ai didi

java - 使用 Node 类将两个列表添加在一起

转载 作者:行者123 更新时间:2023-12-02 07:31:31 26 4
gpt4 key购买 nike

我正在尝试创建一个数据结构来保存多项式。到目前为止,我已经创建了一个 Node 类来存储数据,如下所示:(系数,指数,链接到下一个节点)不过,当我尝试将两个列表添加在一起时遇到了问题。我的方法添加每个列表中的第一个值,然后终止。

下面是我的代码:

    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){

}
}

主要方法:

    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);
}

}

有什么想法吗?

为了提供更多信息,我使用链表作为更有效的数据结构。我正在输入两个链接列表并尝试将相应的元素添加在一起。我将附上我的整个代码!

例如:如果输入是5x^1+7x^4+9x^5 + 3x^1+1x^4+29x^5

我想要程序输出8x^1+8x^4+38^5

最佳答案

根据我从你的问题中得到的信息,问题是当你的外循环第二次执行时,n2已经到达了它的最后位置。

因为,您还没有再次在内循环中重新初始化n2。因此,从第一次迭代开始它就是 null。所以你的外循环只会运行一次。

尝试使用以下方法更改您的方法:-

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;
}

关于java - 使用 Node 类将两个列表添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860614/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com