A meta-polynomial summation C + + implementation of one-link list

Source: Internet
Author: User

A meta-polynomial summation of one-linked list

A unary polynomial summation single-linked list implements pseudo-code

1. Work pointer Pre, p, Qre, Q initialization
2. while (P exists and q exists) perform one of the following three situations:
2.1, if P->exp < q->exp: The pointer p moves back;
2.2, if P->exp > Q->exp, then
2.2.1, the node q is inserted before the node p.
2.2.2, pointer p points to the next node of his original point;
2.3, if p->exp = = Q->exp, then
2.3.1, P->coef = P->coef + Q->coef
2.3.2, if P->coef = = 0, do the following, otherwise the pointer p moves back,
2.3.2.1, delete node p
2.3.2.2 the pointer p to the next node of its original point
2.3.3, delete node q
2.3.4 The pointer q points to the next node of its primary node.
3. If q is not empty, link the node Q to the back of the first single-linked list.
One or one-tuple polynomial summation single-linked list implementation header file: PolynomialOfOneIndeterminateAdd.h

Unary Polynomial summation header file
#include <iostream>
using namespace Std;
Template<class datatype>
Defining a single-Link table node
struct Node
{
Data fields: coefficients and indices for non-0 items
DataType COEF, exp;
Pointer field
Node<datatype> *next;
};
Define a class that holds a unary polynomial
Template<class datatype>
Class Linklist
{
Private
Node<datatype> *first;
The number of items in a unary polynomial
int size;
Public
constructor function
Linklist ();
Initializing a unary polynomial
void Init ();
Output unary polynomial
void Print ();
Define addition operations for unary polynomial
Linklist<datatype> operator+ (linklist &p2);
};

constructor function
Template<class datatype>
Linklist<datatype>::linklist ()
{
First = new node<datatype>;
first = NULL;
size = 0;
}

Realization of initialization of single-linked list with unary polynomial
Template<class datatype>
void Linklist<datatype>::init ()
{
cout << "The number of elements of the polynomial is:";
CIN >> size;
DataType x, y;
cout << "Please enter the 1th factor:";
CIN >> X;
cout << "Please enter the index of item 1th:";
Cin >> y;
Node<datatype> *m;
m = new node<datatype>;
M->coef = x;
M->exp = y;
M->next = NULL;
First = m;
for (int i = 2; I <= size; i++)
{
cout << "Please enter the factor for" << I << ":";
CIN >> X;
cout << "Please enter the index of the" << I << "item:";
Cin >> y;
Node<datatype> *n;
n = new node<datatype>;
N->coef = x;
N->exp = y;
N->next = NULL;
M->next = n;
m = n;
}
}

Realization of the output of a unary polynomial single-link list
Template<class datatype>
void Linklist<datatype>::P rint ()
{
node<datatype> *m = First;
while (m! = NULL)
{
if (m = = first)
{
if (M->coef! = 0 && M->exp! = 0)
{
cout << m->coef << "x^" << m->exp;
}
else if (m->coef! = 0 && M->exp = = 0)
{
cout << m->coef;
}
}
Else
{
if (M->coef > 0 && m->exp! = 0) {
cout << "+" << m->coef << "x^" << m->exp;
}
else if (m->coef<0 && m->exp! = 0)
{
cout << m->coef << "x^" << m->exp;
}
else if (m->coef>0 && m->exp = = 0)
{
cout << "+" << m->coef;
}
else if (M->coef < 0 && M->exp = = 0)
{
cout << m->coef;
}
}
m = m->next;
}
cout << Endl;
}

Implementing the addition of a single-linked list of unary polynomial
Template<class datatype>
Linklist<datatype> linklist<datatype>::operator+ (linklist &p2)
{
Declaring work pointers
Node<datatype> *pre, *p, *qre, *q;
Initializing the work pointer
Pre = this->first;
p = pre->next;
Qre = P2.first;
Q = qre->next;
while (P! = null&&q = NULL)
{
P->exp < q->exp: pointer p move back
if (P->exp < q->exp)
{
Pre = P;
p = p->next;
}
P->exp > Q->exp: Before inserting the node Q into node p, the pointer p points to the next node of his original point
if (P->exp > Q->exp)
{
Node<datatype> *s;
s = q->next;
Pre->next = q;
Q->next = p;
Q = s;
}
P->exp = = Q->exp:
if (p->exp = = q->exp)
{
P->coef = P->coef + Q->coef
P->coef = P->coef + q->coef;
if (P->coef = = 0)
{
Point the pointer p to the next node of its original point
Pre->next = p->next;
Delete Node P
Delete p;
p = p->next;
}
Else
{
Pre = P;
p = pre->next;
}
Point the pointer q to the next node of its original point
Qre->next = q->next;
Delete Node Q
Delete q;
Q = qre->next;
}
}
If q is not empty, link the node Q to the back of the first single-linked list.
if (q! = NULL)
{
Pre->next = q;
Delete P2.first;
}
return *this;
}

Second, the test of one-dollar polynomial single-linked list realization of the source program: TestPolynomialOfOneIndeterminateAdd.cpp

#include <iostream>
Introduction of a single-linked list of one-element polynomial for the implementation of header files
#include "PolynomialOfOneIndeterminateAdd.h"
using namespace Std;

int main ()
{
Declaring a unary polynomial single-linked list
linklist<int> P1, p2, p3;
cout << "Please define the polynomial a (x) by the order of the exponent from small to large:" << Endl;
P1. Init ();
cout << "Input polynomial A (x) is:";
P1. Print ();
cout << "\ n Please define the polynomial B (x) According to the order of the exponent from small to large:" << Endl;
P2. Init ();
cout << "Input polynomial B (x) is:";
P2. Print ();
One-tuple polynomial add-ons
P3 = p1 + p2;
cout << "\ n polynomial a (x) and polynomial B (x) as:" << Endl;
P3. Print ();
return 0;
}

A meta-polynomial summation C + + implementation of one-link list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.