#include<iostream>
using namespace std;
int sz1,sz2,c,d;
template <class T>
class List{
void clear();
bool isempty();
bool append(const T value);
bool insert(const int p,const T value);
bool Delete(const int p);
bool getvalue(const int p,T& value);
bool setvalue(const int p,const T value);
bool getpos(int &p,const T value);
};
template <class T>
class Link
{
public:
T cof,deg;
Link<T> *next;
Link(const T c,const T d, Link<T>*nextValue=NULL){
cof=c;
deg=d;
next=nextValue;
}
Link(Link<T>*nextValue){
next=nextValue;
}
Link(){ //空构造函数,用于append函数里面生成元素
cof=deg=0;
next=NULL;
}
};
template <class T>
class Inklist : public List<T>{
private:
Link<T> *head,*tail;
Link<T> * setpos(const int p);
public:
Inklist(int s);
~Inklist();
bool append(const T c,const T d);
bool insert(const int p,const T c,const T d);
bool getvalue(const int p,T& c,T& d ); //返回下标为p的元素的值
bool Delete(const int p);
bool changevalue(const int p,const T c,const T d);//更改下标为p的元素的值
void add(Inklist<T>& b);
// void add(Inklist<T>& a,Inklist<T>& b);
void print();
} ;
template <class T>
Inklist<T>::Inklist(int defSize){
head=tail=new Link<T>;
}
template <class T>
Inklist<T>::~Inklist(){
Link<T> *tmp;
while(head!=NULL){
tmp=head;
head=head->next;
delete tmp;
}
}
template<class T>
Link<T> * Inklist<T>::setpos(const int i){
int count=0;
if(i==-1) return head;
Link<T> *p= new Link<T> (head->next);
while(p!=NULL&&count<i){
p=p->next;
count++;
}
return p;
}
template <class T>
bool Inklist<T> :: insert(const int i,const T c,const T d){
Link<T> *p,*q;
if((p=setpos(i-1))==NULL){
cout<<"Insert point is illegal\n";
return false;
}
q=new Link<T> (c,d,p->next);
p -> next=q;
if(i==1) head->next=q;
if(p==tail)
tail=q;
return true;
}
template<class T>
bool Inklist<T>::append(const T c,const T d){
Link<T> *p=new Link<T>(c,d,NULL);
if(head->next==NULL) head->next=p,tail=p;
//cout<<head->next<<p<<endl;
else{
tail->next=p;
tail=p;
}
return true;
}
template<class T>
bool Inklist<T>::Delete(const int i){
Link<T> *p,*q;
if((p=setpos(i-1))==NULL||p==tail){
cout<<"delete postion is illegal\n";
return false;
}
q=p->next;
if(q==tail){
tail=p;
p->next=NULL;
delete q;
}
else if(q!=NULL){
p->next=q->next;
delete q;
}
return true;
}
template<class T>
bool Inklist<T>::getvalue(const int p,T&c,T&d){ //返回下标为p的元素的值
Link<T> *temp=setpos(p);
if(temp->next==NULL||temp->next==head){
c=0,d=0;
return false;
}
c=(temp->next)->cof;
d=(temp->next)->deg;
return true;
}
template<class T>
bool Inklist<T>::changevalue(const int p,const T c,const T d){//更改下标为p的元素的值
Link<T> *temp=setpos(p);
if(temp->next==head||temp->next==NULL){
return false;
}
(temp->next)->cof=c;
(temp->next)->deg=d;
return true;
}
template<class T>
void Inklist<T>::add(Inklist<T> &b){
int pos1=1,pos2=1;
int cof1=0,cof2=0,deg1=0,deg2=0;
while(getvalue(pos1-1,cof1,deg1)&&b.getvalue(pos2-1,cof2,deg2)){//遍历两个数组的值
if(deg1==deg2) {
cof1+=cof2;
//if(cof1==0) Delete(pos1),pos1--;
// else
changevalue(pos1-1,cof1,deg1);
pos1++;
pos2++;
}
else{
if(deg1>deg2){
pos1++;
}
if(deg1<deg2){
insert(pos1,cof2,deg2);
pos2++;
pos1++;
}
}
//print();
}
while (b.getvalue(pos2-1,cof2,deg2)){//如果第二个数组仍有剩余
append(cof2,deg2);
pos2++;
}
return;
}
template<class T>
void Inklist<T>::print(){
Link<T> *p;
for(p=head->next;p!=NULL;p=p->next){
if(p->cof==0) continue;//也可以直接删除当前元素
cout<<p->cof<<' '<<p->deg<<' ';
}
cout<<endl;
}
/* //采用生成新的链表的方式,较为简单的写法
template<class T>
void Inklist<T>::add(Inklist<T> &a,Inklist<T> &b){
int pos1=1,pos2=1;
int cof1=0,cof2=0,deg1=0,deg2=0;
while(a.getvalue(pos1-1,cof1,deg1)&&b.getvalue(pos2-1,cof2,deg2)){
if(deg1==deg2) {
if(cof1+cof2) append(cof1+cof2,deg1);
pos1++;
pos2++;
}
else{
if(deg1>deg2){
append(deg1,cof1);
pos1++;
}
if(deg1<deg2){
append(deg2,cof2);
}
}
}
while (a.getvalue(pos1-1,cof1,deg1)){
append(cof1,deg1);
pos1++;
}
while (b.getvalue(pos2-1,cof2,deg2)){
append(cof2,deg2);
pos2++;
}
return;
}
*/
int main(){
cout<<"Please input the number about the first polynomial\n";
cout<<"多项式项数 the number of terms\n";
cin>>sz1;
Inklist<int> ila(sz1+2);
for(int i=1;i<=sz1;i++){
cin>>c>>d;
ila.append(c,d);
}
cout<<"Please input the number about the second polynomial\n";
cout<<"多项式项数 the number of terms\n";
cin>>sz2;
Inklist<int> ilb(sz2+2);
for(int i=1;i<=sz2;i++){
cin>>c>>d;
ilb.append(c,d);
}
//Inklist<int> ilc(sz1+sz2+2);
//ilc.add(ila,ilb);
ila.add(ilb);
ila.print();
//ilc.print();
return 0;
}
数据结构与算法实验一:链表实现多项式相加
最新推荐文章于 2021-11-18 11:33:05 发布