Polynomial combined to achieve single chain (descending output index)

#include<stdio.h>
#include<malloc.h>
#define ElemType int
typedef struct ployNode{
 int coef,index;
 struct ployNode *next;
};


* insert_ployNode ployNode (ployNode L *, * ployNode newp) {
 // Insert descending exponential manner
 ployNode P *, Q *;
 IF (L-> Next == NULL) {
  newp-> Next = NULL;
  L-> Next newp =;
 } the else {
  for (Q = L, L-P => Next; P; P = p-> Next) comparison of major achieved node // {
   IF (newp-> index> p-> index) {
    Q -> Next newp =;
    newp-> Next = P;
    BREAK;
   }
   the else IF (newp-> p-index ==> index) {
    p-> Coef + = newp-> Coef; combining like terms //
    free (newp) ; // release new node
    BREAK;
   }
   the else {
    Q = P; // If a new node <existing node, continue forward lookup
   } 
  }
  IF (P == NULL) {// If the search is completed is still not found to be inserted place, the insertion tail
   newp-> Next = NULL;
   Q-> Next newp =;
  }
 }
 return L;
}

 


* create_ployNode ployNode (ployNode * L) {
 // the random polynomial with insert_ployNode descending inserted into the L,
 // orderly list created
 ployNode tail *, * newp;
 int Coef, index, COUNT;
 
 // Create head node
 L = (ployNode *) the malloc (the sizeof (ployNode));
 L-> Next = NULL;
 
 the printf ( "Please enter the number of terms of the polynomial \ n-");
 Scanf ( "% D", & COUNT);
 
 for (int I = 0; I <COUNT; I ++) {
  the printf ( "\ n-% d of item coefficients Coef =", I +. 1);
  Scanf ( "% d", & Coef);
  the printf ( "% d of indices of = index ", I +. 1);
  Scanf ("% D ", & index);
  
  newp = (ployNode *) the malloc (the sizeof (ployNode)); // Create a node
  newp-> Coef = Coef;
  newp-> index = index ;
  
  L = insert_ployNode (L, newp); 
 }
 return L;  
}


* merge_similarNode ployNode (ployNode * La, Lb ployNode *) {
 // do not destroy the combined polynomials are Lb La, Lb.
 
 * P ployNode, * newp;
 for (P = Lb-> Next; P; P = p-> Next) {
  newp = (ployNode *) the malloc (the sizeof (ployNode));
  newp-> = p-Coef> Coef;
  newp-> = p-index> index;
  La = insert_ployNode (La, newp); // function implemented using insert_ployNode
  // polynomial addition
 }
 return La;
}


void print(ployNode *L){
 ployNode *p;
 for(p=L->next;p;p=p->next){
  printf("conf=%d,index=%d\n",p->coef,p->index);
 }
}


int main(){
 ployNode *La,*Lb;
 La=create_ployNode(La);
 print(La);
 printf("\n");
 Lb=create_ployNode(Lb);
 print(Lb);
 printf("\n");
 La=merge_similarNode(La,Lb);
 print(La);
 return 0;
}

Guess you like

Origin www.cnblogs.com/jiafeng1996/p/11291984.html