//addition.c
#include <stdio.h>
#include <stdlib.h>
#include "struc.h"


void addition_pol(ptr_liste L1, ptr_liste L2, ptr_liste *Lo)
{
    ptr_liste newstruct;//temporaire pour une nouvelle structure
    ptr_liste temp;//temporaire de Lo
    ptr_liste tempL1=NULL;//temporaire de la liste chainé L1
    ptr_liste tempL2=NULL;//temporaire de la liste chainé L2


     //On vérifie si l'une des deux listes est Null
      if (L1==NULL)
       {
          *Lo=L2;
       }
       else if (L2==NULL)
       {
           *Lo=L1;
       }
       else
       {
       tempL1=L1;
       tempL2=L2;

               while  (tempL1!=NULL && tempL2!=NULL)
               {
                      newstruct=(ptr_liste)malloc(sizeof(list));
                      newstruct->suivant=NULL;

                  //1er cas
                        if (tempL1->exp > tempL2->exp)
                        {
                               newstruct->coef=tempL1->coef;
                               newstruct->exp=tempL1->exp;
                               tempL1=tempL1->suivant;

                                if (*Lo==NULL)
                                {
                                      *Lo=newstruct;
                                      temp=*Lo;
                                }
                                else
                                {
                                       temp->suivant=newstruct;
                                       temp=temp->suivant;

                                }
                        }

                  //2 eme cas
                        else if (tempL1->exp < tempL2->exp)
                        {
                               newstruct->coef=tempL2->coef;
                               newstruct->exp=tempL2->exp;
                               tempL2=tempL2->suivant;

                               if (*Lo==NULL)
                               {
                                  *Lo=newstruct;
                                  temp=*Lo;
                               }
                               else
                               {
                                  temp->suivant=newstruct;
                                  temp=temp->suivant;
                               }
                       }
                  //3 eme cas
                      else if (tempL1->exp == tempL2->exp)
                      {
                              newstruct->coef=(tempL2->coef) + (tempL1->coef);
                              newstruct->exp=tempL2->exp;
                              tempL2=tempL2->suivant;
                              tempL1=tempL1->suivant;
                              if (*Lo==NULL)
                              {
                                 *Lo=newstruct;
                                 temp=*Lo;
                              }
                              else
                              {
                                  temp->suivant=newstruct;
                                  temp=temp->suivant;
                              }
                      }
               }

               while (tempL1!=NULL)
               {
                     newstruct=(ptr_liste)malloc(sizeof(list));
                     temp->suivant=newstruct;
                     newstruct->suivant=NULL;
                     newstruct->exp=tempL1->exp;
                     newstruct->coef=tempL1->coef;
                     temp=temp->suivant;
                     tempL1=tempL1->suivant;
               }
               while (tempL2!=NULL)
               {
                     newstruct=(ptr_liste)malloc(sizeof(list));
                     temp->suivant=newstruct;
                     newstruct->suivant=NULL;
                     newstruct->exp=tempL2->exp;
                     newstruct->coef=tempL2->coef;
                     temp=temp->suivant;
                     tempL2=tempL2->suivant;
               }
    }

}


