Data structures - polynomial summation linked list implementation

Data Structure Course Zhejiang University Lecture Exercise: Using a linked list to achieve polynomial sum and product, just write a sum, being the product did not write.

#include <the iostream> 
#include <stdio.h>
 the using  namespace STD; 

// to the Polynomial is typedef aliases PolyNode * type declaration 
typedef struct PolyNode * to the Polynomial;
 struct PolyNode 
{ 
    int Coef;
     int expon; 
    to the Polynomial Link; 
}; 

void the Attach ( int C, int E, to the Polynomial * pRear) / * * pRear is a pointer to a pointer, where to passed by value to change the value Rear of * / 
{ 
    to the Polynomial P; 

    P = (to the Polynomial) the malloc ( the sizeof ( struct PolyNode) );/ * Create a new node, for storing the current polynomial * / 
    P -> Link = NULL; / * because nodes are inserted into each end of the list, so that it pointer to NULL * / 
    P -> C = Coef; / * for the new node assignment * / 
    P -> expon = E; 
    ( * pRear) -> Link = P;
     * = P pRear; / * ---------------- change point Rear the current end of the list ----------------- * / 
} 

to the Polynomial ReadPoly () 
{ 
    to the Polynomial P, T; 
    to the Polynomial Rear; / * current entry pointer of the end result of the expression * / 
    int C, E, N; 

    Scanf ( " % D " , & N); / *There are several polynomials read * / 
    P = (to the Polynomial) the malloc ( the sizeof ( struct PolyNode)); the P-> Link = NULL; / * use application as an empty node list header * / 
    Rear = P; / * current node pointer imparting Rear * / 
    iF (N < . 1 ) / * check whether the input data is correct * / 
        COUT << " iNPUT iS error " << endl;
     the while (the N-- ) { 
        Scanf ( " % D% D " , & C , & E); / * read coefficients of the polynomial and the index * / 
        the Attach (C, E, & Rear);/ * Current item into the polynomial * / 
    } 
    T = P; the P-P => Link; Free (T); / * release beginning APPLICATIONS empty node * / 
    / * ........... ...... * / 
    return P; 
} 

to the Polynomial the Add (to the Polynomial Pl, P2 to the Polynomial) 
{ 
    to the Polynomial T1, T2, P; 
    to the Polynomial Rear; 

    T1 = Pl; P2 = T2; / * Pl and P2 point to the header, can not change its value, or can not find the list * / 
    P = (to the Polynomial) the malloc ( the sizeof ( struct PolyNode)); the P-> Link = NULL; / * use application as an empty node list header * / 
    Rear = P;
     the while ( t1 &&t2) {
        if (t1->expon == t2->expon) {
            if ((t1->coef + t2->coef) != 0){
                Attach(t1->coef + t2->coef, t1->expon, &Rear);
                t1 = t1->link;
                t2 = t2->link;
            }
            else {
                t1 = t1->link;
                t2 = t2->link;
            }
        }
        else if (t1->expon > t2->expon) {
            Attach(t1->coef, t1->expon, &Rear);
            t1 = t1->link;
        }
        else {
            Attach(t2->coef, t2->expon, &Rear);
            t2 = t2->link;
        }
    }
    while (t1) {
        Attach(t1->coef, t1->expon, &Rear); 
        t1 = t1->link;
    }
    while (t2){
        Attach(t2->coef, t2->expon, &Rear);
        t2 = t2->link;
    }
    Polynomial t;
    t = P; P = P->link; free(t);
    return P;
}

void PrintPoly(Polynomial P)
{
    int flag = 0;
    
    if (!P) { printf("0 0\n"); return; }

    while (P){
        if (!flag)
            flag = 1;
        else
            printf(" ");
        printf("%d %d", P->coef, P->expon);
        P = P->link;
    }
    printf("\n");
} 

Int main () 
{ 
    to the Polynomial Pl, P2, P; 
    Pl = ReadPoly (); 
    P2 = ReadPoly (); 
    P = the Add (Pl, P2); 
    
    // to the Polynomial T; / * release header (header why nodes will not release) * /
     // T = P; the P-P => Link; Free (T); 

    PrintPoly (P); 
    the printf ( " % D% D " , the P-> Coef, the P-> expon) ;
     return  0 ; 
}

to sum up:

1. The header pointer can not move, otherwise it will not find the entry list

2. Application empty node as the header, after inserting the data should release the memory node (Free)

3. The application memory using malloc

  Baidu Encyclopedia: apply a continuous of the specified size memory block area allocated to void * return type of memory area address, when not know the specific location of the memory when you want to bind the real memory space, you need to use dynamic memory allocation

  Usage Example: P = (Polynomial) malloc (sizeof (struct PolyNode)); // Polynomial is struct PolyNode *, typeof alias, P is the storage location of this node in memory

4. pointer field on the list, is stored in the pointer, the pointer to the storage position in the general list of a node

  P1 = (Polynomial)malloc(sizeof(struct PolyNode));

  For example P is the current node, node P1 after the application, so that P-> link = P1, i.e., to achieve a new node is inserted as a linked list

Guess you like

Origin www.cnblogs.com/sunrise-to-set/p/10945115.html