Chapter II: 5. Linear Table Application---The representation and summation of one-element polynomial

Source: Internet
Author: User

Objective:

Learning the sequential structure and chain structure of linear tables, what is the use of this learning?

This section will learn how to use linear tables to achieve the representation and addition of a unary polynomial.

Directory:

1. One-dimensional polynomial

Body:

One-dimensional polynomial:

Mathematically, a unary polynomial can be written in the form of a power in ascending order:

Pn (x) = P0 + p1x1 + p2x2 + .... + pnxn

It is determined only by the n+1 coefficients, so that the linear table P can be used in a computer:

P= (P0,P1, P2, ....) PN);

The index of each of these items is hidden in the pi ordinal

The addition of a unary polynomial (with sequential storage structure):

Set M<n, the result of the addition of two polynomial is: Rn (x) = Pn (x) + Qm (x)

This operation can be represented by the phase table R:

R= (P0 + q0,p1+ Q1, P2 + Q2, .... + pm+ qm,pm+1,.... PN);

If you have mastered the sequential storage of linear tables, then you will find that the R P Q uses sequential storage structure, so that the algorithm of the addition of polynomial is very simple and clear, it is easy to implement.

The representation of a unary polynomial is added (using a chained storage structure):

The sequential structure is still used to represent the unary polynomial, so for the following polynomial:

S (x) = 1 + 3x1000 + 2x20000

The sequential storage structure opens up 20,001-length storage space, but this polynomial has only 3 items, resulting in a huge waste of memory space.

Improvements: Using a chained storage structure to represent a unary polynomial, because the chained storage data element is not adjacent in the physical location, at this time can not be determined by the bit order to determine the exponent of the polynomial, then the linked list only stores a unary polynomial non 0 items, each node stores two data fields: (item: PI, index: EI) A pointer field: next.

In this form, the worst-case scenario is that the polynomial has no non-0 items, that is, n+1 items, which consumes twice times the storage space of the sequential structure, but compared with the existence of the S (X) type polynomial, the form of a single-element polynomial with a linked list will greatly save the storage space.

Storage structure diagram:

    

The storage structure expressed in C language:

typedef struct polynomial{
float Coef; Coefficient
int expn; Index
struct Lnode *next; The pointer field of the node
}polynomial,*linklist;

Code implementation:

#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE-1
#define OVERFLOW-2
Status is the type of function whose value is the function result status code
typedef int STATUS;

typedef struct polynomial{
float Coef; Coefficient
int expn; Index
struct polynomial *next; The pointer field of the node
}polynomial,*linklist;

Create a unary multi-form with m items
Status Creatpolyn (linklist &l,int m) {
polynomial * Q=L; Q points to the head node (the last node).

float Coef;
int expn;
for (int i=0;i<m;i++) {
printf ("%s", "Please enter factor:");
scanf ("%f", &coef);
printf ("%s", "Please enter index:");
scanf ("%d", &AMP;EXPN);

Polynomial * polyn= (linklist) malloc (sizeof (polynomial));
if (! Polyn) return ERROR;
polyn->coef=coef;
polyn->expn=expn;

q->next=polyn; Append nodes.
Q=polyn; Point to the last node
}
q->next=null;
return OK;
}

The polynomial PA and PB are added together to store the results in the PA chain.
void Addpolyn (linklist &la,linklist &lb) {
polynomial * QA=LA;
polynomial * QB=LB;
while (Qa->next&&qb->next) {
1. If the current QA index is greater than the QB index. Then insert the QB point to the point where the QA is pointing,
if ((QA-&GT;NEXT-&GT;EXPN) > (QB-&GT;NEXT-&GT;EXPN)) {
Polynomial * temp=qb->next;
qb->next=qb->next->next; Remove the current node from the QB

temp->next=qa->next; Insert the deleted node in the QB before the current element of the QA
qa->next=temp;
}

2. If the current QA index is less than the QB index. Then just move the QA pointer back.
if ((QA-&GT;NEXT-&GT;EXPN) < (QB-&GT;NEXT-&GT;EXPN)) {
qa=qa->next;
}

3. If the current QA index is equal to the QB index. So let's add the corresponding coefficients.
if ((qa->next->expn) = = (QB-&GT;NEXT-&GT;EXPN)) {
Float temp= (QA-&GT;NEXT-&GT;COEF) + (QB-&GT;NEXT-&GT;COEF);

Remove the current node from QA separately
if (temp==0) {
qa->next=qa->next->next;
}

Modifies the factor of the current node of the QA and moves the pointer back.
if (temp!=0) {
qa->next->coef=temp;
qa=qa->next;
}

qb=qb->next;
}
}

}

void Printallvalues (linklist &l) {
polynomial * Q=L; Q Point to head node
while (Q->next) {
printf ("Factor:%f", Q-&GT;NEXT-&GT;COEF);
printf ("Index:%d\n", Q-&GT;NEXT-&GT;EXPN);
q=q->next;
}
}

void Main () {
Polynomial * pa= (linklist) malloc (sizeof (polynomial));
pa->next=null;
printf ("%s\n", "Pa:");
Creatpolyn (pa,4);
Printallvalues (Pa);

Polynomial * pb= (linklist) malloc (sizeof (polynomial));
pb->next=null;
printf ("\n%s\n", "Pb:");
Creatpolyn (pb,3);
Printallvalues (PB);

Addpolyn (PA,PB);
printf ("\n%s\n", "PA+PB:");
Printallvalues (Pa);
}

Operation Result:

      

    

Chapter II: 5. Linear Table Application---The representation and summation of one-element polynomial

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.