
正文
多项式相加java代码 java多项式相乘
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java单链表实现一元多项式相加的算法?
public class Test {
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println("一元多项式的相加过程:");
list1.listAll();
System.out.println(" + ");
list2.listAll();
System.out.println(" = ");
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 一元多项式的一般项类
*/
class Item{
private double coef; //一元多项式的一般项的系数
private int exp; //一元多项式的一般项的指数
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 链表结点类
*/
class Node{
private Item data;
private Node next; //链表结点的指针域多项式相加java代码,指向直接后继结点
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 链表类
*/
class LinkList{
private Node head = null; //头结点指针
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//删除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size--;
return true;
}
java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性多项式相加java代码,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网多项式相加java代码,同时拥有全球最大的开发者专业社群。
相关问答
Q1: 谁能帮我写个数据结构多项式相加的代码啊
#includeiostream
#includefstream//文件读入的头文件
#includestring//字符串操作
using namespace std;static char y;//全局变量用于判断多项式的未知数是否相同
int counting=0;//标记值
//节点的定义
typedef struct Node
{
double cof;
int exp;
struct Node *next;
}Node;
//类的定义
class DuoXiangShi
{
public:
DuoXiangShi(){};//构造函数
void InitDuoXiangShi(int n);//初始化多项式
DuoXiangShi operator +(DuoXiangShi v);//加法
DuoXiangShi operator -(DuoXiangShi v);//减法
void Show();//显示多项式
DuoXiangShi operator *(DuoXiangShi v);//乘法
double get(double x);//获得定点函数值
void Sort();//对多项式排序
private:
int count;//多项式的项数
Node* Head;//头指针
};void DuoXiangShi::InitDuoXiangShi(int n)
{
Node *ha,*hb;
double xishu;//系数
char c,d='+';
Head=(Node*)malloc(sizeof(Node));
//文件读入
ifstream inf("in.txt");
string line;
if(counting==1) getline(inf,line); ha=Head;
cout"请输入一个一元多项式:";
for(int i=0;in;i++)
{
hb=(Node*)malloc(sizeof(Node));
ha-next=hb;
infxishu;
if(xishu==0)//系数为0
{
cout"您输入的系数为0,输入错误!!!程序结束";
exit(-1);
} if(d=='+')
hb-cof=xishu;
else if(d=='-')
hb-cof=-xishu;
else
{
cout"您输入的不是一元多项式(请不要采用省略写法)!!!程序结束";
exit(-1);
} infchb-exp;
if(i==0)
y=c;
if(y!=c)//判断多项式的未知数是否相同
{
cout"您输入的不是一元多项式(变量个数是一个以上)!!!程序结束";
exit(-1);
} ha=hb;
if(i!=n-1)
infd;
hb-next=NULL;
}
count=n;//记录多项式的项数
}void DuoXiangShi::Show()
{
Node *ha;
ha=Head;
ha=ha-next;
if(!ha) cout"0";//如果为空,值为零
else
{
while(ha!=NULL)
{
if(ha-exp)//指数为0,省略
{
if(ha-cof0)
cout"(";
if(ha-cof!=1)//系数为1,省略
coutha-cof;
couty;
if(ha-exp!=1)//指数为1,省略
coutha-exp;
if(ha-cof0)
cout")";
}
else coutha-cof;
if(ha-next)
cout"+";
ha=ha-next;
}
}
coutendl;
}void DuoXiangShi::Sort()//冒泡排序
{
Node *ha,*hb,*pre;
int tempexp;
double tempcof;
for(int i=1;icount;i++)
{
pre=Head;
ha=Head-next;
hb=ha-next;
for(int j=0;jcount-i;j++)
{
if(ha-exphb-exp)
{
tempexp=ha-exp; tempcof=ha-cof;
ha-exp=hb-exp; ha-cof=hb-cof;
hb-exp=tempexp; hb-cof=tempcof;
pre=ha;
ha=hb;
hb=hb-next;
}
else if(ha-exphb-exp)
{
pre=ha;
ha=hb;
hb=hb-next;
}
else//相等合并同类项
{
ha-cof+=hb-cof;
hb=hb-next;
ha-next=hb;
count--;
if(ha-cof==0)//系数为0,消项
{
ha=ha-next;
hb=hb-next;
pre-next=ha;
count--;
}
}
}
}
}DuoXiangShi DuoXiangShi::operator +(DuoXiangShi v)
{//加到hb中
Node *ha,*hb,*hc,*pre;
ha=Head-next;
hb=v.Head-next;
pre=v.Head;
while(hahb)
{
if(ha-exphb-exp)//hb向后移
{
pre=hb;
hb=hb-next;
}
else if(ha-exphb-exp)//将ha添加到hb中
{
if(hb==v.Head-next)//若hb是第一个数据项
{
hc=ha-next;
v.Head-next=ha;
pre=ha;
ha-next=hb;
}
else//若hb不是第一个数据项
{
pre-next=ha;
hc=ha-next;
ha-next=hb;
pre=ha;
}
ha=hc;
}
else //相等合并同类项
{
hb-cof+=ha-cof;
count--;
if(hb-cof==0)////系数为0,消项
{
hb=hb-next;
pre-next=hb;
count--;
}
else
{
pre=hb;
hb=hb-next;
}
ha=ha-next;
}
}
if(ha)//若ha不为空
{
pre-next=ha;
}
v.count+=count;
return v;
}DuoXiangShi DuoXiangShi::operator -(DuoXiangShi v)
{//加到hb中
Node *ha,*hb,*hc,*pre;
ha=Head-next;
hb=v.Head-next;
pre=v.Head;
while(hahb)
{
if(ha-exphb-exp)//hb向后移
{
pre=hb;
hb-cof=-hb-cof;
hb=hb-next;
}
else if(ha-exphb-exp)//将ha添加到hb中
{
if(hb==v.Head-next)//若hb是第一个数据项
{
hc=ha-next;
v.Head-next=ha;
pre=ha;
ha-next=hb;
}
else//若hb不是第一个数据项
{
pre-next=ha;
hc=ha-next;
ha-next=hb;
pre=ha;
}
ha=hc;
}
else //相等合并同类项
{
hb-cof=ha-cof-hb-cof;
count--;
if(hb-cof==0)////系数为0,消项
{
hb=hb-next;
pre-next=hb;
count--;
}
else
{
pre=hb;
hb=hb-next;
}
ha=ha-next;
}
}
if(ha)//若ha不为空
{
pre-next=ha;
}
while(hb)//若hb不为空,变号
{
hb-cof=-hb-cof;
hb=hb-next;
}
v.count+=count;
return v;
}double DuoXiangShi::get(double x)
{
Node *ha;
ha=Head-next;
double y=0,z=1;
while(ha)
{
for(int j=0;jha-exp;j++)
{
z*=x;
}
z*=ha-cof;
y+=z;
z=1;
ha=ha-next;
}
return y;
}DuoXiangShi DuoXiangShi::operator *(DuoXiangShi v)
{
Node *ha,*hb,*NewHead,*hc,*hd,*he;
int i=0;
DuoXiangShi d;
NewHead=(Node*)malloc(sizeof(Node));
d.Head=NewHead;
hd=NewHead;
ha=Head-next;
hb=v.Head-next;
while(hahb)
{
he=hb;
while(he)
{
hc=(Node*)malloc(sizeof(Node));
hc-next=NULL;
hd-next=hc;
hd=hc;
hc-cof=ha-cof*he-cof;
hc-exp=ha-exp+he-exp;
he=he-next;
i++;
}
ha=ha-next;
}
d.count=i;
d.Sort();
ha=d.Head-next;
hb-next=ha;
while(ha)//检查系数是否为0并消项
{
if(ha-cof==0)////系数为0,消项
{
ha=ha-next;
hb-next=ha;
d.count--;
}
else//不为0,后移
{
hb=ha;
ha=ha-next;
}
}
return d;
}void menu()
{
system("cls");//清屏
system("color 0A");//菜单颜色
cout"\n\t\t菜单\n";
cout"\n1请输入一个一元多项式:\n";
cout"\n2加法运算\n";
cout"\n3减法运算\n";
cout"\n4乘法运算\n";
cout"\n5定点函数值\n";
cout"\n6退出系统\n";
}void main()
{ DuoXiangShi D;
DuoXiangShi D1;
DuoXiangShi D2;
menu();//调用菜单函数
char c,d;
int flag=1;
while(flag)
{
menu();
cout" 请选择";
c=getchar();
cin.get(d);
if(d!='\n')
{cout"***输入错误****"endl;exit(-1);}
switch(c)
{
case '1':
int m;
cout"请输入多项式的项数"endl;
cinm;
D.InitDuoXiangShi(m);//项数
D.Sort();
D.Show();
counting=1;
cout"请输入多项式的项数"endl;
cinm;
D1.InitDuoXiangShi(m);
D1.Sort();
D1.Show();
system("pause");
getchar();
break;
case '2':if(counting==0)
{
cout"(请在进行加减运算时先进行'1'操作)!!!";
getchar();break;
}
D2=D+D1;
D2.Show();
counting=0;
system("pause");
break;
case '3':if(counting==0)
{
cout"(请在进行加减运算时先进行'1'操作)!!!";
getchar();
break;
}
D2=D-D1;
D2.Show();
counting=0;
system("pause");
break;
case '4':if(counting==0)
{
cout"(请在进行加减运算时先进行'1'操作)!!!";
getchar();
break;
}
D2=D*D1;
D2.Show();
counting=0;
system("pause");
break;
case '5':
cout"请输入多项式的项数:";
cinm;
D.InitDuoXiangShi(m);//项数
D.Sort();
D.Show();
cout"请输入未知数的值:";
cinm;
coutD.get(m)endl;//定点x的值
system("pause");
getchar();
break;
case '6':flag=0;
cout"\n ***The End***\n";
break;
default:cout"\n Wrong Selection!\n";
getchar();
}
}
}
Q2: java中完成一元稀疏多项式的基本运算
除以上功能外,还有乘法和除法的计算和导数计算呢。
这是我以前做的数据结构课程设计。希望能帮上你的忙。
#includestdio.h
#includemalloc.h
typedef struct Polynomial{
float coef;
int expn;
struct Polynomial *next;
}*Polyn,Polynomial; //Polyn为结点指针类型
void Insert(Polyn p,Polyn h){
if(p-coef==0) free(p); //系数为0的话释放结点
else{
Polyn q1,q2;
q1=h;q2=h-next;
while(q2p-expnq2-expn){ //查找插入位置
q1=q2;
q2=q2-next;
}
if(q2p-expn==q2-expn){ //将指数相同相合并
q2-coef+=p-coef;
free(p);
if(!q2-coef){ //系数为0的话释放结点
q1-next=q2-next;
free(q2);
}
}
else{ //指数为新时将结点插入
p-next=q2;
q1-next=p;
}
}
}//Insert
Polyn CreatePolyn(Polyn head,int m){//建立一个头指针为head、项数为m的一元多项式
int i;
Polyn p;
p=head=(Polyn)malloc(sizeof(struct Polynomial));
head-next=NULL;
for(i=0;im;i++){
p=(Polyn)malloc(sizeof(struct Polynomial));//建立新结点以接收数据
printf("请输入第%d项的系数与指数:",i+1);
scanf("%f %d",p-coef,p-expn);
Insert(p,head); //调用Insert函数插入结点
}
return head;
}//CreatePolyn
void DestroyPolyn(Polyn p){//销毁多项式p
Polyn q1,q2;
q1=p-next;
q2=q1-next;
while(q1-next){
free(q1);
q1=q2;//指针后移
q2=q2-next;
}
}
void PrintPolyn(Polyn P){
Polyn q=P-next;
int flag=1;//项数计数器
if(!q) { //若多项式为空,输出0
putchar('0');
printf("\n");
return;
}
while (q){
if(q-coef0flag!=1) putchar('+'); //系数大于0且不是第一项
if(q-coef!=1q-coef!=-1){//系数非1或-1的普通情况
printf("%g",q-coef);
if(q-expn==1) putchar('X');
else if(q-expn) printf("X^%d",q-expn);
}
else{
if(q-coef==1){
if(!q-expn) putchar('1');
else if(q-expn==1) putchar('X');
else printf("X^%d",q-expn);
}
if(q-coef==-1){
if(!q-expn) printf("-1");
else if(q-expn==1) printf("-X");
else printf("-X^%d",q-expn);
}
}
q=q-next;
flag++;
}//while
printf("\n");
}//PrintPolyn
int compare(Polyn a,Polyn b){
if(ab){
if(!b||a-expnb-expn) return 1;
else if(!a||a-expnb-expn) return -1;
else return 0;
}
else if(!ab) return -1;//a多项式已空,但b多项式非空
else return 1;//b多项式已空,但a多项式非空
}//compare
Polyn AddPolyn(Polyn pa,Polyn pb){//求解并建立多项式a+b,返回其头指针
Polyn qa=pa-next;
Polyn qb=pb-next;
Polyn headc,hc,qc;
hc=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点
hc-next=NULL;
headc=hc;
while(qa||qb){
qc=(Polyn)malloc(sizeof(struct Polynomial));
switch(compare(qa,qb)){
case 1:
{
qc-coef=qa-coef;
qc-expn=qa-expn;
qa=qa-next;
break;
}
case 0:
{
qc-coef=qa-coef+qb-coef;
qc-expn=qa-expn;
qa=qa-next;
qb=qb-next;
break;
}
case -1:
{
qc-coef=qb-coef;
qc-expn=qb-expn;
qb=qb-next;
break;
}
}//switch
if(qc-coef!=0){
qc-next=hc-next;
hc-next=qc;
hc=qc;
}
else free(qc);//当相加系数为0时,释放该结点
}//while
return headc;
}//AddPolyn
Polyn SubtractPolyn(Polyn pa,Polyn pb){//求解并建立多项式a+b,返回其头指针
Polyn h=pb;
Polyn p=pb-next;
Polyn pd;
while(p){ //将pb的系数取反
p-coef*=-1;
p=p-next;
}
pd=AddPolyn(pa,h);
for(p=h-next;p;p=p-next) //恢复pb的系数
p-coef*=-1;
return pd;
}//SubtractPolyn
float ValuePolyn(Polyn head,float x){//输入x值,计算并返回多项式的值
Polyn p;
int i;
float sum=0,t;
for(p=head-next;p;p=p-next){
t=1;
for(i=p-expn;i!=0;){
if(i0){t/=x;i++;}//指数小于0,进行除法
else{t*=x;i--;}//指数大于0,进行乘法
}
sum+=p-coef*t;
}
return sum;
}//ValuePolyn
Polyn Derivative(Polyn head){//求解并建立a的导函数多项式,并返回其头指针
Polyn q=head-next,p1,p2,hd;
hd=p1=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点
hd-next=NULL;
while(q){
if(q-expn!=0){ //该项不是常数项时
p2=(Polyn)malloc(sizeof(struct Polynomial));
p2-coef=q-coef*q-expn;
p2-expn=q-expn-1;
p2-next=p1-next;//连接结点
p1-next=p2;
p1=p2;
}
q=q-next;
}
return hd;
}//Dervative
Polyn MultiplyPolyn(Polyn pa,Polyn pb){//求解并建立多项式a*b,返回其头指针
Polyn hf,pf;
Polyn qa=pa-next;
Polyn qb=pb-next;
hf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点
hf-next=NULL;
for(;qa;qa=qa-next){
for(qb=pb-next;qb;qb=qb-next){
pf=(Polyn)malloc(sizeof(struct Polynomial));
pf-coef=qa-coef*qb-coef;
pf-expn=qa-expn+qb-expn;
Insert(pf,hf);//调用Insert函数以合并指数相同的项
}
}
return hf;
}//MultiplyPolyn
void DevicePolyn(Polyn pa,Polyn pb){//求解并建立多项式a*b,返回其头指针
Polyn hf,pf,af,temp1,temp2,q;
Polyn qa=pa-next;
Polyn qb=pb-next;
hf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点,存储商
hf-next=NULL;
pf=(Polyn)malloc(sizeof(struct Polynomial));//建立头结点,存储余数
pf-next=NULL;
temp1=(Polyn)malloc(sizeof(struct Polynomial));
temp1-next=NULL;
temp2=(Polyn)malloc(sizeof(struct Polynomial));
temp2-next=NULL;
temp1=AddPolyn(temp1,pa);
while(qa!=NULLqa-expn=qb-expn){
temp2-next=(Polyn)malloc(sizeof(struct Polynomial));
temp2-next-coef=(qa-coef)/(qb-coef);
temp2-next-expn=(qa-expn)-(qb-expn);
Insert(temp2-next,hf);
pa=SubtractPolyn(pa,MultiplyPolyn(pb,temp2));
qa=pa-next;
temp2-next=NULL;
}
pf=SubtractPolyn(temp1,MultiplyPolyn(hf,pb));
pb=temp1;
printf("商是:");
PrintPolyn(hf);
printf("余数是:");
PrintPolyn(pf);
}//DevicePolyn
int main(){
int m,n,flag=0;
float x;
Polyn pa=0,pb=0,pc,pd,pe,pf;//定义各式的头指针,pa与pb在使用前付初值NULL
printf("请输入a的项数:");
scanf("%d",m);
pa=CreatePolyn(pa,m);//建立多项式a
printf("请输入b的项数:");
scanf("%d",n);
pb=CreatePolyn(pb,n);//建立多项式a
//输出菜单
printf("**********************************************\n");
printf("操作提示:\n\t1.输出多项式a和b\n\t2.建立多项式a+b\n\t3.建立多项式a-b\n");
printf("\t4.计算多项式a在x处的值\n\t5.求多项式a的导函数\n\t6.建立多项式a*b\n");
printf("\t7.建立多项式a/b\n\t8.退出\n**********************************************\n");
for(;;flag=0){
printf("执行操作");
scanf("%d",flag);
if(flag==1){
printf("多项式a:");PrintPolyn(pa);
printf("多项式b:");PrintPolyn(pb);continue;
}
if(flag==2){
pc=AddPolyn(pa,pb);
printf("多项式a+b:");PrintPolyn(pc);
DestroyPolyn(pc);continue;
}
if(flag==3){
pd=SubtractPolyn(pa,pb);
printf("多项式a-b:");PrintPolyn(pd);
DestroyPolyn(pd);continue;
}
if(flag==4){
printf("输入x的值:x=");
scanf("%f",x);
printf("多项式a的值%g\n",ValuePolyn(pa,x));continue;
}
if(flag==5){
pe=Derivative(pa);
printf("多项式a的导函数:");PrintPolyn(pe);
DestroyPolyn(pe);continue;
}
if(flag==6){
pf=MultiplyPolyn(pa,pb);
printf("多项式a*b:");PrintPolyn(pf);
DestroyPolyn(pf);continue;
}
if(flag==7){
DevicePolyn(pa,pb);
continue;
}
if(flag==8) break;
if(flag1||flag8) printf("Error!!!\n");continue;
}//for
DestroyPolyn(pa);
DestroyPolyn(pb);
return 0;
}
Q3: 用java单链表实现一元多项式相加的算法。()
public class Test {
public static void main(String[] args) {
try{
LinkList list1 = new LinkList();
LinkList list2 = new LinkList();
LinkList list3 = null;
list1.addAt(0, new Item(1, 5));
list1.addAt(1, new Item(-1.5, 3));
list1.addAt(2, new Item(1, 1));
list2.addAt(0, new Item(0.5, 5));
list2.addAt(1, new Item(0.5, 4));
list2.addAt(2, new Item(1.5, 3));
list2.addAt(3, new Item(3, 0));
list3 = mergeLinkList(list1, list2);
System.out.println("一元多项式的相加过程:");
list1.listAll();
System.out.println(" + ");
list2.listAll();
System.out.println(" = ");
list3.listAll();
}
catch(Exception e){
e.printStackTrace();
}
}
//两个一元多项式相加,返回新的一元多项式
public static LinkList mergeLinkList(LinkList list1, LinkList list2){
int i = 0;
Item item = new Item();
Node curr1, curr2;
LinkList list3 = new LinkList();
curr1 = list1.getHead().getNext();
curr2 = list2.getHead().getNext();
while(curr1 != null curr2 != null){
if(curr1.getData().getExp() curr2.getData().getExp()){
item = curr1.getData();
list3.addAt(i, item);
curr1 = curr1.getNext();
i++;
}
else if(curr1.getData().getExp() curr2.getData().getExp()){
item = curr2.getData();
list3.addAt(i, item);
curr2 = curr2.getNext();
i++;
}
else{
item = new Item(curr1.getData().getCoef() + curr2.getData().getCoef(), curr1.getData().getExp());
if(item.getCoef() != 0){
list3.addAt(i, item);
i++;
}
curr1 = curr1.getNext();
curr2 = curr2.getNext();
}
}
while(curr1 != null){
item = curr1.getData();
list3.addAt(i++, item);
curr1 = curr1.getNext();
}
while(curr2 != null){
item = curr2.getData();
list3.addAt(i++, item);
curr2 = curr2.getNext();
}
return list3;
}
}
/**
* 一元多项式的一般项类
*/
class Item{
private double coef; //一元多项式的一般项的系数
private int exp; //一元多项式的一般项的指数
public Item(){
this.coef = 0.0;
this.exp = 0;
}
public Item(double coef, int exp){
this.coef = coef;
this.exp = exp;
}
public double getCoef(){
return this.coef;
}
public void setCoef(double coef){
this.coef = coef;
}
public int getExp(){
return this.exp;
}
public void setExp(int exp){
this.exp = exp;
}
}
/**
* 链表结点类
*/
class Node{
private Item data;
private Node next; //链表结点的指针域,指向直接后继结点
public Node(){
data = null;
next = null;
}
public Node(Item data, Node next){
this.data = data;
this.next = next;
}
public Item getData(){
return this.data;
}
public void setData(Item data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public void setNext(Node next){
this.next = next;
}
}
/**
* 链表类
*/
class LinkList{
private Node head = null; //头结点指针
private int size = 0;
public LinkList(){
head = new Node();
size = 0;
}
//在i位置插入元素elem
public boolean addAt(int i, Item elem) {
if(i 0 || i size){
return false;
}
Node pre,curr;
int pos;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = new Node(elem, pre.getNext());
pre.setNext(curr);
size++;
return true;
}
//删除i位置的元素
public boolean removeAt(int i) {
if(i 0 || i = size){
return false;
}
Node pre,curr;
for(pre=head; i0 pre.getNext()!=null; i--,pre=pre.getNext());
curr = pre.getNext();
pre.setNext(curr.getNext());
size--;
return true;
}
//根据值value查询结点是否存在,若存在返回位置,否则返回-1
public int findByValue(Item value){
Node curr;
int pos;
for(pos=0,curr=head.getNext(); curr!=null; pos++,curr=curr.getNext()){
if(curr.getData().toString().equals(value.toString())){
break;
}
}
if(curr==null){
return -1;
}
return pos;
}
public Node getHead(){
return this.head;
}
public void setHead(Node head){
this.head = head;
}
public int getSize(){
return this.size;
}
public boolean isEmpty(){
return (size==0);
}
public void listAll(){
for(Node curr=head.getNext(); curr!=null; curr=curr.getNext()){
System.out.print("(" + curr.getData().getCoef() + ", " + curr.getData().getExp() + ")\t");
}
System.out.println();
}
}
关于多项式相加java代码和java多项式相乘的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






