数据结构作业:
多项式合并,链表实现。
感觉这个写的还是挺合格的,哈哈。
import java.util.*;
public class PolySinglyListTest {
public static void main(String[] args) {
PolySinglyList Ax=new PolySinglyList("1+x-x^2-x^3+x^5+x^10");
System.out.println("表达式AX="+Ax);
PolySinglyList Bx=new PolySinglyList("-x+2x^2+x^3+x^12");
System.out.println("表达式BX="+Bx);
PolySinglyList Cx=PolySinglyList.add(Ax, Bx);
System.out.println("表达式CX=AX+BX="+Cx);
}
}
class PolySinglyList{
TermNode head;
public PolySinglyList() {
this.head=new TermNode(-1,-1); //头节点用特殊值-1标记。不和后面冲突
}
//这个函数比下面的更复杂,更呕心。。。
PolySinglyList(String polystr){
this();
if (polystr==null || polystr.length()==0)
return;
TermNode rear = this.head;
int start=0, end=0; //stat表示这一项起始位置,end是结束位置
while (start<polystr.length() && end<polystr.length()){
int i=polystr.indexOf('+',end+1); //返回字符+在字符串中从end+1开始的序号
if (i==-1) //未找到指定字符
i=polystr.length();
int j=polystr.indexOf('-',end+1);
if (j==-1)
j=polystr.length();
end=i<j? i: j; //end为下一个+或-号的序号
// System.out.println("start="+start+", end="+end+", "+polystr.substring(start,end));
rear.next = new TermNode(polystr.substring(start,end));
//System.out.println(rear.next);
//尾插入,以序号start~end的子串作为一项,创建结点,创建元素对象
rear = rear.next;
start=end;
}
}
public void add(TermNode term) { //添加一个节点的函数
if(term.removeable()) return; //如果这一项系数是0,不用添加。
TermNode rear=head;
while(rear.next!=null) {
if(rear.next.compareTo(term)==0) { //如果相等直接添加
rear.next.add(term);
if(rear.next.removeable()) { //如果添加后可以移除,直接移除
rear.next=rear.next.next;
}
return;
}else if(rear.next.compareTo(term)>0) { //如果下面那个大于这个,那这个插入到中间
term.next=rear.next;
rear.next=term;
return;
}
rear=rear.next;
}
rear.next=term; //如果遍历到最后一点,末端插入
}
public void add(PolySinglyList list) {
TermNode rear1=head;
TermNode rear2=list.head;
while(rear1.next!=null&&rear2.next!=null) { //遍历两个链表
if(rear1.next.compareTo(rear2.next)==0) { //如果某节点相等,add运算
rear1.next.add(rear2.next);
if(rear1.next.removeable()) {
rear1.next=rear1.next.next;
}
else
rear1=rear1.next;
rear2=rear2.next;
}else if(rear1.next.compareTo(rear2.next)>0) { //如果不相等,插入。
TermNode term=new TermNode(rear2.next);
term.next=rear1.next;
rear1.next=term;
rear1=term;
rear2=rear2.next;
}else {
rear1=rear1.next;
}
}
if(rear2.next!=null) {
rear1.next=rear2.next;
}
}
//静态相加函数,返回值是两个表达式相加
public static PolySinglyList add(PolySinglyList Ax,PolySinglyList Bx) {
PolySinglyList Cx=Ax.Clone();
Cx.add(Bx);
return Cx;
}
//克隆。
public PolySinglyList Clone() {
PolySinglyList Cx=new PolySinglyList();
TermNode rear1=Cx.head;
TermNode rear2=this.head;
while(rear2.next!=null) {
rear1.next=new TermNode(rear2.next);
rear1=rear1.next;
rear2=rear2.next;
}
return Cx;
}
public String toString() {
String str="";
TermNode rear=head;
while(rear.next!=null) {
if(rear!=head&&rear.next.getCoef()>0)
str+='+';
str+=rear.next.toString();
rear=rear.next;
}
return str;
}
}
class TermNode{
private int coef,xexp; //系数,x指数
public TermNode next; //下一个节点
public TermNode() {}
public TermNode(int c,int x) { //两个参数的构造函数
this.coef=c;
this.xexp=x;
}
public TermNode(TermNode term) {
this(term.coef,term.xexp);
}
//这个函数有些复杂,考虑的情况比较多。比较呕心QWQ
public TermNode(String termstr){
if (termstr.charAt(0)=='+') //去掉+号
termstr=termstr.substring(1);
int i = termstr.indexOf('x');
if (i==-1) { //没有x,即指数为0
this.coef = Integer.parseInt(termstr); //获得系数
this.xexp = 0;
}else { //有x,x之前为系数,x^之后为指数
if (i==0) { //以x开头,即系数为1
this.coef = 1;
} else{
String sub=termstr.substring(0,i); //x之前子串表示系数
if (sub.equals("-")) //系数只有-号,即系数为-1
this.coef=-1;
else
this.coef = Integer.parseInt(sub); //获得系数
}
i = termstr.indexOf('^');
if (i==-1)
this.xexp=1; //没有^,说明指数是1
else
this.xexp = Integer.parseInt(termstr.substring(i+1));//获得指数
}
}
public String toString() {
String str="";
if((this.coef!=1&&this.coef!=-1)||this.xexp==0) //如果是1或者-1的话,可以去掉
str+=this.coef+"";
if(this.coef==-1)
str+='-';
if(this.xexp!=0) {
str+="x^"+this.xexp;
}
return str;
}
public int compareTo(TermNode term) { //比较两个点指数的大小
return this.xexp-term.xexp;
}
public void add(TermNode term) { //add函数,指数相同,就相加
if(this.xexp==term.xexp)
this.coef+=term.coef;
}
public boolean removeable() { //判断是否可以移除此节点(系数为0可以移除)
return this.coef==0;
}
public int getCoef() { //因为对指数和系数进行了封装,所以要有get函数
return this.coef;
}
public int getXexp() {
return this.xexp;
}
}