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

/////////////////////////////////////////////////////////////////////////
//Crée le 22/11/2004                                                   //
//Par Balmes regis                                                     //
//    Becker sebastien                                                 //
//                                                                     //
//Objectif: Additioner deux listes triées L1 et L2 et retourner le     //
//          resultat                                                   //
//                                                                     //
//                                                                     //
//Entrée: L1 et L2 de type  ptr_liste                                  //
//Sortie: Lo de type ptr_liste                                         //
/////////////////////////////////////////////////////////////////////////

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)
       {
       
       ////On ne se limite pas à faire une copie de pointeur
       //car si on libere L1 ou L2, il y aura un probleme
       //Donc on copie chacun des éléments de L2 dans Lo
       //De meme lorsque la liste L2 est NULL

          tempL2=L2;
          while (tempL2!=NULL)
          {
          newstruct=(ptr_liste)malloc(sizeof(list));
          newstruct->suivant=NULL;
          newstruct->exp=tempL2->exp;
          newstruct->coef=tempL2->coef;
                    if (*Lo==NULL)
                    {
                               *Lo=newstruct;
                               temp=*Lo;   
                    }
                    else
                    {
                        temp->suivant=newstruct;
                        temp=temp->suivant;
                    }
              tempL2=tempL2->suivant;     
          }   

       }
       else if (L2==NULL)
       {
       

          tempL1=L1;
          while (tempL1!=NULL)
          {
          newstruct=(ptr_liste)malloc(sizeof(list));
          newstruct->suivant=NULL;
          newstruct->exp=tempL1->exp;
          newstruct->coef=tempL1->coef;
                    if (*Lo==NULL)
                    {
                               *Lo=newstruct;
                               temp=*Lo;   
                    }
                    else
                    {
                        temp->suivant=newstruct;
                        temp=temp->suivant;
                    }
              tempL1=tempL1->suivant;
          }   


       }
       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;
               }
    }

}


