One of the data structures meta-polynomial addition (c + + version)

Source: Internet
Author: User

#include <iostream>
#include <stdlib.h>
using namespace Std;

typedef struct lnode{
float Coef; Coefficient
int expn; Index
struct Lnode *next; Pointer field
}lnode, *slink;

int main ()
{
Unary polynomial addition, input two unary polynomial LA, Lb, output two unary polynomial, and LC
Slink La, Lb, Lc, PA, Pb, PC, RA, RB, q;
int m, N, J;
cout << "Please enter the number of items in a unary polynomial la:";
Cin >> m;
La = new Lnode;
RA = La;
if (! La) exit (0); Storage space allocation failed
cout << "input" << m << "coefficient index of items:";
for (j = 0; J < m; J + +)
{
PA = new Lnode; To generate a new node
CIN >> pa->coef>> pa->expn;
Ra->next = PA; RA = pa; Insert to table header
}
Ra->next = NULL;
cout << "Please enter the number of items in a unary polynomial lb:";
CIN >> N;
Lb = new Lnode;
RB = Lb;
if (! Lb) exit (0); Storage space allocation failed
cout << "Input" << n << "coefficient index of items:";
for (j = 0; J < N; j + +)
{
PB = new Lnode; To generate a new node
CIN >> Pb->coef >> pb->expn;
Rb->next = PB; RB = PB; Insert to table header
}
M + = n;
Rb->next = NULL;
PA = la->next; PB = lb->next; Lc = PC = La; Q = lc->next;
while (PA && pb)//Insert PA,PB node in C at once
{
if (PA-&GT;EXPN < PB-&GT;EXPN)
{
Pc->next = PA; PC = PA; PA = pa->next;
}
else if (pa->expn > PB-&GT;EXPN)
{
Pc->next = PB; PC = PB; PB = pb->next;
}
Else
{
Pa->coef = Pa->coef + pb->coef; Pc->next = PA; PC = PA; PA = pa->next; PB = pb->next;
m--;
}
}
Pc->next = PA? PA:PB;

Delete Lb; Releasing the LB head node
j = 0;
cout << "21-Element polynomial and: Lc =";
while (J < m)
{
cout << q->coef << "x^" << q->expn;
if (J < m-1)
cout << "+";
Q = q->next;
j + +;
}
cout << Endl;
}//mergelist_l

One of the data structures meta-polynomial addition (c + + version)

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.