前言
链表的原理:本质上是一个结构体中包含两个部分,内容项与地址项
用代码表示这一个格子:
struct node
{ int xis;
int zhi;
node* next;};
第一个格子中的地址项指向第二个格子的地址(注意是格子的地址,不是格子地址项的值)。第二个格子的元素要访问可以用->来进行。为了实现链表从头到尾的访问,一般用
node* p;
cout<<p;
p=p->next;
cout<<p;
打印的第一个p是第一个格子,进行p=p->next后打印的是第二个格子,注意这里的cout如果想打出p的内容需要对cout进行重载
代码思路:
定义一个链表(结构体),链表中定义一个指数项一个系数项和一个指针项,在类中运用类的构造函数进行链表的初始化。然后定义一个add函数,目的是进行两个多项式的相加,通过对指数项的比较来进行排序,最终用一个链表来接收最终两个多项式相加的结果,最后输出。
1.定义一个成员函数用以在构造函数中初始化链表
node* creatpoly()//创建多项式初始化函数
{
int xis;
int zhi;
node* head = NULL, * p = NULL;
p = head;
for (int i = 1; ; i++)
{
cout << "第" << i << "项系数(<999)";
cin >> xis;
if (xis > 999)
{
break;
}
cout << "第" << i << "项指数(<999)";
cin >> zhi;
if (zhi > 999)
{
break;
}
node* pnew = new node(1, 0);
pnew->xis = xis;
pnew->zhi = zhi;
pnew->next = NULL;
if (head == NULL)
{
head = pnew;
p = pnew;
}
else
{
p->next = pnew;
p = pnew;
}
}
return head;
}
2.创建一个多项式类
class poly //创建一个多项式类
{
public:
friend void del(poly& a);
friend void insertNode(node*& head, int xis, int zhi);//可以友元也可以成员
friend node* creatpoly();
friend void printp(poly p);//用到poly了,所以classpoly在前printp在后,同样可以友元也可以成员
friend void rela(poly pn, int x);
friend node* addPoly(node* poly1, node* poly2);
poly operator+ (poly& b);//只能用成员函数,因为友元函数无法用this指针。并且成员函数在外部定义时需poly在前
friend ostream& operator<<(ostream& cout, poly p);//只能用友元函数,因为ostream不是poly下的成员函数。
poly() { pstr = creatpoly(); }
poly(node* p) { pstr = p; }
~poly() {}
private:
node* pstr;
};
这里要注意的一点是对于<<运算符重载来说,不能将之定义为poly的成员函数,必须要将之定义为poly的友元函数,因为poly的成员函数要想调用就必须要先创建一个类,然后用p.来访问,不符合cout<<的要求
3.定义一个按指数的大小排队进入栈的函数
void insertNode(node*& head, int xis, int zhi)//创建插入函数,目的是实现多项式的排序
{
node* newNode = new node(xis, zhi);
if (head == nullptr) {
head = newNode;
return;
}
if (zhi > head->zhi) {
newNode->next = head;
head = newNode;
return;
}
node* a = head;
node* last = nullptr;
while (a != nullptr && zhi < a->zhi) {
last = a;
a = a->next;
}//a现在为空,或者zhi此时>a->zhi
if (a != nullptr && zhi == a->zhi) {
a->xis += xis;
delete newNode;
}
else {
last->next = newNode;
newNode->next = a;//a为空或者a的指数小于zhi
}
}
4.创建一个多项式相加的函数
node* addPoly(node* poly1, node* poly2) //实现多项式的相加
{
node* result = nullptr;
node* curr = nullptr;
while (poly1 != nullptr && poly2 != nullptr) {
if (poly1->zhi > poly2->zhi) {
insertNode(result, poly1->xis, poly1->zhi);
poly1 = poly1->next;
}
else if (poly1->zhi < poly2->zhi) {
insertNode(result, poly2->xis, poly2->zhi);
poly2 = poly2->next;
}
else {
int xisSum = poly1->xis + poly2->xis;
if (xisSum != 0) {
insertNode(result, xisSum, poly1->zhi);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != nullptr) {
insertNode(result, poly1->xis, poly1->zhi);
poly1 = poly1->next;
}
while (poly2 != nullptr) {
insertNode(result, poly2->xis, poly2->zhi);
poly2 = poly2->next;
}
return result;
}
5.主函数
int main()//主函数
{
poly p1;
cout << "第一个多项式为:";
//printp(p1);
cout << p1;
cout << endl;
poly p2;
cout << "第二个多项式为:";
cout << p2;
//printp(p2);
cout << endl;
poly p3(nullptr);
p3.pstr = addPoly(p1.pstr, p2.pstr);
cout << "二者相加为:";
cout << p3;
//printp(p3);
cout << endl;
cout << "请输入x的值";
int x;
cin >> x;
rela(p3, x);
}
总代码
#include <iostream>
using namespace std;
typedef struct node//创建一个链表
{
int xis;
int zhi;
node* next;
node(int xis, int zhi) : xis(xis), zhi(zhi), next(nullptr) {}
}node;
node* creatpoly()//创建多项式初始化函数
{
int xis;
int zhi;
node* head = NULL, * p = NULL;
p = head;
for (int i = 1; ; i++)
{
cout << "第" << i << "项系数(<999)";
cin >> xis;
if (xis > 999)
{
break;
}
cout << "第" << i << "项指数(<999)";
cin >> zhi;
if (zhi > 999)
{
break;
}
node* pnew = new node(1, 0);
pnew->xis = xis;
pnew->zhi = zhi;
pnew->next = NULL;
if (head == NULL)
{
head = pnew;
p = pnew;
}
else
{
p->next = pnew;
p = pnew;
}
}
return head;
}
void insertNode(node*& head, int xis, int zhi)//创建插入函数,目的是实现多项式的排序
{
node* newNode = new node(xis, zhi);
if (head == nullptr) {
head = newNode;
return;
}
if (zhi > head->zhi) {
newNode->next = head;
head = newNode;
return;
}
node* a = head;
node* last = nullptr;
while (a != nullptr && zhi < a->zhi) {
last = a;
a = a->next;
}//a现在为空,或者zhi此时>a->zhi
if (a != nullptr && zhi == a->zhi) {
a->xis += xis;
delete newNode;
}
else {
last->next = newNode;
newNode->next = a;//a为空或者a的指数小于zhi
}
}
node* addPoly(node* poly1, node* poly2) //实现多项式的相加
{
node* result = nullptr;
node* curr = nullptr;
while (poly1 != nullptr && poly2 != nullptr) {
if (poly1->zhi > poly2->zhi) {
insertNode(result, poly1->xis, poly1->zhi);
poly1 = poly1->next;
}
else if (poly1->zhi < poly2->zhi) {
insertNode(result, poly2->xis, poly2->zhi);
poly2 = poly2->next;
}
else {
int xisSum = poly1->xis + poly2->xis;
if (xisSum != 0) {
insertNode(result, xisSum, poly1->zhi);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != nullptr) {
insertNode(result, poly1->xis, poly1->zhi);
poly1 = poly1->next;
}
while (poly2 != nullptr) {
insertNode(result, poly2->xis, poly2->zhi);
poly2 = poly2->next;
}
return result;
}
class poly //创建一个多项式类
{
public:
friend void del(poly& a);
friend void insertNode(node*& head, int xis, int zhi);//可以友元也可以成员
friend node* creatpoly();
friend void printp(poly p);//用到poly了,所以classpoly在前printp在后,同样可以友元也可以成员
friend void rela(poly pn, int x);
friend node* addPoly(node* poly1, node* poly2);
poly operator+ (poly& b);//只能用成员函数,因为友元函数无法用this指针。并且成员函数在外部定义时需poly在前
friend ostream& operator<<(ostream& cout, poly p);//只能用友元函数,因为ostream不是poly下的成员函数。
poly() { pstr = creatpoly(); }
poly(node* p) { pstr = p; }
~poly() {}
public:
node* pstr;
};
void printp(poly p)//打印多项式
{
while (p.pstr)
{
if (p.pstr->zhi > 1 && p.pstr->xis != 1)
{
cout << p.pstr->xis << "X^" << p.pstr->zhi;
}
else if (p.pstr->xis != 0 && p.pstr->zhi == 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->xis == 1 && p.pstr->zhi != 1)
{
cout << "X^" << p.pstr->zhi;
}
else if (p.pstr->zhi == 0 && p.pstr->xis != 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->zhi == 1 && p.pstr->xis == 1)
{
cout << p.pstr->xis << "X";
}
else if (p.pstr->zhi == 0 && p.pstr->xis != 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->zhi == 1 && p.pstr->xis != 1)
{
cout << p.pstr->xis << "X";
}
if (p.pstr->xis > 0 && p.pstr->next != NULL && p.pstr->next->xis > 0)
{
cout << "+";
}
p.pstr = p.pstr->next;
}
}
void rela(poly pn, int x)//实现x的具体计算
{
double n = 0;
do
{
n = n + pow(x, pn.pstr->zhi) * pn.pstr->xis;
pn.pstr = pn.pstr->next;
} while (pn.pstr != NULL);
cout << "结果是" << n;
}
ostream& operator<<(ostream& cout, poly p)//cout<<运算符重载//注意poly千万不要加&,否则会删除链表
{
while (p.pstr)
{
if (p.pstr->zhi > 1 && p.pstr->xis != 1)
{
cout << p.pstr->xis << "X^" << p.pstr->zhi;
}
else if (p.pstr->xis != 0 && p.pstr->zhi == 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->xis == 1 && p.pstr->zhi != 1)
{
cout << "X^" << p.pstr->zhi;
}
else if (p.pstr->zhi == 0 && p.pstr->xis != 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->zhi == 1 && p.pstr->xis == 1)
{
cout << p.pstr->xis << "X";
}
else if (p.pstr->zhi == 0 && p.pstr->xis != 0)
{
cout << p.pstr->xis;
}
else if (p.pstr->zhi == 1 && p.pstr->xis != 1)
{
cout << p.pstr->xis << "X";
}
if (p.pstr->xis > 0 && p.pstr->next != NULL && p.pstr->next->xis > 0)
{
cout << "+";
}
p.pstr = p.pstr->next;
}
return cout;
}
poly poly::operator+(poly& b)
{
node* result = nullptr;
node* curr = nullptr;
while (this->pstr != nullptr && b.pstr != nullptr) {
if (this->pstr->zhi > b.pstr->zhi) {
insertNode(result, this->pstr->xis, this->pstr->zhi);
this->pstr = this->pstr->next;
}
else if (this->pstr->zhi < b.pstr->zhi) {
insertNode(result, b.pstr->xis, b.pstr->zhi);
b.pstr = b.pstr->next;
}
else {
int xisSum = this->pstr->xis + b.pstr->xis;
if (xisSum != 0) {
insertNode(result, xisSum, this->pstr->zhi);
}
this->pstr = this->pstr->next;
b.pstr = b.pstr->next;
}
}
while (this->pstr != nullptr) {
insertNode(result, this->pstr->xis, this->pstr->zhi);
this->pstr = this->pstr->next;
}
while (b.pstr != nullptr) {
insertNode(result, b.pstr->xis, b.pstr->zhi);
b.pstr = b.pstr->next;
}
poly end(result);
return end;
};
void del(poly& a)
{
while (a.pstr != nullptr)
{
if (a.pstr->next != nullptr)
{
node* b = a.pstr->next;
delete a.pstr;
a.pstr = b;
}
else
{
delete a.pstr;
}
}
}
int main()//主函数
{
poly p1;
cout << "第一个多项式为:";
//printp(p1);
cout << p1;
cout << endl;
poly p2;
cout << "第二个多项式为:";
cout << p2;
//printp(p2);
cout << endl;
poly p3(nullptr);
p3.pstr = addPoly(p1.pstr, p2.pstr);
cout << "二者相加为:";
cout << p3;
//printp(p3);
cout << endl;
cout << "请输入x的值";
int x;
cin >> x;
rela(p3, x);
}