用双向链表实现多次项的加法,最后分别升序降序输出
要求先输入,两个多次项的系数,再输入多次项的常数项和次数。
最后打印输出升序降序的结果
代码如下:
#include
#include
using namespace std;
class node{
public:
int coe;
int index;
node(int coeValue,int indexValue)
{
coe = coeValue;
index = indexValue;
}
node(const node& it)
{
this->coe = it.coe;
this->index = it.index;
}
node()
{
coe = index = 0;
}
void operator =(const node& it)
{
this->coe = it.coe;
this->index = it.index;
}
};
template class Link{
private:
static Link* freelist;
public:
E element;
Link* next;
Link* prev;
Link(const E& it, Link* prevp, Link* nextp)
:element(it),
prev(prevp),
next(nextp)
{
}
Link(Link* prevp = NULL,Link* nextp = NULL)
:prev(prevp),
next(nextp)
{
}
void* operator new(size_t)
{
if(freelist == NULL)
return ::new Link;
Link<E>* temp = freelist;
freelist = freelist->next;
return temp;
}
void operator delete(void* ptr)
{
((Link<E>*)ptr)->next = freelist;
freelist = (Link<E>*)ptr;
}
};
template
Link* Link::freelist = NULL;
template class Dlist{
private:
Link* head;
Link* tail;
Link* curr;
int cnt;
public:
Dlist()
{
curr = head = new Link;
tail = new Link(head,NULL);
head->next = tail;
cnt = 0;
}
void insert(const E& it)
{
curr->next = curr->next->prev =
new Link<E>(it,curr,curr->next);
cnt++;
}
void append(const E& it)
{
tail->prev = tail->prev->next =
new Link<E>(it,tail->prev,tail);
cnt++;
}
E remove()
{
if(curr->next!=tail)
{
E it = curr->next->element;
Link<E>* ltemp = curr->next;
curr->next->next->prev = curr;
curr->next = curr->next->next;
delete ltemp;
cnt--;
return it;
}
}
void prev()
{
if(curr->prev != head)
curr = curr->prev;
}
void next()
{
if(curr->next != tail)
curr = curr->next;
}
int currPos()
{
Link<E>* temp = head;
int i;
for(i = 0;temp != curr;i++)
temp = temp->next;
return i;
}
void moveToPos(int pos)
{
if(pos <0 && pos > cnt)
cerr << "Position out of range" << endl;
curr = head;
for(int i = 0;i<pos;i++)
curr = curr->next;
}
void moveToStart()
{
curr = head;
}
void moveToEnd()
{
curr = tail;
}
E& getValue()
{
if(curr->next != tail)
return curr->next->element;
}
E& getPrevValue()
{
if(curr->prev != head)
return curr->prev->element;
}
E& getTwoValue()
{
if(curr->next->next != tail)
return curr->next->next->element;
}
int length()
{
return cnt;
}
void sort()
{
Link<E>* p;
Link<E>* q;
E temp;
for(p = head->next;p->next != tail;p = p->next)
{
for(q = p->next;q != tail;q= q->next)
{
if(p->element.index > q->element.index)
{
temp = p->element;
p->element = q->element;
q->element = temp;
}
}
}
}
};
int main()
{
int m,n;
cin >> m >> n;
int coeValue;
int indexValue;
Dlist<node> A;
Dlist<node> B;
Dlist<node> res;
for(int i = 0;i<m;i++)
{
cin >> coeValue >> indexValue;
node temp(coeValue,indexValue);
A.append(temp);
}
for(int j = 0;j<n;j++)
{
cin >> coeValue >> indexValue;
node temp(coeValue,indexValue);
B.append(temp);
}//读入链表
A.sort();
B.sort();//排序
A.moveToStart();
B.moveToStart();
while(A.length()>0)//两个链表合并 (将A插入到B中)
{
if(A.getValue().index < B.getValue().index || B.currPos() == B.length())
B.insert(A.remove());
B.next();
}
B.moveToStart();//合并指数相同项
while(B.currPos() != B.length())
{
node temp1 = B.getValue();
node temp2 = B.getTwoValue();
if(temp1.index == temp2.index)
{
node res(temp1.coe+temp2.coe,temp1.index);
B.remove();
B.remove();
B.insert(res);
}
else
{
B.next();
}
}
B.moveToStart();//正序输出
node a;
while(B.currPos() !=B.length())
{
if(B.currPos()!=B.length()-1)
{
a = B.getValue();
cout <<a.coe << "x^" << a.index<<"+";
B.next();
}
else
{
a = B.getValue();
cout <<a.coe << "x^" << a.index<<endl;
B.next();
}
}
B.moveToEnd();//反序输出
while(B.currPos() !=1)
{
if(B.currPos()!= 2)
{
a = B.getPrevValue();
cout <<a.coe << "x^" << a.index<<"+";
B.prev();
}
else
{
a = B.getPrevValue();
cout <<a.coe << "x^" << a.index<<endl;
B.prev();
}
}
}