Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prog_C
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
thibault.chatillo
Prog_C
Commits
60cdc454
Commit
60cdc454
authored
4 years ago
by
thib
Browse files
Options
Downloads
Patches
Plain Diff
joseph
parent
389b7d40
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
circular_list/Makefile
+4
-0
4 additions, 0 deletions
circular_list/Makefile
circular_list/circ.c
+96
-15
96 additions, 15 deletions
circular_list/circ.c
circular_list/circ.h
+5
-0
5 additions, 0 deletions
circular_list/circ.h
circular_list/main.c
+3
-29
3 additions, 29 deletions
circular_list/main.c
with
108 additions
and
44 deletions
circular_list/Makefile
+
4
−
0
View file @
60cdc454
...
...
@@ -2,6 +2,10 @@ cc=gcc
CFLAGS
=
-Wextra
-Wall
-g
-fsanitize
=
address
-fsanitize
=
leak
LFLAGS
=
-lm
main
:
circ.x
./circ.x
circ.x
:
circ.o main.o
$(
cc
)
-o
$@
$^
$(
CFLAGS
)
$(
LFLAGS
)
...
...
This diff is collapsed.
Click to expand it.
circular_list/circ.c
+
96
−
15
View file @
60cdc454
#include
"circ.h"
#include
<stdio.h>
#include
<stdlib.h>
// typedef struct element {
...
...
@@ -25,6 +26,7 @@ int list_count(element *head) {
crt
=
crt
->
next
;
count
++
;
}
printf
(
"
\n
%d elements
\n
"
,
count
);
return
count
;
}
...
...
@@ -35,19 +37,25 @@ element *list_move(element *head) {
return
head
;
}
element
*
move
(
element
*
head
,
int
i
)
{
while
(
i
>
0
)
{
head
=
list_move
(
head
);
i
--
;
}
return
head
;
}
// Insert un élément après le pointeur head. Retourne le pointeur sur l’élé-ment
// inséré
element
*
list_insert_after
(
element
*
head
,
void
*
data
)
{
if
(
list_empty
(
head
))
{
printf
(
"insert empty
\n
"
);
head
=
malloc
(
sizeof
(
element
));
head
->
content
=
data
;
head
->
next
=
head
;
return
head
;
}
printf
(
"insert not empty
\n
"
);
element
*
tmp
=
malloc
(
sizeof
(
element
));
tmp
->
content
=
data
;
tmp
->
next
=
head
->
next
;
...
...
@@ -58,9 +66,17 @@ element *list_insert_after(element *head, void *data) {
// Insert un élément avant le pointeur head. Retourne le pointeur sur l’élé-ment
// inséré
element
*
list_insert_before
(
element
*
head
,
void
*
data
)
{
if
(
list_empty
(
head
))
{
head
=
malloc
(
sizeof
(
element
));
head
->
content
=
data
;
head
->
next
=
head
;
return
head
;
}
element
*
crt
=
head
;
while
(
crt
->
next
!=
head
)
{
list_move
(
crt
)
;
crt
=
crt
->
next
;
}
element
*
tmp
=
malloc
(
sizeof
(
element
));
tmp
->
content
=
data
;
...
...
@@ -91,29 +107,94 @@ element *list_search(element *head, void *data,
// ouNULLsi l’élément est absent
element
*
list_remove
(
element
*
head
,
void
*
data
,
int
(
*
compare
)(
void
*
,
void
*
))
{
element
*
tmp
=
list_search
(
head
,
data
,
compare
);
if
(
tmp
==
NULL
)
{
element
*
crt
=
head
;
while
(
compare
(
crt
->
next
->
content
,
data
))
{
crt
=
crt
->
next
;
if
(
crt
->
next
==
head
)
{
return
NULL
;
}
element
*
removed
=
tmp
->
next
;
tmp
->
next
=
tmp
->
next
->
next
;
}
element
*
removed
=
crt
->
next
;
crt
->
next
=
crt
->
next
->
next
;
return
removed
;
}
// Supprime toute la liste en libérant chaque élément
void
list_free
(
element
*
head
,
void
(
*
data_free
)(
void
*
))
{
element
*
tmp
=
head
;
while
(
!
list_empty
(
tmp
))
{
tmp
=
tmp
->
next
;
data_free
(
head
);
element
*
tmp
=
head
->
next
;
while
(
tmp
!=
head
)
{
head
->
next
=
tmp
->
next
;
data_free
(
tmp
);
tmp
=
head
->
next
;
}
data_free
(
head
);
}
// Appel d’une fonction de traitement quelconque sur chaque élément de laliste
void
list_process
(
element
*
head
,
void
*
(
*
action
)(
void
*
))
{
element
*
crt
=
head
;
while
(
crt
->
next
!=
head
)
{
action
(
crt
->
content
);
element
*
crt
=
head
->
next
;
while
(
crt
!=
head
)
{
crt
=
action
(
crt
);
crt
=
crt
->
next
;
}
crt
=
action
(
head
);
}
void
*
print_int
(
void
*
x
)
{
printf
(
"%d "
,
*
(
int
*
)((
element
*
)
x
)
->
content
);
return
x
;
}
void
free_simple
(
void
*
ptr
)
{
free
(
ptr
);
}
void
custom_free
(
void
*
ptr
)
{
free
(((
element
*
)
ptr
)
->
content
);
free
(
ptr
);
}
int
cmp_int
(
void
*
a
,
void
*
b
)
{
if
(
*
(
int
*
)
a
>
*
(
int
*
)
b
)
{
return
1
;
}
else
if
(
*
(
int
*
)
a
<
*
(
int
*
)
b
)
{
return
-
1
;
}
else
{
return
0
;
}
}
void
joseph
(
element
*
head
,
int
n
,
int
k
)
{
head
=
list_create
();
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
int
*
x
=
malloc
(
sizeof
(
int
));
*
x
=
i
+
1
;
head
=
list_insert_after
(
head
,
x
);
}
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
head
=
move
(
head
,
k
-
1
);
element
*
tmp
=
remove_crt
(
head
);
printf
(
"%d
\n
"
,
*
(
int
*
)
tmp
->
content
);
custom_free
(
tmp
);
}
}
element
*
remove_crt
(
element
*
head
)
{
element
*
crt
=
head
->
next
;
while
(
crt
!=
head
)
{
crt
=
crt
->
next
;
}
element
*
removed
=
crt
->
next
;
crt
->
next
=
crt
->
next
->
next
;
return
removed
;
}
This diff is collapsed.
Click to expand it.
circular_list/circ.h
+
5
−
0
View file @
60cdc454
...
...
@@ -22,6 +22,8 @@ int list_count(element* head);
// Déplace le pointeur head sur l’élément suivant. Retourne la nouvelle têtede liste
element
*
list_move
(
element
*
head
);
element
*
move
(
element
*
head
,
int
i
);
// Insert un élément après le pointeur head. Retourne le pointeur sur l’élé-ment inséré
element
*
list_insert_after
(
element
*
head
,
void
*
data
);
...
...
@@ -34,12 +36,15 @@ element* list_search(element* head,void* data,int (*compare)(void*, void*));
// Supprime un élément de la liste sans libérer l’élément pointé. Renvoie unpointeur sur le 1er élément trouvé à supprimer et l’enlève de la liste, ouNULLsi l’élément est absent
element
*
list_remove
(
element
*
head
,
void
*
data
,
int
(
*
compare
)(
void
*
,
void
*
));
element
*
remove_crt
(
element
*
head
);
// Supprime toute la liste en libérant chaque élément
void
list_free
(
element
*
head
,
void
(
*
data_free
)(
void
*
));
// Appel d’une fonction de traitement quelconque sur chaque élément de laliste
void
list_process
(
element
*
head
,
void
*
(
*
action
)(
void
*
));
void
joseph
(
element
*
head
,
int
n
,
int
k
);
...
...
This diff is collapsed.
Click to expand it.
circular_list/main.c
+
3
−
29
View file @
60cdc454
#include
"circ.h"
#include
<stdlib.h>
void
*
print_int
(
void
*
x
)
{
printf
(
"%d "
,
*
(
int
*
)
x
);
return
x
;
}
void
free_simple
(
void
*
ptr
)
{
free
(
ptr
);
}
int
main
()
{
element
*
head
=
list_create
();
printf
(
"%d
\n
"
,
list_count
(
head
));
int
a
=
1
;
int
b
=
2
;
int
c
=
3
;
head
=
list_insert_after
(
head
,
&
a
);
head
=
list_insert_after
(
head
,
&
b
);
head
=
list_insert_after
(
head
,
&
a
);
// list_insert_after(head, &b);
// list_insert_after(head, &c);
printf
(
"%d
\n
"
,
list_count
(
head
));
// for (int i = 0; i < 8; i++) {
// list_insert_after(head, &i);
// }
// list_process(head, print_int);
// list_free(head, free_simple);
int
main
()
{
element
*
head
;
joseph
(
head
,
8
,
3
);
return
EXIT_SUCCESS
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment