Data Structure Course notes--(2) unary polynomial and multiplication

Source: Internet
Author: User

1. Topics

Read------Linked list

#include <stdio.h> #include <malloc.h> #define LEN sizeof (struct polynode) struct polynode{int coef;
    int expon;
struct Polynode *next;
} ;
int n;
    struct Polynode * creat (int N) {//4 3 4-5 2 6 1-2 0 struct Polynode *head;
    struct Polynode *p1,*p2;
    n = 1;
    P1 = P2 = (struct Polynode *) malloc (LEN);
            while (n <= N) {if (n==1) {scanf ("%d%d", &p1->coef,&p1->expon);
        HEAD=P1;
            }else{P2 = p1;
            P1 = (struct Polynode *) malloc (LEN);
            scanf ("%d%d", &p1->coef,&p1->expon);
        p2->next=p1;
    } n = n+1;
    } p1->next = NULL;
return (head);
    } void print (struct polynode *head) {struct Polynode *p;
    printf ("\nnow,there%d records are:\n", N);
    p = head;
            if (head! = NULL) do {printf ("%d%d", p->coef,p->expon);
        p = p->next;
}while (P!=null);
 } void Main () {   int N;
    struct Polynode *head1;
    struct Polynode *head2;
    struct Polynode *head;
    scanf ("%d", &n);
    Head1 = creat (N);
    scanf ("%d", &n);
    Head2 = creat (N);
Print (HEAD1);
Print (HEAD2); }

----------multiplication and addition of polynomial

1. #include  <stdio.h>   2. #include  <stdlib.h>    3.   4. typedef struct node{   5.    int coefficient;   6.     int exponent;   7.    struct node * next;    8.}  PolyNode, *Polynomial;   9.   10.polynomial readpoly ();   11.void  attach (int c, int e, polynomial * rear);   12.Polynomial  Multpoly (POLYNOMIAL&NBSP;P1,&NBSP;POLYNOMIAL&NBSP;P2);   13.polynomial addpoly (Polynomial 
P1,&NBSP;POLYNOMIAL&NBSP;P2);   14.void printpoly (polynomial p);   15.   16.int main (int argc, char const *argv[])    17. {   18.    polynomial poly1, poly2, polysum, polymul;    19.&NBSP;&NBSp 20.    poly1 = readpoly ();   21.    poly2 =  readpoly ();   22.    polymul = multpoly (Poly1, Poly2);    23.    printpoly (Polymul);   24.    polysum =  addpoly (poly1, poly2);   25.    printpoly (PolySum);   26.    27.    return 0;   28.}    29.   30.polynomial readpoly ()    31. {   32.    polynomial p, rear, temp;   33.     P =  (polynode*) malloc (sizeof (Polynode));   34.    p-> next = null;   35.    rear = p;   36.     int N, c, e;   37.    scanf ("%d", &Nbsp;&n);   38.    while (n--) {   39.      &NBSP;&NBSP;&NBSP;SCANF ("%d %d",  &c, &e);   40.         attach (c, e, &rear);   41.    }   42.     temp = P;   43.    p = p->next;     44.    free (temp);   45.    return p;    46.}    47.   48.void attach (int c, int e, polynomial * prear)    49. {   50.    polynomial p;   51.    p =   (polynode*) malloc (sizeof (Polynode));   52.    p->coefficient =  c;   53.    p->exponent = e;   54.    p->next = null;   55.     (*pRear)->next  = P;   56.    *prear = p;   57.}    58.   59.polynomial multpoly (POLYNOMIAL&NBSP;P1,&NBSP;POLYNOMIAL&NBSP;P2)     60. {   61.    polynomial p, temp1, temp2, rear, temp;    62.    int c, e;   63.    if (! p1 | |  ! P2)    64.        return null;   65.     temp1 = P1;   66.    temp2 = p2;   67.     P =  (polynode*) malloc (sizeof (Polynode));   68.     P->next = NULL;   69.    rear = p;   70.     while (TEMP2) {   71.        c = temp1-> coefficient * temp2->coefficient;   72.        e  = temp1->exponent + temp2->exponent;   73.         if (c != 0) {   74.             attach (c, e, &rear);   75.             temp2 = temp2->next;   76.         }   77.    }   78.    temp1 =  temp1->next;   79.    while (TEMP1) {   80.         temp2 = P2, Rear = P;   81.      &nbSp;  while (TEMP2) {   82.             c = temp1->coefficient * temp2->coefficient;   83.             e = temp1->exponent + temp2-> exponent;   84.            if (c !=  0) {   85.              
  while (rear->next && rear->next->exponent > e)    86.                     Rear = Rear->next;   87.                 if (rear->next && rear->next-> exponent ==  e) {   88.                     if (rear->next->coefficient + c)    89.                          Rear->next->coefficient += c;   90.                     else{   91.                          temp = Rear->next;   92.                          Rear->next = temp->next;   93.                         free (temp);   94.                      }   95.                 }   96.                 else{   97.                     temp =  (polynode*) malloc (sizeof (Polynode));    98.                     temp->coefficient = c;   99.                     temp->exponent  = e; &nbsP 100.                     temp->next = Rear->next;   101.                     Rear->next =  temp;   102.                     Rear = Rear->next;   103.                 }   104.                 temp2 = temp2->next ;   105.            }   106.         }   107.         temp1 = temp1->next;   108.    }   109.    temp =  P;   110.    p = p->next;   111.     free (temp);   112.    return p;   113.}    114.   115.polynomial addpoly (POLYNOMIAL&NBSP;P1,&NBSP;POLYNOMIAL&NBSP;P2)     116. {   117.    polynomial p, temp1, temp2, rear, temp;    118.    if (! p1 && ! P2) {   119.        if (! P1)    120.            return p2;    121.        else   122.             return P1;   123.    }   124.    p =  (polynode*) malloc (sizeof (Polynode));   125.     P->next = NULL;   126.    rear = p ;   127.    temp1 = p1;   128.&NBSP;&NBSP;&NBSP;&NBSP;TEMP2
 = P2;   129.    while (TEMP1&NBSP;&AMP;&AMP;&NBSP;TEMP2) {   130.        if (temp1->exponent > temp2->exponent) {    131.            if (temp1-> coefficient) {   132.                 attach (temp1->coefficient, temp1->exponent, &rear);                   133.             }   134.             temp1 = temp1->next;   135.        }    136.        else if (temp1->exponent == temp2- >exponent) {   137.            if ( temp1->coefficient + temp2->coefficient) {   138.                 attach (temp1->coefficient + temp2- >coefficient, temp1->exponent, &rear);   139.             }   140.             temp1 = temp1->next;   141.             temp2 = temp2->next;   142.        }    143.        else{   144.             if (temp2->coefficient) {   145.                 attach (Temp2->coefficient,  temp2->exponent, &rear);                   146.             }   147.            temp2 =  temp2->next;   148.        }   149.     }   150.    while (TEMP1) {   151.       &nbSp; attach (temp1->coefficient, temp1->exponent, &rear);   152.         temp1 = temp1->next;   153.    }    154.    while (TEMP2) {   155.         attach (temp2->coefficient, temp2->exponent, &rear);   156.         temp2 = temp2->next;   157.     }   158.    temp = p;   159.    p  = P->next;   160.    free (temp);   161.     return P;   162.}    163.   164.void printpoly (polynomial p)    165. {   166.    int flag = 0;   167.     if (! P) {   168.        printf ("0 0");   169.     }   170.    while (P) {   171.         if  (!flag)    172.             flag = 1;   173.        else    174.            printf (" ");    175.        printf ("%d %d",  p->coefficient,  p->exponent);   176.        p = p->next
;   177.    }   178.    printf ("\ n");   179.}  


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.