java链表实现一元多项式的合并同类项以及加法

上课的作业:利用java数据结构的知识表示一元多项式,以及实现一元多项式的加法运算以及合并同类项

链表节点类:

package PloyItem;


public class Lnode implements Comparable<Lnode>{
	public float coef;
	public int exp;
	public Lnode next;
	public Lnode (float coef,int exp){
		this.exp=exp;
		this.coef=coef;
		next=null;
	}
	public Lnode(float coef,int exp,Lnode next){
		this.exp=exp;
		this.coef=coef;
		this.next=next;
	}
	public boolean equals(Object e){
		Lnode node=(Lnode)e;
		return (exp==node.exp);
	}
	@Override
	public int compareTo(Lnode o) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}

多项式类:

package PloyItem;

import java.util.Iterator;

import org.junit.Test;
import seqList.AbList;

public class PloyItemList  {
	private int length;
	Lnode first;
	public PloyItemList(int length)
	{
		first=null;
		this.length=length;
	}
	public PloyItemList(){
		first=null;
		length=0;
	}
	public int size()//获取链表的长度
	{
		Lnode p=first;
		int count=0;
		while(p!=null){
			count++;
			p=p.next;
		}
		return count;
	}
	public boolean add(float coef,int exp)//向链表当中添加元素的方法
	{
		
		Lnode p=first,s;
		s=new Lnode(coef,exp,null);
		
		if(first==null){
			first=s;
			s.next=null;
			return true;
		}
		else {
			while(p.next!=null){
				p=p.next;
			}
			p.next=s;
			s.next=null;
			return true;
		}
		
	}
	public boolean add(float coef)//想链表当中添加元素的方法
	{
		
		Lnode p=first,s;
		s=new Lnode(coef,0,null);
		if(first==null){
			first=s;
			s.next=null;
			return true;
		}
		else {
			while(p.next!=null){
				p=p.next;
			}
			p.next=s;
			s.next=null;
			return true;
		}
	}
	public void sort(){//对多项式进行降序排列的方法
		Lnode p,q;int i,j,m;float n;
		for( i=0,p=first;i<this.size()-1;i++,p=p.next)
			for(j=i+1,q=p.next;j<this.size();j++,q=q.next)
				if(p.exp<q.exp)
				{
					m=p.exp;p.exp=q.exp;q.exp=m;
					n=p.coef;p.coef=q.coef;q.coef=n;
				}
	}
	public void union()//对多项式进行合并同类项的方法
	{
		Lnode p,q,r;
		sort();
		p=first;
		q=p.next;
		while(p!=null&& q!=null){
			if(p.exp==q.exp)
			{	r=q;
				p.coef=p.coef+q.coef;
				remove(q.coef,q.exp);
				p=r;
				q=r.next;
			} 
			else {
				p=q;
				q=q.next;
			}
		}
	}
	public void remove (float coef,int exp)//删除链表当中的某一个节点的方法
	{
		Lnode p=first,q=p;
		for(q=p;q!=null;q=q.next)
			if(q.next.coef==coef && q.next.exp==exp)
				break;
		q.next=q.next.next;
	}
	public String toString()//将链表转化为一个字符串输出的方法
	{
		String s="p(x)=";
		Lnode p=first;
		sort();
		union();
		while(p!=null){
			 if(p.coef==0)
				s=s+"+";
			else if(p.exp==0)
				s=s+p.coef+"+";
			else if(p.exp==1)
				s=s+p.coef+"x"+"+";
			else
			s=s+p.coef+"x^"+p.exp+"+";
			p=p.next;
		}
		return s.substring(0, s.length()-1)+"\n";
	}
	public void addPloyItem(PloyItemList p2)//多项式想家的而方法
	{
		this.sort();p2.sort();
		Lnode p=this.first,q=p2.first;
		while(p!=null || q!=null)
		{
			if(p!=null && q!=null)
			{
				if(p.exp==q.exp){
					p.coef+=q.coef;
					p=p.next;q=q.next;
				}
				else if(p.exp<q.exp){
					this.add(q.coef, q.exp);
					q=q.next;
				}
				else {
					this.add(q.coef, q.exp);
					q=q.next;
				}
			}
			else if(p==null && q!=null)
				this.add(q.coef, q.exp);
			else if(p!=null && q==null)
				p=p.next;
		}
	}
	
}
测试类:

package PloyItem;
/**
 * 用线性表实现一元多项式的加法运算以及多项式的输出
 * @author asus
 *
 */
public class PolyItemDemo {
public static void main(String[] args) {
	PloyItemList p1=new PloyItemList(4);
	p1.add(3, 3);p1.add(2, 2);p1.add(1, 1);p1.add(1);
	System.out.println("第一个多项式(合并同类项):"+p1);
	p1.add(4, 4);
	p1.add(15);
	p1.add(3, 2);
	System.out.println(p1);
	System.out.println("多项式的项数为(合并同类项):"+p1.size());
	PloyItemList p2=new PloyItemList();
	p2.add(6,4);p2.add(4,4);p2.add(3,2);p2.add(1,0);
	System.out.println(p2);
	p2.add(19);
	System.out.println(p2);
	System.out.println("多项式的项数为:"+p2.size());
	System.out.println("多项式的相加:");
	p1.addPloyItem(p2);
	System.out.println("相加的结果为:"+p1);
}

}
测试的结果:





猜你喜欢

转载自blog.csdn.net/Scorpion_CG/article/details/67639550