Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
William Sha
ai01_tp1
Commits
1d753eef
Commit
1d753eef
authored
Nov 22, 2017
by
William Sha
Browse files
Added that f*cking sort function and so on...
Implemented sort, lengthInteger. Updated compare, Removed initials printf.
parent
a8084cd1
Changes
2
Hide whitespace changes
Inline
Side-by-side
main.c
View file @
1d753eef
...
@@ -66,7 +66,7 @@ int main()
...
@@ -66,7 +66,7 @@ int main()
break
;
break
;
case
TOKEN_SRT_LI_ASC
:
case
TOKEN_SRT_LI_ASC
:
sort
(
NULL
);
sort
(
default_list
);
break
;
break
;
case
TOKEN_DSP_LI_STD
:
case
TOKEN_DSP_LI_STD
:
...
...
tp3.c
View file @
1d753eef
...
@@ -11,15 +11,12 @@
...
@@ -11,15 +11,12 @@
void
initialize
(
List
**
list
){
void
initialize
(
List
**
list
){
if
(
DEBUG
)
printf
(
"INITIALIZE
\n
"
);
*
list
=
malloc
(
sizeof
(
List
));
*
list
=
malloc
(
sizeof
(
List
));
(
*
list
)
->
head
=
NULL
;
(
*
list
)
->
head
=
NULL
;
(
*
list
)
->
tail
=
NULL
;
(
*
list
)
->
tail
=
NULL
;
}
}
void
insert_empty_list
(
List
**
list
,
char
*
str
){
void
insert_empty_list
(
List
**
list
,
char
*
str
){
if
(
DEBUG
)
printf
(
"INSERT_EMPTY_LIST
\n
"
);
if
((
*
list
)
->
head
||
(
*
list
)
->
tail
){
if
((
*
list
)
->
head
||
(
*
list
)
->
tail
){
printf
(
"List is not empty... use another insert function.
\n
"
);
printf
(
"List is not empty... use another insert function.
\n
"
);
return
;
return
;
...
@@ -38,7 +35,7 @@ void insert_empty_list(List** list, char* str){
...
@@ -38,7 +35,7 @@ void insert_empty_list(List** list, char* str){
while
(
length
>
0
){
// O(strlen(str)) car le second while est lié
while
(
length
>
0
){
// O(strlen(str)) car le second while est lié
i
=
0
;
i
=
0
;
Element
*
e
=
malloc
(
sizeof
(
Element
));
Element
*
e
=
malloc
(
sizeof
(
Element
));
e
->
data
=
malloc
(
sizeof
(
char
)
*
N
);
e
->
data
=
malloc
(
sizeof
(
char
)
*
(
N
+
1
)
);
while
(
length
>
0
&&
i
<
5
){
// tant qu'il reste de l'input et que l'on a pas rempli la data
while
(
length
>
0
&&
i
<
5
){
// tant qu'il reste de l'input et que l'on a pas rempli la data
*
(
e
->
data
+
i
)
=
*
(
str
+
total
+
i
);
*
(
e
->
data
+
i
)
=
*
(
str
+
total
+
i
);
i
++
;
i
++
;
...
@@ -65,7 +62,6 @@ void insert_empty_list(List** list, char* str){
...
@@ -65,7 +62,6 @@ void insert_empty_list(List** list, char* str){
}
}
void
insert_beginning_list
(
List
**
list
,
char
*
str
){
void
insert_beginning_list
(
List
**
list
,
char
*
str
){
if
(
DEBUG
)
printf
(
"INSERT_BEGINNING_LIST(%p, %s)
\n
"
,
list
,
str
);
// empty list
// empty list
if
((
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
((
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
...
@@ -83,7 +79,6 @@ void insert_beginning_list(List** list, char* str){
...
@@ -83,7 +79,6 @@ void insert_beginning_list(List** list, char* str){
}
}
void
insert_end_list
(
List
**
list
,
char
*
str
){
void
insert_end_list
(
List
**
list
,
char
*
str
){
if
(
DEBUG
)
printf
(
"INSERT_END_LIST
\n
"
);
if
((
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
((
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
insert_empty_list
(
list
,
str
);
insert_empty_list
(
list
,
str
);
...
@@ -100,7 +95,6 @@ void insert_end_list(List** list, char* str){
...
@@ -100,7 +95,6 @@ void insert_end_list(List** list, char* str){
}
}
int
insert_after_position
(
List
**
list
,
char
*
str
,
int
p
){
int
insert_after_position
(
List
**
list
,
char
*
str
,
int
p
){
if
(
DEBUG
)
printf
(
"INSERT_AFTER_POSITION
\n
"
);
if
(
!
(
*
list
)){
if
(
!
(
*
list
)){
printf
(
"List doesn't exist.
\n
"
);
printf
(
"List doesn't exist.
\n
"
);
return
-
1
;
return
-
1
;
...
@@ -145,8 +139,6 @@ int insert_after_position(List** list, char* str, int p){
...
@@ -145,8 +139,6 @@ int insert_after_position(List** list, char* str, int p){
}
}
int
remove_element
(
List
*
list
,
int
p
){
int
remove_element
(
List
*
list
,
int
p
){
if
(
DEBUG
)
printf
(
"REMOVE_ELEMENT
\n
"
);
if
(
!
list
){
if
(
!
list
){
printf
(
"List pointer is null.
\n
"
);
printf
(
"List pointer is null.
\n
"
);
return
-
1
;
return
-
1
;
...
@@ -166,7 +158,6 @@ int remove_element(List* list, int p){
...
@@ -166,7 +158,6 @@ int remove_element(List* list, int p){
*/
*/
while
(
n
<=
p
){
// tant que l'on est pas arrivé au Pème chiffre
while
(
n
<=
p
){
// tant que l'on est pas arrivé au Pème chiffre
if
(
DEBUG
)
printf
(
"WHILE
\n
"
);
do
{
do
{
e
=
e
->
next
;
e
=
e
->
next
;
}
while
(
e
->
data
!=
NULL
);
// on parcoure la liste jusqu'au prochain nombre
}
while
(
e
->
data
!=
NULL
);
// on parcoure la liste jusqu'au prochain nombre
...
@@ -216,14 +207,13 @@ int remove_element(List* list, int p){
...
@@ -216,14 +207,13 @@ int remove_element(List* list, int p){
}
}
int
compare
(
char
*
str1
,
char
*
str2
){
int
compare
(
char
*
str1
,
char
*
str2
){
if
(
DEBUG
)
printf
(
"COMPARE
\n
"
);
if
(
!
str1
||
(
strlen
(
str1
)
<
strlen
(
str2
)))
return
2
;
if
(
!
str2
||
(
strlen
(
str1
)
>
strlen
(
str2
)))
return
1
;
if
(
!
str1
)
return
2
;
if
(
!
str2
)
return
1
;
int
length
=
strlen
(
str1
);
char
car1
=
*
str1
,
car2
=
*
str2
;
char
car1
=
*
str1
,
car2
=
*
str2
;
int
int1
=
0
,
int2
=
0
,
i
=
0
;
int
int1
=
0
,
int2
=
0
,
i
=
0
;
int
length
=
strlen
(
str1
)
>
strlen
(
str2
)
?
strlen
(
str2
)
:
strlen
(
str1
);
while
(
i
<
length
){
// O(min(strlen(str1), strlen(str2)))
while
(
i
<
length
){
// O(min(strlen(str1), strlen(str2)))
int1
=
int1
*
10
+
car1
-
'0'
;
int1
=
int1
*
10
+
car1
-
'0'
;
...
@@ -239,12 +229,96 @@ int compare(char* str1, char* str2){
...
@@ -239,12 +229,96 @@ int compare(char* str1, char* str2){
return
2
;
return
2
;
}
}
int
sort
(
List
*
list
){
int
sort
(
List
*
list
){
// tri à bulles
if
(
DEBUG
)
printf
(
"SORT
\n
"
);
int
l
=
lengthList
(
list
);
if
(
!
list
){
printf
(
"List does not exist and can't be sorted."
);
return
-
1
;
}
if
(
l
<=
1
){
return
0
;
// rien à trier
}
char
*
n1
=
malloc
(
sizeof
(
char
)
*
256
);
n1
[
0
]
=
'\0'
;
char
*
n2
=
malloc
(
sizeof
(
char
)
*
256
);
n2
[
0
]
=
'\0'
;
Element
*
start
,
*
interval
,
*
fh
,
*
ft
,
*
sh
,
*
st
,
*
read
,
*
ox
;
// start, firsthead, firsttail, secondhead, secondtail
// pour faire le tri à bulles, il faut repérer les deux nombres par leurs débuts et leurs fins, d'oû 4 pointeurs d'élement fh ft sh st
// cela nous servira pour les facilement switcher les nombres
interval
=
list
->
tail
;
start
=
list
->
head
;
fh
=
start
;
ft
=
fh
;
while
(
ft
->
next
->
data
!=
NULL
){
ft
=
fh
->
next
;
}
while
(
interval
!=
list
->
head
){
do
// un passage de bulle
{
sh
=
ft
->
next
->
next
;
st
=
sh
;
while
(
st
->
next
->
data
!=
NULL
){
st
=
sh
->
next
;
}
for
(
read
=
fh
;
read
!=
ft
->
next
;
read
=
read
->
next
){
// on lit la première valeur
n1
=
strcat
(
n1
,
read
->
data
);
// concatenation
}
for
(
read
=
sh
;
read
!=
st
->
next
;
read
=
read
->
next
){
// on lit la première valeur
n2
=
strcat
(
n2
,
read
->
data
);
// concatenation
}
if
(
compare
(
n1
,
n2
)
==
1
){
ox
=
st
->
next
;
st
->
next
=
ft
->
next
;
ft
->
next
=
ox
;
st
->
next
->
next
=
fh
;
/* free(ft->next); // on libère l'élément de séparation des deux nombres
ft->next = st->next; // la fin du premier nombre pointe sur le reste de la liste
st->next = fh;*/
if
(
fh
==
list
->
head
){
list
->
head
=
sh
;
// le second nombre se positionne en tete de liste
}
else
{
start
->
next
=
sh
;
}
// on permute pour remettre les pointeurs dans l'ordre
ox
=
fh
;
fh
=
sh
;
sh
=
ox
;
ox
=
ft
;
ft
=
st
;
st
=
ox
;
display
(
list
);
}
start
=
ft
->
next
;
fh
=
start
->
next
;
ft
=
fh
;
while
(
ft
->
next
->
data
!=
NULL
){
ft
=
fh
->
next
;
}
n1
[
0
]
=
'\0'
;
n2
[
0
]
=
'\0'
;
}
while
(
ft
->
next
!=
interval
&&
ft
->
next
!=
list
->
tail
&&
ft
->
next
->
next
!=
interval
);
interval
=
sh
;
start
=
list
->
head
;
fh
=
start
;
ft
=
fh
;
while
(
ft
->
next
->
data
!=
NULL
){
ft
=
fh
->
next
;
}
if
(
ft
->
next
->
next
==
sh
){
// le pointeur interval est sur le deuxième nombre donc on a trié tout le reste après
interval
=
list
->
head
;
}
display
(
list
);
}
free
(
n1
);
free
(
n2
);
}
}
void
display
(
List
*
list
){
void
display
(
List
*
list
){
if
(
DEBUG
)
printf
(
"DISPLAY
\n
"
);
if
(
!
list
||
!
list
->
head
){
if
(
!
list
||
!
list
->
head
){
printf
(
"Nothing to display...
\n
"
);
printf
(
"Nothing to display...
\n
"
);
return
;
return
;
...
@@ -253,8 +327,6 @@ void display(List* list){
...
@@ -253,8 +327,6 @@ void display(List* list){
int
c
;
int
c
;
c
=
lengthList
(
list
);
c
=
lengthList
(
list
);
printf
(
"length list: %d
\n
"
,
c
);
Element
*
p
=
list
->
head
;
Element
*
p
=
list
->
head
;
for
(
p
=
list
->
head
;
p
!=
NULL
;
p
=
p
->
next
){
for
(
p
=
list
->
head
;
p
!=
NULL
;
p
=
p
->
next
){
printf
(
"element: %s
\n
"
,
p
->
data
);
printf
(
"element: %s
\n
"
,
p
->
data
);
...
@@ -264,8 +336,6 @@ void display(List* list){
...
@@ -264,8 +336,6 @@ void display(List* list){
}
}
void
destruct
(
List
**
list
){
void
destruct
(
List
**
list
){
if
(
DEBUG
)
printf
(
"DESTRUCT
\n
"
);
if
(
!*
list
){
if
(
!*
list
){
printf
(
"List is already destroyed...
\n
"
);
printf
(
"List is already destroyed...
\n
"
);
return
;
return
;
...
@@ -325,3 +395,10 @@ int lengthList (List *list){
...
@@ -325,3 +395,10 @@ int lengthList (List *list){
return
compteur
;
return
compteur
;
}
}
int
lengthInteger
(
int
nb
){
if
(
nb
==
0
)
return
1
;
else
return
floor
(
log10
(
abs
(
nb
)))
+
1
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment