Skip to content
33 changes: 33 additions & 0 deletions Headers_Fcts/dlistHeader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef HEADER
#define HEADER

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct celluleDouble
{
int element;
struct celluleDouble * suivant;
struct celluleDouble * precedent;
};
typedef struct celluleDouble celluleDouble;

struct listeDouble
{
celluleDouble * debut;
celluleDouble * fin;
unsigned longueur;
};
typedef struct listeDouble listeDouble;


#endif

int listeDoubleVide(listeDouble);
void affichageListeDouble(listeDouble);
void ajoutDebut(int, listeDouble *);
void ajoutFin(int,listeDouble *);
void suppressionDebut(listeDouble *);
void suppressionFin(listeDouble *);
void libererListeDouble(listeDouble);
Binary file added Headers_Fcts/dlistHeader.h.gch
Binary file not shown.
118 changes: 118 additions & 0 deletions Headers_Fcts/dlistfcts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "dlistHeader.h"


int listeDoubleVide(listeDouble L)
{
if(L.debut==NULL)
return 1;
return 0;
}


void affichageListeDouble(listeDouble L)
{
if(listeDoubleVide(L)==1)
{
printf("[]\n");
return;
}
celluleDouble *temp=L.debut;
printf("[");
while(temp->suivant!=NULL)
{
printf("%d, ",temp->element);
temp=temp->suivant;
}
if(temp!=NULL)
printf("%d",temp->element);
printf("]\n");
}



void ajoutDebut(int x,listeDouble * L)
{
celluleDouble * C=(celluleDouble *)malloc(sizeof(celluleDouble));
C->element=x;
C->suivant=L->debut;
C->precedent=NULL;

if(listeDoubleVide(*L)==1)
{
L->debut=C;
L->fin=C;
L->longueur=1;
return;
}
L->debut->precedent=C;
L->debut=C;
L->longueur+=1;
return;
}


void ajoutFin(int x,listeDouble *L)
{
celluleDouble * C=(celluleDouble *)malloc(sizeof(celluleDouble));
C->element=x;
C->precedent=L->fin;
C->suivant=NULL;

if(listeDoubleVide(*L)==1)
{
ajoutDebut(x,L);
return;
}
L->fin->suivant=C;
L->fin=C;
L->longueur+=1;
return;
}

void suppressionDebut(listeDouble * L)
{
if(listeDoubleVide(*L)==1)
return;
if(L->longueur==1)
{
L->longueur=0;
if(L->debut!=NULL)
free(L->debut);
L->debut=NULL;
return;
}
celluleDouble * temp=L->debut;
L->debut=L->debut->suivant;
L->longueur-=1;
free(temp);
return;
}


void suppressionFin(listeDouble * L)
{
if(listeDoubleVide(*L)==1)
return;
if(L->longueur==1)
{
suppressionDebut(L);
return;
}
celluleDouble * temp=L->fin;
L->fin=L->fin->precedent;
L->fin->suivant=NULL;
L->longueur-=1;
free(temp);
return;
}


void libererListeDouble(listeDouble L)
{
while(L.debut!=NULL)
{
celluleDouble * temp=L.debut;
L.debut=L.debut->suivant;
free(temp);
}
}
26 changes: 26 additions & 0 deletions Headers_Fcts/listHeader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef HEADER
#define HEADER

#include<stdio.h>
#include<stdlib.h>
//#include "liste_simple.h"

struct cellule
{
int element;
struct cellule * suivant;

};
typedef struct cellule cellule;
typedef struct cellule * liste;

#endif

int testVide(liste);
unsigned longueur(liste);
void affichageListe(liste);
void ajoutDebut(int,liste *);
void ajoutFin(int,liste *);
void suppressionDebut(liste *);
void suppressionFin(liste *);
void liberer(liste);
116 changes: 116 additions & 0 deletions Headers_Fcts/listfcts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "listHeader.h"


int testVide(liste L)
{
if(L==NULL)
return 1;
return 0;
}

unsigned longueur(liste L)
{
unsigned l=0;
if(testVide(L)==0)
{
liste temp=L;
while(temp!=NULL)
{
l+=1;
temp=temp->suivant;
}
}
return l;
}

void affichageListe(liste L)
{
if(L==NULL)
printf("[ ]");
else
{
printf("[");
liste temp=L;
while(temp->suivant!=NULL)
{
printf("%d, ",temp->element);
temp=temp->suivant;
}
if(temp!=NULL)
printf("%d",temp->element);
printf("]\n");
}
}

void ajoutDebut(int x,liste * L)
{
cellule * C=(cellule *)malloc(sizeof(cellule));
C->element=x;
C->suivant=*L;
*L=C;
return;
}

void ajoutFin(int x,liste * L)
{
liste C=(liste)malloc(sizeof(cellule));
C->element=x;
C->suivant=NULL;
if(*L==NULL)
*L=C;
else
{
liste temp=*L;
while(temp->suivant!=NULL)
temp=temp->suivant;
temp->suivant=C;
}
return;
}

void suppressionDebut(liste * L)
{
if(L!=NULL)
{
liste temp=*L;
*L=(*L)->suivant;
free(temp);
}
return;
}

void suppressionFin(liste * L)
{
if(L==NULL)
{
return;
}
if((*L)->suivant==NULL)
{
free(*L);
*L=NULL;
}
else
{
liste temp=*L;
liste temp_1=*L;
while(temp->suivant!=NULL)
{
temp_1=temp;
temp=temp->suivant;
}
temp_1->suivant=NULL;
free(temp);
}
return;
}

void liberer(liste L)
{
while(L!=NULL)
{
liste temp=L;
L=L->suivant;
free(temp);
}
}
8 changes: 7 additions & 1 deletion TP1/EX1/iterative_fibo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

int iterative_fibo(int n)
{
// write your iterative_fibo code here
int f1=0,f2=1,s=0;
for(int i=1;i<n;i++){
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this line does?

s=f1+f2;
f1=f2;
f2=s;
}
return s;
}
4 changes: 3 additions & 1 deletion TP1/EX1/recursive_fibo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

int recursive_fibo(int n)
{
// write your recursive fibo code here
if(n<=1)
return n;
return recursive_fibo(n-1)+recursive_fibo(n-2);
}
17 changes: 16 additions & 1 deletion TP1/EX2/get_third_largest.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,20 @@
*/
int get_third_largest(int *tab, int size)
{
// write your algo here
int p=0,q=0,r=0;
for(int i=0;i<size;i++){
if (tab[i]>p){
r=q;
q=p;
p=tab[i];
}
if (tab[i]>q && tab[i]<p){
r=q;
q=tab[i];
}
if (tab[i]>r && tab[i]<q){
r=tab[i];
}
}
return r;
}
1 change: 1 addition & 0 deletions TP1/EX2/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <stdio.h>
#include <stdlib.h> // for rand and srand functions
#include <time.h>

void print_table(int *tab, int size);
void rand_fill_table(int *tab, int size);
Expand Down
7 changes: 6 additions & 1 deletion TP1/EX2/print_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
*/
void print_table(int *tab, int size)
{
// iterate the array tab and print its elements
printf("Here is the array:\n");
for ( int i = 0; i < size; i++)
{
printf("%d ",tab[i]);
}
return ;
}
6 changes: 5 additions & 1 deletion TP1/EX2/rand_fill_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
*/
void rand_fill_table(int *tab, int size)
{
// fill up your table here with randome elements between 10 and 100
srand(time(0));
for(int i=0;i<size;i++){
tab[i]=rand();
}
return ;
}
Loading