init projet
This commit is contained in:
1
tp1/Makefile
Normal file
1
tp1/Makefile
Normal file
@@ -0,0 +1 @@
|
||||
tp1 : tp1.c tp1.h
|
||||
BIN
tp1/Sources-TD1/._.ccls
Normal file
BIN
tp1/Sources-TD1/._.ccls
Normal file
Binary file not shown.
BIN
tp1/Sources-TD1/._.dir-locals.el
Normal file
BIN
tp1/Sources-TD1/._.dir-locals.el
Normal file
Binary file not shown.
BIN
tp1/Sources-TD1/._Exercice3.c
Normal file
BIN
tp1/Sources-TD1/._Exercice3.c
Normal file
Binary file not shown.
BIN
tp1/Sources-TD1/._Exercice6.c
Normal file
BIN
tp1/Sources-TD1/._Exercice6.c
Normal file
Binary file not shown.
BIN
tp1/Sources-TD1/._Exercice8.4.c
Normal file
BIN
tp1/Sources-TD1/._Exercice8.4.c
Normal file
Binary file not shown.
12
tp1/Sources-TD1/.ccls
Normal file
12
tp1/Sources-TD1/.ccls
Normal file
@@ -0,0 +1,12 @@
|
||||
gcc-13
|
||||
%c -std=std2x
|
||||
%c -Wall
|
||||
%c -Wextra
|
||||
%c -pedantic
|
||||
%c -Wshadow
|
||||
%c -Wpointer-arith
|
||||
%c -Wcast-qual
|
||||
%c -Wstrict-prototypes
|
||||
%c -Wmissing-prototypes
|
||||
%c -Wno-gnu-zero-variadic-macro-arguments
|
||||
%c -I/opt/homebrew/include/
|
||||
2
tp1/Sources-TD1/.dir-locals.el
Normal file
2
tp1/Sources-TD1/.dir-locals.el
Normal file
@@ -0,0 +1,2 @@
|
||||
((c-mode . ((+format-with . uncrustify))))
|
||||
;; ((c-mode . ((+format-with . clang-format))))
|
||||
3643
tp1/Sources-TD1/.uncrustify
Normal file
3643
tp1/Sources-TD1/.uncrustify
Normal file
File diff suppressed because it is too large
Load Diff
55
tp1/Sources-TD1/Exercice3.c
Normal file
55
tp1/Sources-TD1/Exercice3.c
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void echange_1(int x, int y) {
|
||||
int tmp;
|
||||
|
||||
tmp = x;
|
||||
x = y;
|
||||
y = tmp;
|
||||
}
|
||||
|
||||
void echange_2(int *x, int *y) {
|
||||
int *tmp;
|
||||
|
||||
tmp = x;
|
||||
x = y;
|
||||
y = tmp;
|
||||
}
|
||||
|
||||
void echange_3(int *x, int *y) {
|
||||
int tmp;
|
||||
|
||||
tmp = *x;
|
||||
*x = *y;
|
||||
*y = tmp;
|
||||
}
|
||||
|
||||
void echange_4(int *x, int *y) {
|
||||
int *tmp;
|
||||
|
||||
*tmp = *x;
|
||||
*x = *y;
|
||||
*y = *tmp;
|
||||
}
|
||||
|
||||
// Expliquer de façon détaillée et justifiée ce qu'on obtient à l'exécution.
|
||||
|
||||
int main(void) {
|
||||
int a = 1, b = 2;
|
||||
echange_1(a, b);
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 1, b = 2;
|
||||
echange_2(&a, &b);
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 1, b = 2;
|
||||
echange_3(&a, &b);
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
a = 1, b = 2;
|
||||
echange_4(&a, &b);
|
||||
printf("a = %d, b = %d\n", a, b);
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
tp1/Sources-TD1/Exercice4.c
Normal file
15
tp1/Sources-TD1/Exercice4.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int a = 1, b = 2, c = 3, d = 4;
|
||||
int *p1 = &a, *p2 = &b, *p3 = &c, *p4 = &d, *p5;
|
||||
|
||||
p5 = p3;
|
||||
*p3 = *p2;
|
||||
*p2 = *p5;
|
||||
*p4 = *p1;
|
||||
*p1 = *p4;
|
||||
printf("a, b ,c and d are equal to: %d, %d, %d and %d\n", a, b, c, d);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
33
tp1/Sources-TD1/Exercice5.c
Normal file
33
tp1/Sources-TD1/Exercice5.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int *x1[1], *x2[1], y[1], z[1];
|
||||
|
||||
void f(int **p) {
|
||||
**p = 2;
|
||||
*p = y;
|
||||
**p = 3;
|
||||
p = x2;
|
||||
*p = z;
|
||||
**p = 4;
|
||||
// Fin de f
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int **p, ***q, **r, *s, t;
|
||||
|
||||
p = x1;
|
||||
*p = &t;
|
||||
**p = 1;
|
||||
q = &p;
|
||||
r = p;
|
||||
s = *p;
|
||||
|
||||
printf("%d %d %d %d %d %d \n", ***q, **p, **r, *s, *z, t);
|
||||
|
||||
f(p);
|
||||
|
||||
printf("%d %d %d %d %d %d \n", ***q, **p, **r, *s, *z, t);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
20
tp1/Sources-TD1/Exercice6.c
Normal file
20
tp1/Sources-TD1/Exercice6.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int a = 1, b = 2, c = 3, d = 4;
|
||||
int *p1 = &a, **p2 = &p1, *p3 = &c, *p4 = &b, **p5 = &p3, *p6 = &d;
|
||||
|
||||
*p3 = **p2 + 6;
|
||||
*p2 = p3;
|
||||
*p1 = *p4 * 5;
|
||||
p5 = &p6;
|
||||
**p5 = **p5 + **p2;
|
||||
*p4 = *p3 + 14;
|
||||
*p5 = p1;
|
||||
**p2 = 1 + **p2;
|
||||
**p5 = **p5 * **p2;
|
||||
|
||||
printf("a, b ,c and d are equal to: %d, %d, %d and %d\n", a, b, c, d);
|
||||
return 1;
|
||||
}
|
||||
39
tp1/Sources-TD1/Exercice7.c
Normal file
39
tp1/Sources-TD1/Exercice7.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int x1, y1, y2, z1, z2;
|
||||
|
||||
int *px1 = &x1,
|
||||
*py1 = &y1, *py2 = &y2,
|
||||
*pz1 = &z1, *pz2 = &z2;
|
||||
|
||||
void aux(int **p, int ***q)
|
||||
{
|
||||
**p = 2;
|
||||
***q = 99;
|
||||
*p = pz1;
|
||||
**p = 12;
|
||||
p = &py2;
|
||||
*q = &px1;
|
||||
**p = 4;
|
||||
***q = 42;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int **p, **q, ***r, s, *t;
|
||||
p = &py1;
|
||||
q = &px1;
|
||||
r = &q;
|
||||
*p = &s;
|
||||
**q = 1;
|
||||
**p = 34;
|
||||
***r = 76;
|
||||
t = **r;
|
||||
r = &p;
|
||||
|
||||
aux(p, &q);
|
||||
printf("%d %d %d %d %d \n",
|
||||
**q, **p, ***r, s, *t);
|
||||
return 1;
|
||||
}
|
||||
39
tp1/Sources-TD1/Exercice8.1.c
Normal file
39
tp1/Sources-TD1/Exercice8.1.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void foo(int **);
|
||||
|
||||
int main(void);
|
||||
|
||||
int somme(void);
|
||||
|
||||
int resultat = 0;
|
||||
|
||||
void foo(int **p) {
|
||||
resultat++;
|
||||
*p = &resultat;
|
||||
main();
|
||||
return;
|
||||
}
|
||||
|
||||
int m;
|
||||
int *p[5] = {&m, &m, &m, &m, &m};
|
||||
|
||||
int somme(void) {
|
||||
int i;
|
||||
int s = 0;
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
s += *p[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static int n;
|
||||
|
||||
if (n == 5)
|
||||
printf("%d\n", somme());
|
||||
else
|
||||
foo(p + n++);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
39
tp1/Sources-TD1/Exercice8.2.c
Normal file
39
tp1/Sources-TD1/Exercice8.2.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void foo(int *p);
|
||||
|
||||
int somme(void);
|
||||
|
||||
int main(void);
|
||||
|
||||
int resultat = 0;
|
||||
|
||||
void foo(int *p) {
|
||||
resultat++;
|
||||
p = &resultat;
|
||||
main();
|
||||
return;
|
||||
}
|
||||
|
||||
int m;
|
||||
int *p[5] = {&m, &m, &m, &m, &m};
|
||||
|
||||
int somme(void) {
|
||||
int i;
|
||||
int s = 0;
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
s += *p[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static int n;
|
||||
|
||||
if (n == 5)
|
||||
printf("%d\n", somme());
|
||||
else
|
||||
foo(p[n++]);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
39
tp1/Sources-TD1/Exercice8.3.c
Normal file
39
tp1/Sources-TD1/Exercice8.3.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void foo(int **p);
|
||||
|
||||
int somme(void);
|
||||
|
||||
int main(void);
|
||||
|
||||
int resultat = 0;
|
||||
|
||||
void foo(int **p) {
|
||||
int resultbis = ++resultat;
|
||||
*p = &resultbis;
|
||||
main();
|
||||
return;
|
||||
}
|
||||
|
||||
int m;
|
||||
int *p[5] = {&m, &m, &m, &m, &m};
|
||||
|
||||
int somme(void) {
|
||||
int i;
|
||||
int s = 0;
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
s += *p[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static int n;
|
||||
|
||||
if (n == 5)
|
||||
printf("%d\n", somme());
|
||||
else
|
||||
foo(p + n++);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
45
tp1/Sources-TD1/Exercice8.4.c
Normal file
45
tp1/Sources-TD1/Exercice8.4.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void foo(int **p);
|
||||
|
||||
int somme(void);
|
||||
|
||||
int main(void);
|
||||
|
||||
int resultat = 0;
|
||||
|
||||
void foo(int **p) {
|
||||
int resultbis = ++resultat;
|
||||
*p = &resultbis;
|
||||
main();
|
||||
return;
|
||||
}
|
||||
|
||||
int m;
|
||||
int *p[5] = {&m, &m, &m, &m, &m};
|
||||
|
||||
int somme(void) {
|
||||
int i;
|
||||
int s = 0;
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
printf("%d\n", *p[i]);
|
||||
s += *p[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static int n;
|
||||
int m = n;
|
||||
|
||||
if (n != 5) {
|
||||
foo(p + (m = n++));
|
||||
if (m == 0)
|
||||
printf("%d\n", somme());
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
440
tp1/tp1.c
Normal file
440
tp1/tp1.c
Normal file
@@ -0,0 +1,440 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "tp1.h"
|
||||
|
||||
// Fonctions de tp1.h à implémenter ici
|
||||
//
|
||||
//
|
||||
|
||||
char *double_upper(char *s) {
|
||||
int n = 0;
|
||||
for(int i = 0; s[i] != '\0'; i++) {
|
||||
n++;
|
||||
if(isupper(s[i])) {
|
||||
n++;
|
||||
}
|
||||
}
|
||||
char ns[n];
|
||||
for(int i = 0, j = 0; i < n; i++, j++) {
|
||||
ns[j] = s[i];
|
||||
if(isupper(s[i])) {
|
||||
j++;
|
||||
ns[j] = s[i];
|
||||
}
|
||||
}
|
||||
s = ns;
|
||||
return s;
|
||||
}
|
||||
|
||||
dlist *create_empty_dlist() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
dlist *add_left_to_dlist(dlist *list, int value) {
|
||||
dlist *new_list = create_empty_dlist();
|
||||
new_list = malloc(sizeof(dlist));
|
||||
new_list -> value = value;
|
||||
if(list == NULL) {
|
||||
list -> prev = NULL;
|
||||
list -> next = NULL;
|
||||
}
|
||||
if(list -> prev == NULL) {
|
||||
new_list -> prev = NULL;
|
||||
} else {
|
||||
new_list -> prev = list -> prev;
|
||||
list -> prev -> next = new_list;
|
||||
}
|
||||
new_list -> next = list;
|
||||
list -> prev = new_list;
|
||||
return new_list;
|
||||
}
|
||||
|
||||
|
||||
dlist *add_right_to_dlist(dlist *list, int value) {
|
||||
dlist *new_list = create_empty_dlist();
|
||||
new_list = malloc(sizeof(dlist));
|
||||
new_list -> value = value;
|
||||
if(list == NULL) {
|
||||
list -> prev = NULL;
|
||||
list -> next = NULL;
|
||||
}
|
||||
if(list -> next == NULL) {
|
||||
new_list -> next = NULL;
|
||||
} else {
|
||||
new_list -> next = list -> next;
|
||||
list -> next -> prev = new_list;
|
||||
}
|
||||
new_list -> next = list -> next;
|
||||
new_list -> prev = list;
|
||||
list -> next = new_list;
|
||||
return new_list;
|
||||
}
|
||||
|
||||
void del_cell_dlist(dlist *list) {
|
||||
if(list != NULL) {
|
||||
if(list -> prev != NULL) {
|
||||
list -> prev -> next = list -> next;
|
||||
}
|
||||
if(list -> next != NULL) {
|
||||
list -> next -> prev = list -> prev;
|
||||
}
|
||||
free(list);
|
||||
}
|
||||
}
|
||||
|
||||
void free_dlist(dlist *list) {
|
||||
if(list == NULL) return;
|
||||
while(list -> prev != NULL) {
|
||||
del_cell_dlist(list -> prev);
|
||||
}
|
||||
while(list -> next != NULL) {
|
||||
del_cell_dlist(list -> next);
|
||||
}
|
||||
del_cell_dlist(list);
|
||||
}
|
||||
|
||||
dlist *iota_dlist(int n) {
|
||||
if(n == 0) return NULL;
|
||||
dlist *list = create_empty_dlist();
|
||||
list = malloc(sizeof(dlist));
|
||||
list -> value = 0;
|
||||
list -> prev = NULL;
|
||||
list -> next = NULL;
|
||||
for(int i = 1; i < n; i++) {
|
||||
add_right_to_dlist(list, i);
|
||||
list = list -> next;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
void print_dlist(dlist *list) {
|
||||
if(list == NULL) {
|
||||
printf("_\n");
|
||||
return;
|
||||
}
|
||||
while(list -> prev != NULL) {
|
||||
list = list -> prev;
|
||||
}
|
||||
while(list -> next != NULL) {
|
||||
printf("%i ", list -> value);
|
||||
list = list -> next;
|
||||
}
|
||||
printf("%i\n", list -> value);
|
||||
}
|
||||
|
||||
dlist *create_random_dlist(int length, int max) {
|
||||
dlist *list = create_empty_dlist();
|
||||
list = malloc(sizeof(dlist));
|
||||
list -> value = random()%max;
|
||||
list -> prev = NULL;
|
||||
list -> next = NULL;
|
||||
for(int i = 1; i < length; i++) {
|
||||
add_right_to_dlist(list, random()%max);
|
||||
list = list -> next;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
dlist *copy_dlist(dlist *list) {
|
||||
if(list == NULL) return NULL;
|
||||
while(list -> prev != NULL) {
|
||||
list = list -> prev;
|
||||
}
|
||||
dlist *new_list = create_empty_dlist();
|
||||
new_list = malloc(sizeof(dlist));
|
||||
new_list -> prev = NULL;
|
||||
new_list -> value = list -> value;
|
||||
new_list -> next = NULL;
|
||||
while(list -> next != NULL) {
|
||||
add_right_to_dlist(new_list, list -> next -> value);
|
||||
new_list = new_list -> next;
|
||||
list = list -> next;
|
||||
}
|
||||
return new_list;
|
||||
}
|
||||
|
||||
dlist *reverse_dlist(dlist *list) {
|
||||
if(list == NULL) return NULL;
|
||||
while(list -> prev != NULL) {
|
||||
list = list -> prev;
|
||||
}
|
||||
dlist *new_list = create_empty_dlist();
|
||||
new_list = malloc(sizeof(dlist));
|
||||
new_list -> prev = NULL;
|
||||
new_list -> value = list -> value;
|
||||
new_list -> next = NULL;
|
||||
while(list -> next != NULL) {
|
||||
add_left_to_dlist(new_list, list -> next -> value);
|
||||
new_list = new_list -> prev;
|
||||
list = list -> next;
|
||||
}
|
||||
return new_list;
|
||||
}
|
||||
|
||||
dlist *mix_dlist(dlist *list1, dlist *list2) {
|
||||
if(list1 == NULL && list2 == NULL) return NULL;
|
||||
if(list1 == NULL) {
|
||||
return copy_dlist(list2);
|
||||
}
|
||||
else if(list2 == NULL) {
|
||||
return copy_dlist(list1);
|
||||
}
|
||||
while(list1 -> prev != NULL) {
|
||||
list1 = list1 -> prev;
|
||||
}
|
||||
while(list2 -> prev != NULL) {
|
||||
list2 = list2 -> prev;
|
||||
}
|
||||
dlist *new_list = create_empty_dlist();
|
||||
new_list = malloc(sizeof(dlist));
|
||||
new_list -> prev = NULL;
|
||||
new_list -> value = list1 -> value;
|
||||
new_list -> next = NULL;
|
||||
add_right_to_dlist(new_list, list2 -> value);
|
||||
new_list = new_list -> next;
|
||||
while(list1 -> next != NULL || list2 -> next != NULL) {
|
||||
if(list1 -> next != NULL) {
|
||||
add_right_to_dlist(new_list, list1 -> next -> value);
|
||||
new_list = new_list -> next;
|
||||
list1 = list1 -> next;
|
||||
}
|
||||
if(list2 -> next != NULL) {
|
||||
add_right_to_dlist(new_list, list2 -> next -> value);
|
||||
new_list = new_list -> next;
|
||||
list2 = list2 -> next;
|
||||
}
|
||||
}
|
||||
return new_list;
|
||||
}
|
||||
|
||||
void evenodd_dlist(dlist *list) {
|
||||
if(list == NULL) return;
|
||||
while(list -> next != NULL) {
|
||||
list = list -> next;
|
||||
}
|
||||
dlist *new_list_even = create_empty_dlist();
|
||||
new_list_even = malloc(sizeof(dlist));
|
||||
new_list_even -> prev = NULL;
|
||||
new_list_even -> value = 1;
|
||||
new_list_even -> next = NULL;
|
||||
dlist *new_list_odd = create_empty_dlist();
|
||||
new_list_odd = malloc(sizeof(dlist));
|
||||
new_list_odd -> prev = NULL;
|
||||
new_list_odd -> value = 0;
|
||||
new_list_odd -> next = NULL;
|
||||
while(list -> prev != NULL) {
|
||||
if(list -> value % 2 == 0) {
|
||||
add_left_to_dlist(new_list_even, list -> value);
|
||||
new_list_even = new_list_even -> prev;
|
||||
}
|
||||
else {
|
||||
add_left_to_dlist(new_list_odd, list -> value);
|
||||
new_list_odd = new_list_odd -> prev;
|
||||
}
|
||||
list = list -> prev;
|
||||
}
|
||||
if(list -> value % 2 == 0) {
|
||||
add_left_to_dlist(new_list_even, list -> value);
|
||||
new_list_even = new_list_even -> prev;
|
||||
}
|
||||
else {
|
||||
add_left_to_dlist(new_list_odd, list -> value);
|
||||
new_list_odd = new_list_odd -> prev;
|
||||
}
|
||||
|
||||
while(new_list_even -> value != 1) {
|
||||
list -> value = new_list_even -> value;
|
||||
new_list_even = new_list_even -> next;
|
||||
list = list -> next;
|
||||
}
|
||||
|
||||
while(new_list_odd -> value != 0) {
|
||||
list -> value = new_list_odd -> value;
|
||||
new_list_odd = new_list_odd -> next;
|
||||
list = list -> next;
|
||||
}
|
||||
}
|
||||
|
||||
void bubblesort_dlist(dlist *list) {
|
||||
if(list == NULL) return;
|
||||
while(list -> prev != NULL) {
|
||||
list = list -> prev;
|
||||
}
|
||||
bool check = true;
|
||||
while(check) {
|
||||
check = false;
|
||||
dlist *list_temp = list;
|
||||
while (list_temp -> next != NULL) {
|
||||
if(list_temp -> next -> value < list_temp -> value) {
|
||||
check = true;
|
||||
int value_temp = list_temp -> value;
|
||||
list_temp -> value = list_temp -> next -> value;
|
||||
list_temp -> next -> value = value_temp;
|
||||
}
|
||||
list_temp = list_temp -> next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dlist *split_dlist(dlist *list) {
|
||||
if(list == NULL) return NULL;
|
||||
if(list -> prev == NULL && list -> next == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
dlist *new_list = list;
|
||||
while(list -> next != NULL) {
|
||||
list = list -> next;
|
||||
}
|
||||
while(new_list -> prev != NULL) {
|
||||
new_list = new_list -> prev;
|
||||
}
|
||||
while (list != new_list && list -> prev != new_list) {
|
||||
list = list -> prev;
|
||||
new_list = new_list -> next;
|
||||
}
|
||||
if(list == new_list) {
|
||||
list = list -> next;
|
||||
}
|
||||
list -> prev = NULL;
|
||||
new_list -> next = NULL;
|
||||
return new_list;
|
||||
}
|
||||
|
||||
void merge_dlist(dlist *list1, dlist *list2) {
|
||||
if(list1 == NULL || list2 == NULL) {
|
||||
return;
|
||||
}
|
||||
while(list1 -> prev != NULL) {
|
||||
list1 = list1 -> prev;
|
||||
}
|
||||
while(list2 -> prev != NULL) {
|
||||
list2 = list2 -> prev;
|
||||
}
|
||||
printf(" 2. ");
|
||||
print_dlist(list1);
|
||||
printf(" 2. ");
|
||||
print_dlist(list2);
|
||||
while(list1 -> next != NULL && list2 != NULL) {
|
||||
while(list2 != NULL && list2 -> value <= list1 -> value) {
|
||||
add_left_to_dlist(list1, list2 -> value);
|
||||
if(list2 -> next != NULL) {
|
||||
list2 = list2 -> next;
|
||||
del_cell_dlist(list2 -> prev);
|
||||
}
|
||||
else {
|
||||
list2 = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
list1 = list1 -> next;
|
||||
}
|
||||
if(list1 -> next == NULL) {
|
||||
while(list2 != NULL && list2 -> value <= list1 -> value) {
|
||||
printf(" 4. %d - %d\n", list1 -> value, list2 -> value);
|
||||
add_left_to_dlist(list1, list2 -> value);
|
||||
if(list2 -> next != NULL) {
|
||||
list2 = list2 -> next;
|
||||
del_cell_dlist(list2 -> prev);
|
||||
}
|
||||
else {
|
||||
list2 = NULL;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// printf("dddddddddddddd\n");
|
||||
}
|
||||
if(list2 != NULL && list1 -> next == NULL) {
|
||||
dlist *list_temp = copy_dlist(list2);
|
||||
list1 -> next = list_temp;
|
||||
list_temp -> prev = list1;
|
||||
list2 = NULL;
|
||||
printf(" 5. ");
|
||||
print_dlist(list1);
|
||||
}
|
||||
}
|
||||
|
||||
void mergesort_dlist(dlist *list) {
|
||||
if(list == NULL) return;
|
||||
if(list -> prev == NULL && list -> next == NULL) return;
|
||||
dlist *new_list1 = copy_dlist(list);
|
||||
dlist *new_list2 = split_dlist(new_list1);
|
||||
printf("1. ");
|
||||
print_dlist(new_list1);
|
||||
printf("1. ");
|
||||
print_dlist(new_list2);
|
||||
mergesort_dlist(new_list1);
|
||||
mergesort_dlist(new_list2);
|
||||
printf(" 3. ");
|
||||
print_dlist(new_list1);
|
||||
printf(" 3. ");
|
||||
print_dlist(new_list2);
|
||||
merge_dlist(new_list1, new_list2);
|
||||
printf(" 6. ");
|
||||
print_dlist(new_list1);
|
||||
printf(" 6. ");
|
||||
print_dlist(list);
|
||||
while(list -> prev != NULL) {
|
||||
list = list -> prev;
|
||||
}
|
||||
while(new_list1 -> prev != NULL) {
|
||||
new_list1 = new_list1 -> prev;
|
||||
}
|
||||
while(list -> next != NULL && new_list1 -> next != NULL) {
|
||||
list -> value = new_list1 -> value;
|
||||
list = list -> next;
|
||||
new_list1 = new_list1 -> next;
|
||||
}
|
||||
list -> value = new_list1 -> value;
|
||||
list = list -> next;
|
||||
printf(" 6. ");
|
||||
print_dlist(list);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
srandom(time(NULL));
|
||||
// char *s = "Vive le C!!\n";
|
||||
// printf("%s", s);
|
||||
// s = double_upper(s);
|
||||
// printf("%s", s);
|
||||
// Tests à écrire ici
|
||||
/* ... */
|
||||
dlist *list = iota_dlist(10);
|
||||
print_dlist(list);
|
||||
free_dlist(list);
|
||||
list = create_random_dlist(10, 100);
|
||||
print_dlist(list);
|
||||
dlist *new_list = copy_dlist(list);
|
||||
print_dlist(new_list);
|
||||
new_list = reverse_dlist(list);
|
||||
print_dlist(new_list);
|
||||
dlist *new_new_list = mix_dlist(list, new_list);
|
||||
print_dlist(new_new_list);
|
||||
evenodd_dlist(new_new_list);
|
||||
print_dlist(new_new_list);
|
||||
bubblesort_dlist(new_new_list);
|
||||
print_dlist(new_new_list);
|
||||
new_new_list = iota_dlist(10);
|
||||
print_dlist(new_new_list);
|
||||
evenodd_dlist(new_new_list);
|
||||
list = split_dlist(new_new_list);
|
||||
print_dlist(new_new_list);
|
||||
print_dlist(list);
|
||||
merge_dlist(new_new_list, list);
|
||||
print_dlist(new_new_list);
|
||||
list = iota_dlist(10);
|
||||
list = reverse_dlist(list);
|
||||
printf("sort\n");
|
||||
print_dlist(list);
|
||||
mergesort_dlist(list);
|
||||
print_dlist(list);
|
||||
free_dlist(list);
|
||||
free_dlist(new_list);
|
||||
free_dlist(new_new_list);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
127
tp1/tp1.h
Normal file
127
tp1/tp1.h
Normal file
@@ -0,0 +1,127 @@
|
||||
/********************************/
|
||||
/********************************/
|
||||
/* Prototypes des fonctions TP1 */
|
||||
/********************************/
|
||||
/********************************/
|
||||
|
||||
/***************************/
|
||||
/***************************/
|
||||
/* Exercice préliminaire */
|
||||
/***************************/
|
||||
/***************************/
|
||||
|
||||
// Doublement des majuscules dans une chaîne
|
||||
char *double_upper(char *s);
|
||||
|
||||
/******************************/
|
||||
/******************************/
|
||||
/* Liste doublement chaînées */
|
||||
/******************************/
|
||||
/******************************/
|
||||
|
||||
// Type utilisé pour représenter une cellule d'une liste doublement chaînée
|
||||
struct dlist {
|
||||
int value;
|
||||
struct dlist *next, *prev;
|
||||
};
|
||||
|
||||
// On définit dlist comme un synonyme de struct dlist.
|
||||
typedef struct dlist dlist;
|
||||
|
||||
/**********************/
|
||||
/* Fonctions basiques */
|
||||
/**********************/
|
||||
|
||||
// Création d'une liste vide
|
||||
dlist *create_empty_dlist();
|
||||
|
||||
// Ajout d'une nouvelle valeur (à droite ou à gauche de la cellule prise en
|
||||
// entrée)
|
||||
dlist *add_left_to_dlist(dlist *list, int value);
|
||||
dlist *add_right_to_dlist(dlist *list, int value);
|
||||
|
||||
// Suppression d'une cellule (attention à mettre à jour les pointeurs des
|
||||
// cellules adjacentes)
|
||||
void del_cell_dlist(dlist *list);
|
||||
|
||||
// Libération d'une dlist entière
|
||||
void free_dlist(dlist *list);
|
||||
|
||||
/*********************/
|
||||
/* Fonctions de test */
|
||||
/*********************/
|
||||
|
||||
// Génération d'une dlist contenant les n premiers entiers dans l'ordre:
|
||||
// 0 1 2 3 4 5 6 7 ...n-1.
|
||||
dlist *iota_dlist(int n);
|
||||
|
||||
// Affichage des valeurs de toutes les cellules d'une liste, dans l'ordre.
|
||||
// Pensez à tester vos fonctions précédentes avec print_dlist !
|
||||
void print_dlist(dlist *list);
|
||||
|
||||
// Génération d'une dlist aléatoire (on prend en argument la longueur voulue et
|
||||
// la valeur maximale).
|
||||
//
|
||||
// Utiliser la fonction random() pour générer des valeurs entières (la
|
||||
// documentation est accessible par man 3 random), et utiliser l'opérateur
|
||||
// modulo % pour ne pas dépasser la valeur maximale demandée.
|
||||
//
|
||||
// Par exemple, create_random_dlist(5, 10) renvoie une liste contenant 5 valeurs
|
||||
// inférieures à 10. Elle peut donc renvoyer, par exemple la liste 3 7 2 5 8.
|
||||
dlist *create_random_dlist(int length, int max);
|
||||
|
||||
/**********************************/
|
||||
/* Fonctions utilisant des listes */
|
||||
/**********************************/
|
||||
|
||||
// Crée une copie d'une dlist et renvoie cette copie.
|
||||
dlist *copy_dlist(dlist *list);
|
||||
|
||||
// Inversion d'une dlist: retourne une nouvelle dlist obtenue en inversant
|
||||
// l'ordre des valeurs dans celle prise en argument.
|
||||
dlist *reverse_dlist(dlist *list);
|
||||
|
||||
// Fusion alternée: retourne une nouvelle dlist obtenue en insérant de façon
|
||||
// alternée les valeurs des deux listes prises en argument. Si une des deux
|
||||
// listes est plus longue, les valeurs restantes sont ajoutées à la fin de la
|
||||
// liste renvoyée.
|
||||
dlist *mix_dlist(dlist *list1, dlist *list2);
|
||||
|
||||
// Place les cellules de valeur paire avant celles de numéro pair.
|
||||
// Par exemple, si la liste contient la séquence 1 2 3 4 5 6 7 8, elle doit être
|
||||
// modifiée pour contenir 2 4 6 8 1 3 5 7
|
||||
void evenodd_dlist(dlist *list);
|
||||
|
||||
/****************/
|
||||
/* Tri à bulles */
|
||||
/****************/
|
||||
|
||||
// Tri à bulles d'une dlist
|
||||
void bubblesort_dlist(dlist *list);
|
||||
|
||||
/**************/
|
||||
/* Tri fusion */
|
||||
/**************/
|
||||
|
||||
// Retire la première moitié des éléments de la liste prise en argument.
|
||||
// Retourne ces éléments dans une nouvelle liste.
|
||||
dlist *split_dlist(dlist *list);
|
||||
|
||||
// Les deux listes prises en arguments de la fonction merge_dlist sont supposées
|
||||
// triées. Fusionne ces deux listes dans la première pour obtenir une unique
|
||||
// liste triée.
|
||||
void merge_dlist(dlist *list1, dlist *list2);
|
||||
|
||||
// Tri fusion d'une liste, en utilisant les fonctions auxiliaires précédentes.
|
||||
void mergesort_dlist(dlist *list);
|
||||
|
||||
/**************/
|
||||
/* Tri rapide */
|
||||
/**************/
|
||||
|
||||
// Retire tous les éléments strictement plus petits que l'entier pivot dans la
|
||||
// liste prise en argument, et retourne ces éléments dans une nouvelle liste.
|
||||
dlist *pivot_dlist(int pivot, dlist *list);
|
||||
|
||||
// Tri rapide d'une liste, en utilisant la fonction auxiliaire précédente.
|
||||
void quicksort_dlist(dlist *list);
|
||||
Reference in New Issue
Block a user