我是靠谱客的博主 忧虑奇迹,这篇文章主要介绍数据结构(Java):用单链表实现多项式相加,现在分享给大家,希望可以做个参考。

要求:
1.已知有两个多项式Pn(x)和Qm(x),并且在Pn(x)和Qm(x)中指数相差很多,设计算法,求Pn(x)+Qm(x)
2.进行加法运算时不重新开辟存储空间。


//定义节点类
class Node{
public int coef;//系数

public int exp;//指数

public Node next=null;//下个节点

public Node(){
coef=0;
exp=0;
}
public Node(int coef,int exp){
this.coef=coef;
this.exp=exp;
}
}
//多项式类
public class PolynList {
//多项式相加
public Node add(Node link1, Node link2) {
Node pre=link1;
Node qre=link2;
Node p=pre.next;
Node q=qre.next;
Node result=p;
while(p!=null && q!=null){
if(p.exp<q.exp){
pre=p;
p=p.next;
}else if(p.exp>q.exp){
Node temp=q.next;
pre.next=q;
q.next=p;
q=temp;
}else{
p.coef=p.coef+q.coef;
if(p.coef==0){
pre.next=p.next;
p=pre.next;
}else{
pre=p;
p=p.next;
}
qre.next=q.next;
q=qre.next;
}
}
if(q!=null){
pre.next=q;
}
return result;
}
//添加数据方法
public Node insert(Node link,int coef,int exp) {//添加节点

Node node=new Node(coef,exp);
if(link==null){
link.next=node;
}else{
Node temp=link;
while(temp.next!=null){
temp=temp.next;
}
temp.next=node;
}
return link;
}
}
//主方法
public static void main(String[] args) {
PolynList ts = new PolynList();
Node link1=new Node();
Node link2=new Node();
//第一个多项式
ts.insert(link1,4,0);
ts.insert(link1,5,2);
ts.insert(link1,4,8);
ts.insert(link1,6,12);
//第二个多项式
ts.insert(link2,6,1);
ts.insert(link2,6,3);
ts.insert(link2,3,8);
ts.insert(link2,4,15);
ts.insert(link2,8,20);
link1 = ts.add(link1, link2);
while(link1!=null){
if(link1.exp== 0)
System.out.print(link1.coef);
else
System.out.print(link1.coef+"x^"+link1.exp);
link1=link1.next;
if(link1!=null)
System.out.print("+");
}
}
}

最后

以上就是忧虑奇迹最近收集整理的关于数据结构(Java):用单链表实现多项式相加的全部内容,更多相关数据结构(Java)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部