排序单链表实现一元多项式的加减乘
public class SortedSinglyList<T extends Comparable<? super T>> extends SinglyList<T> {
protected boolean asc;
public SortedSinglyList(boolean asc){
super();
this.asc=asc;
}
public SortedSinglyList(){
this(true);
}
public SortedSinglyList(T[] values, boolean asc){
this(asc);
for (int i=0; i<values.length; i++){
this.insert(values[i]);
}
}
public SortedSinglyList(T[] values){
this(values,true);
}
public SortedSinglyList(SortedSinglyList<T> list){
this(list.asc);
Node<T> rear=this.head;
for (int i=0; i<list.size; i++){
rear.next=new Node<>(list.get(i),null);
rear=rear.next;
this.size++;
}
}
public SortedSinglyList(SortedSinglyList<T> list, boolean asc){
this(asc);
for (int i=0; i<list.size; i++){
this.insert(list.get(i));
}
}
public SortedSinglyList(SinglyList<T> list){
this(true);
for (int i=0; i<list.size; i++){
this.insert(list.get(i));
}
}
public void set(int i, T x){
throw new UnsupportedOperationException("set(int i, T x)");
}
public Node<T> insert(int i,T x){
throw new UnsupportedOperationException("insert(int i, T x)");
}
public Node<T> insert(T x){
if (x==null){
return null;
}
Node<T> front=this.head;
Node<T> p=front.next;
while (p!=null && (this.asc ? x.compareTo(p.data)>=0 : x.compareTo(p.data)<=0)){
front=p;
p=p.next;
}
front.next=new Node<>(x,p);
this.size++;
return front.next;
}
public Node<T> search(T key){
Node<T> p=this.head;
for (int i=0; i<this.size; i++){
if (p.next.data.equals(key)){
return p.next;
}
else {
p=p.next;
}
}
return null;
}
public T remove(T key){
return this.remove(this.search(key).data);
}
}
public class PolySinglyList extends SortedSinglyList<TermX> {
public PolySinglyList() {
super();
}
public PolySinglyList(boolean asc){
super(asc);
}
public PolySinglyList(TermX[] termXList, boolean asc){
this(asc);
for (int i=0; i<termXList.length; i++){
if (termXList[i]!=null){
this.insert(termXList[i]);
}
}
}
public PolySinglyList(PolySinglyList list){
this.asc=list.asc;
Node<TermX> rear=this.head;
for (int i=0; i<list.size; i++){
rear.next=new Node<>(list.get(i),null);
rear=rear.next;
this.size++;
}
}
public String toString(){
String str=this.getClass().getName() + "(";
if (this.size>0)
str += this.get(0).coef + "X^" + this.get(0).xexp;
for (int i=1;i<size;i++){
str += ", " + this.get(i).coef + "X^" + this.get(i).xexp;
}
return str + ")";
}
public void add(PolySinglyList pList){
Node<TermX> front=this.head;
Node<TermX> p=front.next;
Node<TermX> q=pList.head.next;
while (p!=null){
if (p.data.compareTo(q.data) == 0){
p.data.add(q.data);
if (p.data.removeAble()){
p=p.next;
front.next=p;
q=q.next;
}
p=p.next;
front=front.next;
q=q.next;
}
else if (p.data.compareTo(q.data) < 0){
p=p.next;
front=front.next;
}
else {
Node<TermX> temp=new Node<>(q.data,null);
temp.next=p;
front.next=temp;
front=front.next;
q=q.next;
this.size++;
}
}
while (q!=null){
Node<TermX> temp=new Node<>(q.data, null);
front.next=temp;
front=front.next;
q=q.next;
this.size++;
}
}
public PolySinglyList multi(PolySinglyList pList){
PolySinglyList multiList=new PolySinglyList();
for (int i=0; i<this.size; i++){
for (int j=0; j<pList.size; j++){
TermX t=new TermX(this.get(i).coef*pList.get(j).coef,this.get(i).xexp+pList.get(j).xexp);
multiList.insert(t);
}
}
return multiList;
}
public PolySinglyList sub(PolySinglyList pList){
PolySinglyList temp=new PolySinglyList(this);
TermX[] flagTermX={new TermX(-1,0)};
PolySinglyList flagList=new PolySinglyList(flagTermX,true);
temp=temp.multi(flagList);
temp.add(pList);
temp=temp.multi(flagList);
return temp;
}
public static void main(String[] args) {
TermX[] termX1={new TermX(2,6),new TermX(-9,3),new TermX(1,2),new TermX(-1,1),new TermX(2,0)};
PolySinglyList list1=new PolySinglyList(termX1,true);
System.out.println(list1.toString());
TermX[] termX2={new TermX(3,7)};
PolySinglyList list2=new PolySinglyList(termX2,true);
System.out.println(list2.toString());
list1=list1.sub(list2);
System.out.println(list1.toString());
}
}
public class TermX implements Comparable<TermX>,AddInterface<TermX>{
protected int coef;
protected int xexp;
public TermX(int coef, int xexp){
this.coef=coef;
this.xexp=xexp;
}
public TermX(TermX termX){
this.coef=termX.coef;
this.xexp=termX.xexp;
}
public boolean equals(Object obj){
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TermX termX=(TermX)obj;
if (this.coef == termX.coef && this.xexp==termX.xexp){
return true;
}
else {
return false;
}
}
@Override
public int compareTo(TermX o) {
if (this.xexp < o.xexp){
return -1;
}
else if (this.xexp == o.xexp){
return 0;
}
else {
return 1;
}
}
@Override
public void add(TermX termX) {
if (this.xexp == termX.xexp){
this.coef += termX.coef;
}
}
@Override
public boolean removeAble() {
if (this.coef == 0){
return true;
}
else {
return false;
}
}
}
public interface AddInterface<T> {
public void add(T t);
public boolean removeAble();
}
|