diff --git a/Headers_Fcts/dlistHeader.h b/Headers_Fcts/dlistHeader.h new file mode 100644 index 0000000..3a1dd4a --- /dev/null +++ b/Headers_Fcts/dlistHeader.h @@ -0,0 +1,33 @@ +#ifndef HEADER +#define HEADER + +#include +#include +#include + +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); diff --git a/Headers_Fcts/dlistHeader.h.gch b/Headers_Fcts/dlistHeader.h.gch new file mode 100644 index 0000000..76019ab Binary files /dev/null and b/Headers_Fcts/dlistHeader.h.gch differ diff --git a/Headers_Fcts/dlistfcts.c b/Headers_Fcts/dlistfcts.c new file mode 100644 index 0000000..19510fa --- /dev/null +++ b/Headers_Fcts/dlistfcts.c @@ -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); + } +} diff --git a/Headers_Fcts/listHeader.h b/Headers_Fcts/listHeader.h new file mode 100644 index 0000000..2464def --- /dev/null +++ b/Headers_Fcts/listHeader.h @@ -0,0 +1,26 @@ +#ifndef HEADER +#define HEADER + +#include +#include +//#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); diff --git a/Headers_Fcts/listfcts.c b/Headers_Fcts/listfcts.c new file mode 100644 index 0000000..885379e --- /dev/null +++ b/Headers_Fcts/listfcts.c @@ -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); + } +} diff --git a/TP1/EX1/iterative_fibo.c b/TP1/EX1/iterative_fibo.c index 348f646..79ad64c 100644 --- a/TP1/EX1/iterative_fibo.c +++ b/TP1/EX1/iterative_fibo.c @@ -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;ip){ + r=q; + q=p; + p=tab[i]; + } + if (tab[i]>q && tab[i]r && tab[i] #include // for rand and srand functions +#include void print_table(int *tab, int size); void rand_fill_table(int *tab, int size); diff --git a/TP1/EX2/print_table.c b/TP1/EX2/print_table.c index 3700f0a..73390c0 100644 --- a/TP1/EX2/print_table.c +++ b/TP1/EX2/print_table.c @@ -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 ; } diff --git a/TP1/EX2/rand_fill_table.c b/TP1/EX2/rand_fill_table.c index 873f5fe..7687405 100644 --- a/TP1/EX2/rand_fill_table.c +++ b/TP1/EX2/rand_fill_table.c @@ -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;ibase,&T->height); + return ; +} + +void output(t_point T){ + printf("( %d , %d )\n",T.base,T.height); + return ; +} + +char** array(t_point T) +{ + char** arr = malloc(T.base*sizeof(char*)); + for (int i=0; i=D.base || T.height<0 || T.height>=D.height) + return; + if(p[T.base][T.height]==c){ + p[T.base][T.height]=d; + flood_fill(p , (t_point){T.base+1,T.height} , D, c , d); + flood_fill(p , (t_point){T.base-1,T.height} , D, c , d); + flood_fill(p , (t_point){T.base,T.height+1} , D, c , d); + flood_fill(p , (t_point){T.base,T.height-1} , D, c , d); + + } +} + + + + + +void free_array(t_point T,char** arr) +{ + for (int i=0; i +#include + +struct t_point +{ + int base; + int height; +}; +typedef struct t_point t_point; + + +void input(t_point*); +void output(t_point); +char ** array(t_point); +void free_array(t_point,char**); +char** array_input(t_point,char **); +void array_output(t_point,char**); +t_point get_position(); +void flood_fill(char**,t_point,t_point,char,char); + +#endif diff --git a/TP2/Ex1/main.c b/TP2/Ex1/main.c new file mode 100644 index 0000000..b690207 --- /dev/null +++ b/TP2/Ex1/main.c @@ -0,0 +1,23 @@ +#include "header.h" + +int main(){ + t_point T,S; + printf("Please enter the dimensions of the array:\n"); + input(&T); + output(T); + char **p; + p=array(T); + printf("Please input the array values (Note that '0' will be changed to '8' in case the position is correct):\n"); + array_input(T,p); + printf("Here is what it looks like:\n"); + array_output(T,p); + printf("Now please input the initial positon to apply the flood fill algorithm:\n"); + S=get_position(); + flood_fill(p,S,T,'0','8'); + printf("Here is the result:\n"); + array_output(T,p); + printf("\n"); + free_array(T,p); + + return 0; +} diff --git a/TP2/Ex2/Fcts.c b/TP2/Ex2/Fcts.c new file mode 100644 index 0000000..252289e --- /dev/null +++ b/TP2/Ex2/Fcts.c @@ -0,0 +1,112 @@ +#include "header.h" + +t_list create_node(int value){ + t_list c=(t_list)malloc(sizeof(node)); + c->value=value; + c->next=NULL; + return c; +} + +void add_node(t_list L,int value){ + t_list n=create_node(value); + t_list p=L; + if(p==NULL){ + L=n; + } + else{ + while(p->next!=NULL){ + p=p->next; + } + p->next=n; + } + return; +} + +t_list create_list(int n){ + t_list L; + int value; + printf("Please enter the values: "); + scanf("%d",&value); + L=create_node(value); + for(int i=1;inext!=NULL) + { + printf("%d , ",p->value); + p=p->next; + } + printf("%d ]\n",p->value); + } + return ; +} + +void remove_node(t_list * L,int n){ + t_list p=*L,t=*L; + int i=0,j=0; + if(p==NULL){ + printf("There is nothing to remove.\n"); + return ; + } + while (p->next!=NULL) + { + p=p->next; + i++; + } + p=*L; + if(n<=i && n>0){ + while (jnext; + j++; + } + p->next=t->next; + free(t); + return ; + } + else{ + if(n==0){ + *L=(*L)->next; + free(t); + return ; + } + else{ + printf("The adress you have chosen is outside the range of the list and nothing changes.\n"); + return ; + } + } +} + +void free_node(t_list L){ + while(L!=NULL){ + t_list p=L; + L=L->next; + free(p); + } + return ; +} + + diff --git a/TP2/Ex2/Makefile b/TP2/Ex2/Makefile new file mode 100644 index 0000000..dd0a235 --- /dev/null +++ b/TP2/Ex2/Makefile @@ -0,0 +1,9 @@ +SRC = main.c Fcts.c +PRO_NAME = prog + +all: + + gcc $(SRC) -o $(PRO_NAME) + ./prog + rm prog + diff --git a/TP2/Ex2/header.h b/TP2/Ex2/header.h new file mode 100644 index 0000000..3e95fe1 --- /dev/null +++ b/TP2/Ex2/header.h @@ -0,0 +1,23 @@ +#ifndef HEADER +#define HEADER + +#include +#include + +struct node +{ + int value; + struct node* next; +}; +typedef struct node node; +typedef node* t_list; + +t_list create_node(int); //Creating a node +void add_node(t_list,int value); //Adding a node to an existing one +t_list array_to_list(int* ,int); //Creating a list based on the array values +void print_list(t_list); //Printing the list values +void remove_node(t_list*,int); //Removing a node from the overall list +void free_node(t_list); //Freeing the allocated nodes in the overall list +t_list create_list(int n); //(Not required from the exercice) Creating an overall list + +#endif diff --git a/TP2/Ex2/main.c b/TP2/Ex2/main.c new file mode 100644 index 0000000..bbe511a --- /dev/null +++ b/TP2/Ex2/main.c @@ -0,0 +1,26 @@ +#include "header.h" + +int main(){ + t_list c; + t_list* k=&c; + int n,T[n],m; + printf("Enter the desired array length: "); + scanf("%d",&n); + if(n>0){ + printf("Please start typing the array values:\n"); + for(int i=0;ielement!=f.fin->element){ + return false; + } + d.debut=d.debut->suivant; + f.fin=f.fin->precedent; + } + return true; +} + +int main(int ac, char** av){ + listeDouble P = {NULL, NULL, 0}; + P=string_to_list(av); + affichageListeDouble(P); + if (palindrome(P)==true) + printf("True\n"); + else + printf("False\n"); + libererListeDouble(P); + return 0; +} \ No newline at end of file diff --git a/TP3/Ex1/Makefile b/TP3/Ex1/Makefile new file mode 100644 index 0000000..0617ac2 --- /dev/null +++ b/TP3/Ex1/Makefile @@ -0,0 +1,8 @@ +CC = gcc +SRC = *.c +DEPS = *.h + +all : $(DEPS) + $(CC) -o Ex1 $(SRC) +clear : + rm Ex1 \ No newline at end of file diff --git a/TP3/Ex1/double.c b/TP3/Ex1/double.c new file mode 100644 index 0000000..076625e --- /dev/null +++ b/TP3/Ex1/double.c @@ -0,0 +1,119 @@ +#include "double.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("%c, ",temp->element); + temp=temp->suivant; + } + if(temp!=NULL) + printf("%c",temp->element); + printf("]\n"); +} + + + +void ajoutDebut(char 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); + } +} \ No newline at end of file diff --git a/TP3/Ex1/double.h b/TP3/Ex1/double.h new file mode 100644 index 0000000..85e361d --- /dev/null +++ b/TP3/Ex1/double.h @@ -0,0 +1,32 @@ +#ifndef DOUBLE_H +#define DOUBLE_H + +#include +#include +#include +#include +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; + +int listeDoubleVide(listeDouble); +void affichageListeDouble(listeDouble); +void ajoutDebut(char, listeDouble *); +void ajoutFin(int,listeDouble *); +void suppressionDebut(listeDouble *); +void suppressionFin(listeDouble *); +void libererListeDouble(listeDouble); + +#endif \ No newline at end of file diff --git a/TP3/Ex2/Ex2.c b/TP3/Ex2/Ex2.c new file mode 100644 index 0000000..a98ed8f --- /dev/null +++ b/TP3/Ex2/Ex2.c @@ -0,0 +1,85 @@ +#include "double.h" + +void sort_list(listeDouble* P){ + int i,j,k; + listeDouble temp=*P, temp2=*P; + while(temp.debut->suivant!=NULL){ + while(temp2.debut->suivant!=NULL){ + if(temp.debut->element>temp2.debut->suivant->element){ + k=temp.debut->element; + temp.debut->element=temp2.debut->suivant->element; + temp2.debut->suivant->element=k; + } + temp2.debut=temp2.debut->suivant; + } + temp.debut=temp.debut->suivant; + temp2=temp; + } + return; +} + +void merge_lists(listeDouble* P,listeDouble * L){ + listeDouble temp=*P; + temp.fin->suivant=(*L).debut; + return; +} + +void remove_duplicated(listeDouble* L){ + listeDouble temp=*L; + listeDouble temp2=*L; + while(temp.debut->suivant!=NULL){ + if(temp.debut->element==temp.debut->suivant->element){ + if(temp.debut->suivant->suivant==NULL){ + temp2.debut=temp.debut; + temp.debut->suivant=NULL; + temp2.debut=temp2.debut->suivant; + free(temp2.debut); + return; + } + else{ + temp2.debut=temp.debut; + temp2.debut=temp2.debut->suivant; + temp.debut->suivant=temp.debut->suivant->suivant; + temp2.debut->suivant=NULL; + free(temp2.debut); + } + } + else{ + temp.debut=temp.debut->suivant; + } + } +} + +int Randoms(int lower, int upper) +{ + int num = (rand() % (upper - lower + 1)) + lower; + return num; +} + + +int main(){ + listeDouble P={NULL,NULL,0}; + listeDouble L={NULL,NULL,0}; + srand(time(0)); + for (int i=0;i<100;i++){ + ajoutDebut(Randoms(0,40),&P); + } + printf("List P: "); + affichageListeDouble(P); + for (int i=0;i<30;i++){ + ajoutDebut(Randoms(0,40),&L); + } + printf("List L: "); + affichageListeDouble(L); + merge_lists(&P,&L); + printf("List P merged with L: "); + affichageListeDouble(P); + sort_list(&P); + printf("List P: "); + affichageListeDouble(P); + remove_duplicated(&P); + printf("List P without doubles: "); + affichageListeDouble(P); + libererListeDouble(P); + return 0; +} \ No newline at end of file diff --git a/TP3/Ex2/Makefile b/TP3/Ex2/Makefile new file mode 100644 index 0000000..f694038 --- /dev/null +++ b/TP3/Ex2/Makefile @@ -0,0 +1,8 @@ +CC = gcc +SRC = *.c +DEPS = *.h + +all : $(DEPS) + $(CC) -o Ex2 $(SRC) + ./Ex2 + rm Ex2 \ No newline at end of file diff --git a/TP3/Ex2/double.c b/TP3/Ex2/double.c new file mode 100644 index 0000000..3bb5b37 --- /dev/null +++ b/TP3/Ex2/double.c @@ -0,0 +1,118 @@ +#include "double.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(char 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); + } +} \ No newline at end of file diff --git a/TP3/Ex2/double.h b/TP3/Ex2/double.h new file mode 100644 index 0000000..85d1997 --- /dev/null +++ b/TP3/Ex2/double.h @@ -0,0 +1,33 @@ +#ifndef DOUBLE_H +#define DOUBLE_H + +#include +#include +#include +#include +#include +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; + +int listeDoubleVide(listeDouble); +void affichageListeDouble(listeDouble); +void ajoutDebut(char, listeDouble *); +void ajoutFin(int,listeDouble *); +void suppressionDebut(listeDouble *); +void suppressionFin(listeDouble *); +void libererListeDouble(listeDouble); + +#endif \ No newline at end of file diff --git a/TP3/Ex2Arr/Ex2Arr.c b/TP3/Ex2Arr/Ex2Arr.c new file mode 100644 index 0000000..218a08c --- /dev/null +++ b/TP3/Ex2Arr/Ex2Arr.c @@ -0,0 +1,66 @@ +#include "Ex2Arr.h" + +void printArray(int arr[],int n){ + printf("[ "); + for(int i=0;iarr[j+1]){ + s=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=s; + } + } + } + return ; +} + +int NotDuplicates(int arr[], int n){ + int c=0,i=0; + while(i +#include +#include + +void printArray(int arr[],int n); +int Randoms(int lower, int upper); +void sortArray(int arr[], int n); +int NotDuplicates(int arr[], int n); +int* removeDuplicates(int arr[],int n, int c); +int* mergeArray(int arr1[], int n1, int arr2[], int n2); + +#endif \ No newline at end of file diff --git a/TP3/Ex2Arr/Makefile b/TP3/Ex2Arr/Makefile new file mode 100644 index 0000000..f694038 --- /dev/null +++ b/TP3/Ex2Arr/Makefile @@ -0,0 +1,8 @@ +CC = gcc +SRC = *.c +DEPS = *.h + +all : $(DEPS) + $(CC) -o Ex2 $(SRC) + ./Ex2 + rm Ex2 \ No newline at end of file diff --git a/TP3/Ex2Arr/main.c b/TP3/Ex2Arr/main.c new file mode 100644 index 0000000..ce67016 --- /dev/null +++ b/TP3/Ex2Arr/main.c @@ -0,0 +1,28 @@ +#include "Ex2Arr.h" + +int main(){ + int Arr1[100], Arr2[30]; + srand(time(0)); + for(int i=0;i<100;i++){ + Arr1[i]=Randoms(0,10); + } + printf("Arr1= "); + printArray(Arr1,100); + for(int i=0;i<30;i++){ + Arr2[i]=Randoms(0,20); + } + printf("Arr2= "); + printArray(Arr2,30); + int* Arr=mergeArray(Arr1,100,Arr2,30); + printf("Merged arrays : "); + printArray(Arr,130); + sortArray(Arr,130); + printf("Sorted Array :"); + printArray(Arr,130); + int n=NotDuplicates(Arr,130); + int* Arr3=removeDuplicates(Arr,130,n); + printf("Array without duplicates : "); + printArray(Arr3,n); + free(Arr3); + return 0; +} \ No newline at end of file diff --git a/TP3/RPN/Makefile b/TP3/RPN/Makefile new file mode 100644 index 0000000..04de95f --- /dev/null +++ b/TP3/RPN/Makefile @@ -0,0 +1,8 @@ +CC = gcc +SRC = *.c +DEPS = *.h + +all : $(DEPS) + $(CC) -o RPN $(SRC) +clear : + rm RPN \ No newline at end of file diff --git a/TP3/RPN/RPN.c b/TP3/RPN/RPN.c new file mode 100644 index 0000000..aab600d --- /dev/null +++ b/TP3/RPN/RPN.c @@ -0,0 +1,150 @@ +#include "stack.h" + +int add(int x, int y){ + return x+y; +} + +int sub(int x, int y){ + return x-y; +} + +int mul(int x, int y){ + return x*y; +} + +int divd(int x, int y){ + if(y==0){ + printf("Division by zero.\n"); + exit(EXIT_FAILURE); + } + return x/y; +} + +int mod(int x, int y){ + if(y==0){ + printf("Modulo Op by zero.\n"); + exit(EXIT_FAILURE); + } + return x%y; +} + +char** form(char* s){ + char delim[] = " "; + + char str[strlen(s)]; + int j=0; + + for(int i=0;idata=data; + P->next=NULL; + return P; +} + +int is_empty(t_stack* head){ + if(head==NULL){ + return 1; + } + return 0; +} + +void push(t_stack** head, int data){ + t_stack* P=new_stack(data); + P->next=*head; + *head=P; + return; +} + +int pop(t_stack** head){ + if(is_empty(*head)){ + return INT_MIN; + } + int data=(*head)->data; + t_stack* L=*head; + *head=(*head)->next; + free(L); + return data; +} + +int stack_len(t_stack* head){ + t_stack* temp=head; + int n=0; + while(temp!=NULL){ + n++; + temp=temp->next; + } + return n; +} + +int peek_stack(t_stack* head){ + if(is_empty(head)){ + return INT_MIN; + } + return head->data; +} + +void print_stack(t_stack* head){ + t_stack* temp=head; + printf("[ "); + while(temp!=NULL){ + printf("%d ",temp->data); + temp=temp->next; + } + printf("]\n"); + return; +} + +void free_stack(t_stack* head){ + while(head!=NULL){ + t_stack* temp=head; + head=head->next; + free(temp); + } + return; +} \ No newline at end of file diff --git a/TP3/RPN/stack.h b/TP3/RPN/stack.h new file mode 100644 index 0000000..b835ba2 --- /dev/null +++ b/TP3/RPN/stack.h @@ -0,0 +1,29 @@ +#ifndef STACK_H +#define STACK_H + +#include +#include +#include +#include +#include +#include + +typedef struct s_stack +{ + int data; + struct s_stack *next; +} t_stack; + +/* ************************************************************************** */ +/* STACK FUNCTIONS */ +/* ************************************************************************** */ +t_stack *new_stack(int data); +int is_empty(t_stack *head); +void push(t_stack **head, int data); +int pop(t_stack **head); +int stack_len(t_stack *head); +int peek_stack(t_stack *head); +void print_stack(t_stack *head); +void free_stack(t_stack*); + +#endif \ No newline at end of file diff --git a/TP4/.vscode/configurationCache.log b/TP4/.vscode/configurationCache.log new file mode 100644 index 0000000..d7689fc --- /dev/null +++ b/TP4/.vscode/configurationCache.log @@ -0,0 +1 @@ +{"buildTargets":["TP"],"launchTargets":["/home/ayoub/Work/Info/TP4>TP()"],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":["/home/ayoub/Work/Info/TP4"],"compilerArgs":["-c","TP.c","Tree.c","-c","TP.c","Tree.c","-c","-o","TP.o","TP.c","-c","-o","TP.o","TP.c","-o","TP","TP.c","Tree.c"],"compilerPath":"/usr/bin/gcc","standard":"c11","windowsSdkVersion":""},"fileIndex":[["/home/ayoub/Work/Info/TP4/*.c",{"uri":{"$mid":1,"path":"/home/ayoub/Work/Info/TP4/*.c","scheme":"file"},"configuration":{"defines":[],"standard":"c11","includePath":[],"forcedInclude":[],"intelliSenseMode":"gcc-x64","compilerPath":"/usr/bin/gcc","compilerArgs":["-o","TP","TP.c","Tree.c"],"windowsSdkVersion":""}}],["/home/ayoub/Work/Info/TP4/Tree.c",{"uri":{"$mid":1,"external":"file:///home/ayoub/Work/Info/TP4/Tree.c","path":"/home/ayoub/Work/Info/TP4/Tree.c","scheme":"file"},"configuration":{"defines":[],"standard":"c11","includePath":[],"forcedInclude":[],"intelliSenseMode":"gcc-x64","compilerPath":"/usr/bin/gcc","compilerArgs":["-c","-o","Tree.o","Tree.c"],"windowsSdkVersion":""}}],["/home/ayoub/Work/Info/TP4/TP.c",{"uri":{"$mid":1,"path":"/home/ayoub/Work/Info/TP4/TP.c","scheme":"file"},"configuration":{"defines":[],"standard":"c11","includePath":[],"forcedInclude":[],"intelliSenseMode":"gcc-x64","compilerPath":"/usr/bin/gcc","compilerArgs":["-c","-o","TP.o","TP.c"],"windowsSdkVersion":""}}]]}} \ No newline at end of file diff --git a/TP4/.vscode/dryrun.log b/TP4/.vscode/dryrun.log new file mode 100644 index 0000000..f95b262 --- /dev/null +++ b/TP4/.vscode/dryrun.log @@ -0,0 +1,8 @@ +make --dry-run --always-make --keep-going --print-directory +make: Entering directory '/home/ayoub/Work/Info/TP4' +gcc -c *.c +gcc -o TP *.c +./TP +rm TP *.o +make: Leaving directory '/home/ayoub/Work/Info/TP4' + diff --git a/TP4/.vscode/settings.json b/TP4/.vscode/settings.json new file mode 100644 index 0000000..65e1ec0 --- /dev/null +++ b/TP4/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "makefile.extensionOutputFolder": "./.vscode" +} \ No newline at end of file diff --git a/TP4/.vscode/targets.log b/TP4/.vscode/targets.log new file mode 100644 index 0000000..9f8fa6e --- /dev/null +++ b/TP4/.vscode/targets.log @@ -0,0 +1,376 @@ +make all --print-data-base --no-builtin-variables --no-builtin-rules --question +make: *** No rule to make target 'all'. Stop. + +# GNU Make 4.2.1 +# Built for x86_64-pc-linux-gnu +# Copyright (C) 1988-2016 Free Software Foundation, Inc. +# License GPLv3+: GNU GPL version 3 or later +# This is free software: you are free to change and redistribute it. +# There is NO WARRANTY, to the extent permitted by law. + +# Make data base, printed on Fri Jun 4 14:19:01 2021 + +# Variables + +# 'override' directive +GNUMAKEFLAGS := +# automatic +left; + } + else + { + temp=pop(&P); + printf("%d ",temp->data); + temp=temp->right; + } + } + printf("]\n"); + return; +} + +//Ex1 + +int max(int x, int y){ + if(x>y) + return x; + return y; +} + +int max_nodes(Tree* T){ + if(testNull(T)){ + return 0; + } + int lmax = max_nodes(left(T)); + int rmax = max_nodes(right(T)); + return max(lmax , rmax) + 1 ; +} + +int tree_depth(Tree* T){ + return max_nodes(T) - 1 ; +} + +int tree_diameter(Tree* T){ + if(testNull(T)) + return 0; + int ltree = tree_diameter(left(T)); + int rtree = tree_diameter(right(T)); + return max(max_nodes(left(T)) + max_nodes(right(T)) + 1, max(ltree,rtree)); +} + +int leaf_sum(Tree* T){ + if(T==NULL) + return 0; + Tree_stack P={NULL,0}; + Tree* temp=T; + unsigned s = 0; + while(testNullTS(P)==0 || temp!=NULL) + { + if(temp!=NULL) + { + push(temp,&P); + temp=temp->left; + } + else + { + temp=pop(&P); + if(testNull(left(temp)) && testNull(right(temp))) + s+=temp->data; + temp=temp->right; + } + } + return s; +} + +int leaf_sum_2(Tree* T){ + if(testNull(T)) + return 0; + if(testNull(left(T)) && testNull(right(T))) + return T->data; + return leaf_sum_2(left(T)) + leaf_sum_2(right(T)); +} + +//Ex2 + +void insert_node(int x,Tree* T){ + if(testNull(T)){ + buildTree(x,NULL,NULL); + return; + } + Tree* temp = T; + while(!(testNull(left(temp))) || !(testNull(right(temp)))){ + if(xdata){ + if(!testNull(left(temp))) + temp = temp->left; + else + break; + } + else{ + if(x>temp->data){ + if(!testNull(right(temp))) + temp = temp->right; + else + break; + } + else + return; + } + } + Tree* P=buildTree(x,NULL,NULL); + if(xdata){ + fixLeft(P,&temp); + return; + } + else{ + if(x>temp->data){ + fixRight(P,&temp); + return; + } + else{ + return; + } + } +} + +void search_Tree_IN(int x,Tree* T){ + if(testNull(T)){ + printf("The value %d doesn't exist\n",T->data); + return; + } + if(T->data == x){ + printf("The value %d exists\n",T->data); + return; + } + if(T->data < x){ + return search_Tree_IN(x,right(T)); + } + if(T->data > x){ + return search_Tree_IN(x,left(T)); + } +} + +int max_depth(Tree* T){ + return tree_depth(T); +} + +int tree_size(Tree* T){ + if(testNull(T)) + return 0; + return 1 + tree_size(left(T)) + tree_size(right(T)); +} + +int main(){ + Tree* T1=NULL; + Tree* T2=NULL; + T1=buildTree(3,NULL,NULL); + T2=buildTree(7,NULL,NULL); + Tree* T=buildTree(5,T1,T2); + Tree* T3=NULL; + Tree* T4=NULL; + Tree* T5=buildTree(11,NULL,NULL); + Tree* T6=buildTree(15,NULL,NULL); + Tree* T7=buildTree(13,T5,T6); + T3=buildTree(0,NULL,T); + T4=buildTree(10,T3,T7); + printf("\n\n"); + print_tree(T4); + //Ex1 + printf("The depth of the tree is %d\n",tree_depth(T4)); + printf("The diameter of the tree is %d\n",tree_diameter(T4)); + printf("The sum of the leaves in the tree is %d\n",leaf_sum_2(T4)); + //Ex2 + insert_node(4,T4); + print_tree(T4); + search_Tree_IN(15,T4); + printf("The tree size is %d\n",tree_size(T4)); + return 0; +} \ No newline at end of file diff --git a/TP4/Tree.c b/TP4/Tree.c new file mode 100644 index 0000000..9271bc6 --- /dev/null +++ b/TP4/Tree.c @@ -0,0 +1,114 @@ +#include "Tree.h" + +Tree* treeNull() +{ + Tree* T=NULL; + return T; +} + +bool testNull(Tree* T) +{ + if(T==NULL) + return true; + return false; +} + + int root(Tree* T) +{ + if(testNull(T)) + exit(EXIT_FAILURE); + return T->data; +} + +Tree* left(Tree* T) +{ + if(testNull(T)) + return NULL; + return T->left; +} + +Tree* right(Tree* T) +{ + if(testNull(T)) + return NULL; + return T->right; +} + +Tree* buildTree(int x,Tree* T_1,Tree* T_2) +{ + Tree* T=(Tree* )malloc(sizeof(Tree)); + T->data=x; + T->left=T_1; + T->right=T_2; + return T; +} + +void fixData(int x,Tree ** P) +{ + if(testNull(*P)) + exit(EXIT_FAILURE); + else + { + (*P)->data=x; + } +} + +void fixLeft(Tree * G, Tree ** P) +{ + if(testNull(*P)) + exit(EXIT_FAILURE); + else + { + (*P)->left=G; + } +} + +void fixRight(Tree * D, Tree ** P) +{ + if(testNull(*P)) + exit(EXIT_FAILURE); + else + { + (*P)->right=D; + } +} + +void freeTree(Tree ** T) +{ + Tree* temp=*T; + if(temp==NULL) + return; + if(temp->left!=NULL) + freeTree(&temp->left); + if(temp->right!=NULL) + freeTree(&temp->right); + free(temp); + *T=NULL; +} + +bool testNullTS(Tree_stack P) +{ + if (P.length ==0) + return true; + return false; +} + +void push(Tree* T,Tree_stack * P) +{ + if(P->length==N) + { + printf("Stack is empty.\n"); + return; + } + P->head[P->length]=T; + P->length+=1; +} + +Tree* pop(Tree_stack * P) +{ + if(testNullTS(*P)) + return NULL; + Tree* T=P->head[P->length-1]; + P->length-=1; + return T; +} \ No newline at end of file diff --git a/TP4/Tree.h b/TP4/Tree.h new file mode 100644 index 0000000..31c29eb --- /dev/null +++ b/TP4/Tree.h @@ -0,0 +1,40 @@ +#ifndef TREE_H +#define TREE_H + +#include +#include +#include + +typedef struct TreeNode { + int data; + struct TreeNode * left; + struct TreeNode * right; +} Tree ; + +#define N 200 + +typedef struct Tree_stack +{ + Tree* head[N]; + unsigned length; +} Tree_stack; + + +//Tree functions +Tree* treeNull(); +bool testNull(Tree*); /*Test if a tree is NULL*/ +int root(Tree*); /*Extract the root of the tree*/ +Tree* left(Tree*);/*Extract the left sub-tree*/ +Tree* right(Tree*); /*Extract the right sub-tree*/ +Tree* buildTree(int, Tree*,Tree*); /* Build a tree with a root data and two trees left and right*/ +void fixData(int, Tree **); /*Modify the root of the tree*/ +void fixLeft(Tree*, Tree **); /*Modify the left sub-tree*/ +void fixRight(Tree*, Tree **); /*Modify the right sub-tree*/ +void freeTree(Tree **); /*free the allocated memory for the tree*/ + +//Tree_stack functions +bool testNullTS(Tree_stack P); +void push(Tree* T,Tree_stack* P); +Tree* pop(Tree_stack* P); + +#endif \ No newline at end of file