该有的链表结构:
class DbList;
class DbListNode
{
friend class DbList;
private:
DbListNode *pre;
DbListNode *next;
int exp;
float coef;
public:
DbListNode(DbListNode *pt = NULL,DbListNode *nt = NULL)
{
pre = pt;
next = nt;
}
DbListNode(float y,int x,DbListNode *pt = NULL,DbListNode *nt = NULL)
{
coef = y;
exp = x;
pre = pt;
next = nt;
}
};
class DbList
{
private:
DbListNode *begin;
DbListNode *end;
size_t size;
public:
DbList()
{
begin = new DbListNode;
end = begin;
begin->pre = end;
end->next = begin;
size = 0;
}
~DbList(){makeEmpty();}
Status push_back(float y,int x);
void show_dblist();
size_t getLenth(){return size;}
void makeEmpty();
Status IsEmpty(){return begin == end?TRUE:FALSE;}
Status pop_back();
Status pop_front();
Status insert_val(float y,int x);
Status del_val(float y,int x);
void sort();
void listadd(DbList *y1,DbList *y2);
};
void DbList::sort()
{
if(IsEmpty())
{
cout<<"链表为空(sort)..."<<endl;
}
else
{
//p和m是断开后面的两个指针
DbListNode *p = begin->next;//p在后
DbListNode *m; //m在前
p->pre->next = begin;
begin->pre = p->pre;
end = begin;
//h是新的链表这边的指针
DbListNode *h;
while(p!=begin)
{
m = p;
p = p->next;
h = begin->next;
while(h!=begin && m->exp>h->exp)
{
h = h->next;
}
if(h == begin)
{
end = m;
}
m->next = h;
m->pre = h->pre;
h->pre->next = m;
h->pre = m;
}
}
}
Status DbList::del_val(float y,int x)
{
DbListNode *p = begin->next;
while(p!=begin&&(p->exp!=x||p->coef!=y))
{
p = p->next;
}
if(p == begin)
{
cout<<"该值不存在..."<<endl;
return ERROR;
}
if(p == end)
{
end = p->pre;
}
p->pre->next = p->next;
p->next->pre = p->pre;
delete p;
--size;
return OK;
}
Status DbList::insert_val(float y,int x)
{
DbListNode *s = new DbListNode;
s->coef = y;
s->exp = x;
s->next = NULL;
s->pre = NULL;
DbListNode *p = begin->next;
while(p!=begin && p->exp<x)
{
p = p->next;
}
if(p == begin)
{
end = s;
}
s->next = p;
s->pre = p->pre;
s->pre->next = s;
p->pre = s;
++size;
return OK;
}
Status DbList::pop_front()
{
if(IsEmpty())
{
cout<<"链表已空(pop_front)..."<<endl;
return ERROR;
}
DbListNode *p = begin->next;
begin->next = p->next;
p->next->pre = begin;
delete p;
--size;
if(size == 0)
{
end = begin;
}
return OK;
}
Status DbList::pop_back()
{
if(IsEmpty())
{
cout<<"链表已空(pop_back)..."<<endl;
return ERROR;
}
end->pre->next = begin;
begin->pre = end->pre;
delete end;
end = begin->pre;
--size;
return OK;
}
void DbList::show_dblist()
{
sort();
DbListNode *p = begin->next;
while(p!=begin)
{
cout<<p->coef<<"X^"<<p->exp;
if(p->next != begin)
cout<<" + ";
p = p->next;
}
cout<<endl;
}
Status DbList::push_back(float y,int x)
{
DbListNode *s = new DbListNode;
s->exp = x;
s->coef = y;
s->pre = end;
s->next = end->next;
end->next = s;
s->next->pre = s;
end = s;
++size;
return OK;
}
void DbList::makeEmpty()
{
if(IsEmpty())
{
cout<<"链表已空(make empty)..."<<endl;
}
else
{
DbListNode *p = begin->next;
while(p!=begin)
{
p->pre->next = p->next;
p->next->pre = p->pre;
delete p;
p = begin->next;
}
end = begin;
size = 0;
}
}
核心加法运算:
void DbList::listadd(DbList *y1,DbList *y2)
{
float temp;
DbListNode *p1 = y1->begin->next;
DbListNode *p2 = y2->begin->next;
DbListNode *p;
while(p1!=y1->begin && p2!= y2->begin)
{
if(p1->exp > p2->exp)
{
push_back(p2->coef,p2->exp);
p2 = p2->next;
}
else if(p1->exp < p2->exp)
{
push_back(p1->coef,p1->exp);
p1 = p1->next;
}
else if(p1->exp == p2->exp)
{
temp = p1->coef + p2->coef;
push_back(temp,p2->exp);
p1 = p1->next;
p2 = p2->next;
}
}
if(p1 != y1->begin)
{
while(p1!=y1->begin)
{
push_back(p1->coef,p1->exp);
p1 = p1->next;
}
}
else if(p2 != y2->begin)
{
while(p2!=y2->begin)
{
push_back(p2->coef,p2->exp);
p2 = p2->next;
}
}
}
测试main函数:
#if 1
#include"mathical.h"
int main()
{
DbList y1;
DbList y2;
DbList y3;
y1.push_back(13,1);
y1.push_back(12,3);
y1.push_back(11,2);
y1.push_back(14,5);
cout<<"y1 = ";
y1.show_dblist();
y2.push_back(3,2);
y2.push_back(4,4);
y2.push_back(5,3);
cout<<"y2 = ";
y2.show_dblist();
y3.listadd(&y1,&y2);
cout<<"y3 = ";
y3.show_dblist();
}
#endif
测试结果: