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
8db795f2
Commit
8db795f2
authored
Nov 20, 2017
by
Unknown
Browse files
Test commit 2
parent
dc05fed3
Changes
3
Hide whitespace changes
Inline
Side-by-side
main.c
View file @
8db795f2
...
...
@@ -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
:
...
...
@@ -68,7 +68,7 @@ int main()
break
;
case
TOKEN_DEL_NB_SPC
:
remove_element
(
NULL
,
0
);
//
remove_element(NULL, 0);
break
;
case
TOKEN_SRT_LI_ASC
:
...
...
@@ -81,6 +81,7 @@ int main()
case
TOKEN_RMX_LI_ALL
:
destruct
(
&
default_list
);
initialize
(
&
default_list
);
break
;
}
...
...
tp3.c
View file @
8db795f2
...
...
@@ -66,41 +66,37 @@ 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
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
){
...
...
@@ -110,11 +106,11 @@ int insert_after_position(List* list, char* str, 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
"
);
return
-
1
;
}
/*
if(p <= 0){
printf("The provided value for the position is incorrect.\n");
return -1;
...
...
@@ -122,26 +118,20 @@ int remove_element(List* list, int p){
int n = 1; // position du parcours
Element* e = list->head, suppr;
/*
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
do{
e = e->next;
}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{
e->next = suppr->next;
free(suppr->data);
...
...
@@ -161,8 +151,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
"
);
...
...
@@ -247,5 +237,6 @@ void display_menu(){
printf
(
"7 - Destroy the list
\n
"
);
printf
(
"8 - Quit
\n
"
);
printf
(
"**************************************
\n\n
"
);
}
tp3.h
View file @
8db795f2
...
...
@@ -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