Skip to content
GitLab
Menu
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
5282aaec
Commit
5282aaec
authored
Nov 20, 2017
by
William Sha
Browse files
Merge remote-tracking branch 'origin/master'
parents
29f997d4
8db795f2
Changes
3
Hide whitespace changes
Inline
Side-by-side
main.c
View file @
5282aaec
...
...
@@ -7,14 +7,14 @@ int main()
if
(
DEBUG
)
printf
(
"Initialization of ChainedLists...
\n\n
"
);
int
action
=
0
;
// represents the choice of the user
char
str_number
[
5
];
char
test
[
5
];
char
str_number
[
100
];
//
char test[5];
printf
(
"*****CHAINED LISTS*****
\n
"
);
printf
(
"**UTC - November 2017**
\n
"
);
printf
(
"*****W.Sha - Y.Ly******
\n\n
"
);
List
*
lTest
=
malloc
(
sizeof
(
List
));
/*
List *lTest = malloc(sizeof(List));
Element *e1 = malloc(sizeof(Element));
Element *e2 = malloc(sizeof(Element));
e1->data = "yaya";
...
...
@@ -23,7 +23,7 @@ int main()
e2->next = NULL;
lTest->head = e1;
lTest->tail = e2;
display
(
lTest
);
display(lTest);
*/
if
(
DEBUG
)
printf
(
"Initialization of an instance of List...
\n\n
"
);
List
*
default_list
;
...
...
@@ -53,14 +53,14 @@ int main()
case
TOKEN_ADD_NB_BEG
:
printf
(
"What do you want to add at the beginning ? "
);
scanf
(
"%s"
,
str_number
);
insert_beginning_list
(
default_list
,
str_number
);
insert_beginning_list
(
&
default_list
,
str_number
);
if
(
DEBUG
)
printf
(
"Data inserted case : %s
\n
"
,
default_list
->
head
->
data
);
break
;
case
TOKEN_ADD_NB_END
:
printf
(
"What do you want to add at the end ? "
);
scanf
(
"%s"
,
str_number
);
insert_end_list
(
default_list
,
str_number
);
insert_end_list
(
&
default_list
,
str_number
);
break
;
case
TOKEN_ADD_NB_SPC
:
...
...
@@ -81,6 +81,7 @@ int main()
case
TOKEN_RMX_LI_ALL
:
destruct
(
&
default_list
);
initialize
(
&
default_list
);
break
;
}
...
...
tp3.c
View file @
5282aaec
...
...
@@ -60,46 +60,43 @@ void insert_empty_list(List** list, char* str){
e_vide
->
data
=
NULL
;
e_vide
->
next
=
NULL
;
(
*
list
)
->
tail
->
next
=
e_vide
;
(
*
list
)
->
tail
=
e_vide
;
//tail doit pointer sur le dernier element
if
(
DEBUG
)
printf
(
"Data inserted : %s
\n
"
,
(
*
list
)
->
head
->
data
);
}
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
if
(
list
->
head
==
NULL
&&
list
->
tail
==
NULL
){
if
(
(
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
insert_empty_list
(
&
list
,
str
);
insert_empty_list
(
list
,
str
);
return
;
}
Element
*
e
=
malloc
(
sizeof
(
Element
));
if
(
DEBUG
)
printf
(
"Trying to put input into a new element...
\n
"
);
List
*
tempList
=
malloc
(
sizeof
(
List
));
insert_empty_list
(
&
tempList
,
str
);
tempList
->
tail
->
next
=
(
*
list
)
->
head
;
(
*
list
)
->
head
=
tempList
->
head
;
free
(
tempList
);
char
tmp
[
N
];
if
(
DEBUG
)
printf
(
"Copy of string...
\n
"
);
strncpy
(
tmp
,
str
,
N
);
e
->
data
=
tmp
;
// changes the chain to plug the new element
if
(
DEBUG
)
printf
(
"Reorganizing the list...
\n
"
);
e
->
next
=
list
->
head
;
list
->
head
=
e
;
}
void
insert_end_list
(
List
*
list
,
char
*
str
){
void
insert_end_list
(
List
*
*
list
,
char
*
str
){
if
(
DEBUG
)
printf
(
"INSERT_END_LIST
\n
"
);
Element
*
e
=
malloc
(
sizeof
(
Element
));
char
tmp
[
N
];
strncpy
(
tmp
,
str
,
N
);
e
->
data
=
tmp
;
e
->
next
=
NULL
;
list
->
tail
->
next
=
e
;
list
->
tail
=
e
;
if
(
DEBUG
)
printf
(
"Data inserted : %s
\n
"
,
list
->
tail
->
data
);
if
(
DEBUG
)
printf
(
"Data head : %s
\n
"
,
list
->
head
->
data
);
if
((
*
list
)
->
head
==
NULL
&&
(
*
list
)
->
tail
==
NULL
){
if
(
DEBUG
)
printf
(
"The list is empty...
\n
"
);
insert_empty_list
(
list
,
str
);
return
;
}
List
*
tempList
=
malloc
(
sizeof
(
List
));
insert_empty_list
(
&
tempList
,
str
);
(
*
list
)
->
tail
->
next
=
tempList
->
head
;
(
*
list
)
->
tail
=
tempList
->
tail
;
free
(
tempList
);
}
int
insert_after_position
(
List
*
list
,
char
*
str
,
int
p
){
...
...
@@ -113,7 +110,7 @@ int remove_element(List* list, int p){
printf
(
"List pointer is null.
\n
"
);
return
-
1
;
}
/*
if(p <= 0){
printf("The provided value for the position is incorrect.\n");
return -1;
...
...
@@ -129,7 +126,7 @@ int remove_element(List* list, int p){
e sera utilisé pour pointer la bonne position, suppr sera utilisé pour "manger le nombre" (désallocation)
e will be used to focus on the right place, suppr will be used to "eat the number"
*/
/*
while(n < p){ // tant que l'on est pas arrivé au Pème chiffre
if (DEBUG) printf("WHILE\n");
do{
...
...
@@ -137,15 +134,8 @@ int remove_element(List* list, int p){
}while(e->data != NULL); // on parcoure la liste jusqu'au prochain nombre
suppr = e->next;
n++;
}
/* à ce niveau, on en est là :
P
| ##### | ##### | ¤¤¤¤¤ | ##### | ##### | ¤¤¤¤¤ |
^ ^
e suppr
*/
}*/
/*
do{
if (DEBUG) printf("DO2\n");
e->next = suppr->next;
...
...
@@ -166,8 +156,8 @@ int remove_element(List* list, int p){
free(suppr);
}
return
0
;
}
return 0;
*/
}
int
compare
(
char
*
str1
,
char
*
str2
){
if
(
DEBUG
)
printf
(
"COMPARE
\n
"
);
...
...
@@ -219,7 +209,7 @@ void destruct(List** list){
return
;
}
while
((
*
list
)
->
head
!=
(
*
list
)
->
tail
){
while
((
*
list
)
->
head
->
next
!=
NULL
){
/*cetait pas la bonne condition*/
printf
(
"while
\n
"
);
Element
*
e
=
(
*
list
)
->
head
;
printf
(
"pointeurs : list head : %p, e : %p
\n
"
,
(
*
list
)
->
head
,
e
);
...
...
@@ -230,7 +220,9 @@ void destruct(List** list){
printf
(
"free
\n
"
);
printf
(
"pointeurs : list head : %p, e : %p
\n
"
,
(
*
list
)
->
head
,
e
);
free
(
e
->
data
);
printf
(
"free2
\n
"
);
free
(
e
);
printf
(
"free3
\n
"
);
}
free
(
*
list
);
...
...
@@ -250,5 +242,6 @@ void display_menu(){
printf
(
"7 - Destroy the list
\n
"
);
printf
(
"8 - Quit
\n
"
);
printf
(
"**************************************
\n\n
"
);
}
tp3.h
View file @
5282aaec
...
...
@@ -21,8 +21,8 @@ typedef struct list{
void
initialize
(
List
**
list
);
void
insert_empty_list
(
List
**
list
,
char
*
str
);
void
insert_beginning_list
(
List
*
list
,
char
*
str
);
void
insert_end_list
(
List
*
list
,
char
*
str
);
void
insert_beginning_list
(
List
*
*
list
,
char
*
str
);
void
insert_end_list
(
List
*
*
list
,
char
*
str
);
int
insert_after_position
(
List
*
list
,
char
*
str
,
int
p
);
int
remove_element
(
List
*
list
,
int
p
);
int
compare
(
char
*
str1
,
char
*
str2
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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