[英]polynomial addition using linked list in java
這是我使用鏈接列表將兩個多項式相加的實現。
例如,如果我想添加
3x ^ 2 + 5 ^ x + 3和4x ^ 3 + 5x + 2
首先,我檢查兩個多項式中是否存在相似的指數,如果是,則將它們的系數相加,然后將指數附加到字符串中。
在添加相似的指數之后,然后使用字符串,我將兩個多項式中的其余部分添加到最終結果中。
public class Node2{
int coef;
int exp;
Node2 next;
Node2(int c,int e,Node2 n){
coef=c;
exp=e;
next=n;
}
Node2(int c,int e){
coef=c;
exp=e;
}
}
public class LinkedPoly{
static String exponent="";
Node2 head;
Node2 current;
LinkedPoly(){
head=null;
}
public void createList(int c,int e){
head=new Node2(c,e,head);
}
public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){
LinkedPoly addList=new LinkedPoly();
Node2 temp1=list1.head;
Node2 temp3=temp1;
Node2 temp2=list2.head;
Node2 temp4=temp2;
while(temp1.next!=null){
while(temp2.next!=null){
if(temp1.exp==temp2.exp){
addList.createList((temp1.coef+temp2.coef),temp1.exp);
exponent+=temp1.exp;
}
temp2=temp2.next;
}
temp1=temp1.next;
temp2=temp4;
}
String[] array=exponent.split("");
for(int i=1;i<array.length;i++){
while(temp3.next!=null){
if(temp3.exp!=Integer.parseInt(array[i])){
addList.createList(temp3.coef,temp3.exp);
}
temp3=temp3.next;
}
while(temp4.next!=null){
if(temp4.exp!=Integer.parseInt(array[i])){
addList.createList(temp4.coef,temp4.exp);
}
temp4=temp4.next;
}
}
return addList;
}
public static void main (String args[]){
LinkedPoly l1=new LinkedPoly();
l1.createList(3,2);
l1.createList(5,1);
l1.createList(3,0);
LinkedPoly l2=new LinkedPoly();
l2.createList(4,3);
l2.createList(5,1);
l2.createList(2,0);
LinkedPoly l3=add(l1,l2);
System.out.println(l3.head.next.next.coef);
}
}
根據我的示例,指數字符串包括1和0,但是它僅求和系數1。此外,其余的加法運算也是錯誤的。
我看不到錯誤的地方,也如何打印出最終的addList,以便可以檢查此實現是否正常
這是一個有效的添加方法:
public static LinkedPoly add(LinkedPoly list1,LinkedPoly list2){
LinkedPoly addList=new LinkedPoly();
Node2 temp1=list1.head;
Node2 temp3=temp1;
Node2 temp2=list2.head;
Node2 temp4=temp2;
while(temp1.next!=null){
while(temp2.next!=null){
if(temp1.exp==temp2.exp){
addList.createList((temp1.coef+temp2.coef),temp1.exp);
exponent+=temp1.exp;
}
temp2=temp2.next;
}
temp1=temp1.next;
temp2=temp4;
addList.print();
}
String[] array=exponent.split("");
while(temp3!=null){
boolean exponentPresent = false;
for(int i=1;i<array.length;i++){
if(temp3.exp==Integer.parseInt(array[i])){
exponentPresent = true;
}
}
if (!exponentPresent) {
addList.createList(temp3.coef,temp3.exp);
}
temp3=temp3.next;
}
while(temp4!=null){
boolean exponentPresent = false;
for(int i=1;i<array.length;i++){
if(temp4.exp==Integer.parseInt(array[i])){
exponentPresent = true;
}
}
if (!exponentPresent) {
addList.createList(temp4.coef,temp4.exp);
}
temp4=temp4.next;
}
return addList;
}
這是可以添加到LinkedPoly類的打印方法:
public void print() {
current = head;
System.out.print(current.coef + "x^" + current.exp);
while (current.next != null) {
current = current.next;
System.out.print(" + " + current.coef + "x^" + current.exp);
}
System.out.println();
}
您的add方法存在兩個主要問題。
第1期 。 首先是遍歷已包含的指數數組的循環不在遍歷多項式鏈表的節點的循環之內-它應該在內部。 您以前的操作方式是這樣的:
一種。 從數組b中選取一個已經包含的指數。 遍歷每個多項式c的所有項。 如果這些術語中的任何一個具有與a。部分中的指數都不匹配的指數,請將其添加到結果中。 d。 從a。部分重復,但是使用下一個已經包含的指數。
這種方法的問題在於,您僅想在結果的指數與任何已包含的術語不匹配時才向結果添加新術語-不僅是它與其中任何一個都不匹配,也不是僅將其添加到結果中。 這就是為什么您的結果具有所有這些額外的x ^ 1項的原因-當您的程序位於數組的“ 0”元素上時,它會將多項式的x ^ 1項相加。
第2期 。 您應該用(temp3!= null)或(temp4!= null)代替while(temp3.next!= null)或(temp4.next!= null)。 否則,您的代碼將永遠無法到達多項式的最后一個節點(它會在最后一個節點之前停止,因為它會檢查最后一個節點之后是否存在“下一個”)。 這就是為什么您的結果中沒有x ^ 3和x ^ 4項的原因-您的循環在獲得這些項之前就結束了。
需要考慮的幾件事
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.