Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC 142 - Travail Pratique 011
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
Package Registry
Model registry
Operate
Environments
Terraform modules
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
florian.burgener
ISC 142 - Travail Pratique 011
Commits
0cba4092
Commit
0cba4092
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Update of the header and the code as a result of the change of signature of the functions
parent
6d45ad9a
Branches
main
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
circ_list.c
+136
-126
136 additions, 126 deletions
circ_list.c
circ_list.h
+22
-22
22 additions, 22 deletions
circ_list.h
circ_list_tests.c
+102
-101
102 additions, 101 deletions
circ_list_tests.c
joseph.c
+4
-11
4 additions, 11 deletions
joseph.c
with
264 additions
and
260 deletions
circ_list.c
+
136
−
126
View file @
0cba4092
#include
"circ_list.h"
#include
<stdlib.h>
element
*
list_create
()
{
return
NULL
;
}
bool
list_empty
(
element
*
head
)
{
return
head
==
NULL
;
}
unsigned
int
list_count
(
element
*
head
)
{
if
(
list_empty
(
head
))
{
return
0
;
}
unsigned
int
count
=
0
;
element
*
iterator
=
head
;
do
{
count
+=
1
;
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
return
count
;
}
element
*
list_move
(
element
*
head
)
{
if
(
list_empty
(
head
))
{
return
NULL
;
}
return
head
->
next
;
}
element
*
list_insert_after
(
element
*
head
,
int
data
)
{
element
*
new_element
=
(
element
*
)
malloc
(
sizeof
(
element
));
new_element
->
data
=
data
;
if
(
list_empty
(
head
))
{
new_element
->
next
=
new_element
;
}
else
{
new_element
->
next
=
head
->
next
;
head
->
next
=
new_element
;
}
return
new_element
;
}
element
*
list_insert_before
(
element
*
head
,
int
data
)
{
if
(
list_empty
(
head
))
{
return
list_insert_after
(
head
,
data
);
}
element
*
iterator
=
head
;
while
(
iterator
->
next
!=
head
)
{
iterator
=
iterator
->
next
;
}
return
list_insert_after
(
iterator
,
data
);
}
element
*
list_search
(
element
*
head
,
int
data
)
{
if
(
list_empty
(
head
))
{
return
NULL
;
}
element
*
iterator
=
head
;
do
{
if
(
iterator
->
data
==
data
)
{
return
iterator
;
}
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
return
NULL
;
}
// This function cannot work when the element to be removed is the head.
element
*
list_remove
(
element
*
head
,
int
data
)
{
element
*
el
=
list_search
(
head
,
data
);
if
(
el
==
NULL
)
{
return
NULL
;
}
element
*
iterator
=
head
;
while
(
iterator
->
next
!=
el
)
{
iterator
=
iterator
->
next
;
}
iterator
->
next
=
iterator
->
next
->
next
;
return
el
;
}
void
list_free
(
element
*
head
)
{
if
(
list_empty
(
head
))
{
return
;
}
element
*
iterator
=
head
;
do
{
element
*
tmp
=
iterator
;
iterator
=
iterator
->
next
;
free
(
tmp
);
}
while
(
iterator
!=
head
);
}
void
list_process
(
element
*
head
,
int
(
*
action
)(
int
))
{
if
(
list_empty
(
head
))
{
return
;
}
element
*
iterator
=
head
;
do
{
iterator
->
data
=
action
(
iterator
->
data
);
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
}
#include
"circ_list.h"
#include
<stdlib.h>
element
*
list_create
()
{
return
NULL
;
}
bool
list_empty
(
element
*
head
)
{
return
head
==
NULL
;
}
unsigned
int
list_count
(
element
*
head
)
{
if
(
list_empty
(
head
))
{
return
0
;
}
unsigned
int
count
=
0
;
element
*
iterator
=
head
;
do
{
count
+=
1
;
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
return
count
;
}
element
*
list_move
(
element
*
head
)
{
if
(
list_empty
(
head
))
{
return
NULL
;
}
return
head
->
next
;
}
element
*
list_insert_after
(
element
*
head
,
int
data
)
{
element
*
new_element
=
(
element
*
)
malloc
(
sizeof
(
element
));
new_element
->
data
=
data
;
if
(
list_empty
(
head
))
{
new_element
->
next
=
new_element
;
}
else
{
new_element
->
next
=
head
->
next
;
head
->
next
=
new_element
;
}
return
new_element
;
}
element
*
list_insert_before
(
element
*
head
,
int
data
)
{
if
(
list_empty
(
head
))
{
return
list_insert_after
(
head
,
data
);
}
element
*
iterator
=
head
;
while
(
iterator
->
next
!=
head
)
{
iterator
=
iterator
->
next
;
}
return
list_insert_after
(
iterator
,
data
);
}
element
*
list_search
(
element
*
head
,
int
data
)
{
if
(
list_empty
(
head
))
{
return
NULL
;
}
element
*
iterator
=
head
;
do
{
if
(
iterator
->
data
==
data
)
{
return
iterator
;
}
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
return
NULL
;
}
element
*
list_remove
(
element
**
head
,
int
data
)
{
element
*
el
=
list_search
(
*
head
,
data
);
if
(
el
==
NULL
)
{
return
NULL
;
}
element
*
iterator
=
*
head
;
while
(
iterator
->
next
!=
el
)
{
iterator
=
iterator
->
next
;
}
iterator
->
next
=
iterator
->
next
->
next
;
if
(
el
==
*
head
)
{
*
head
=
iterator
;
}
if
(
iterator
==
el
)
{
*
head
=
NULL
;
}
return
el
;
}
void
list_free
(
element
**
head
)
{
if
(
list_empty
(
*
head
))
{
return
;
}
element
*
iterator
=
*
head
;
do
{
element
*
tmp
=
iterator
;
iterator
=
iterator
->
next
;
free
(
tmp
);
}
while
(
iterator
!=
*
head
);
*
head
=
NULL
;
}
void
list_process
(
element
*
head
,
int
(
*
action
)(
int
))
{
if
(
list_empty
(
head
))
{
return
;
}
element
*
iterator
=
head
;
do
{
iterator
->
data
=
action
(
iterator
->
data
);
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
head
);
}
This diff is collapsed.
Click to expand it.
circ_list.h
+
22
−
22
View file @
0cba4092
#ifndef CIRC_LIST_H
#define CIRC_LIST_H
#include
<stdbool.h>
typedef
struct
element
{
int
data
;
struct
element
*
next
;
}
element
;
element
*
list_create
();
bool
list_empty
(
element
*
head
);
unsigned
int
list_count
(
element
*
head
);
element
*
list_move
(
element
*
head
);
element
*
list_insert_after
(
element
*
head
,
int
data
);
element
*
list_insert_before
(
element
*
head
,
int
data
);
element
*
list_search
(
element
*
head
,
int
data
);
element
*
list_remove
(
element
*
head
,
int
data
);
void
list_free
(
element
*
head
);
void
list_process
(
element
*
head
,
int
(
*
action
)(
int
));
#endif
#ifndef CIRC_LIST_H
#define CIRC_LIST_H
#include
<stdbool.h>
typedef
struct
element
{
int
data
;
struct
element
*
next
;
}
element
;
element
*
list_create
();
bool
list_empty
(
element
*
head
);
unsigned
int
list_count
(
element
*
head
);
element
*
list_move
(
element
*
head
);
element
*
list_insert_after
(
element
*
head
,
int
data
);
element
*
list_insert_before
(
element
*
head
,
int
data
);
element
*
list_search
(
element
*
head
,
int
data
);
element
*
list_remove
(
element
*
*
head
,
int
data
);
void
list_free
(
element
*
*
head
);
void
list_process
(
element
*
head
,
int
(
*
action
)(
int
));
#endif
This diff is collapsed.
Click to expand it.
circ_list_tests.c
+
102
−
101
View file @
0cba4092
#include
<assert.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
"circ_list.h"
int
action_add_10
(
int
element
)
{
return
element
+
10
;
}
void
print_list
(
element
*
list
)
{
element
*
iterator
=
list
;
do
{
printf
(
"%d "
,
iterator
->
data
);
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
list
);
printf
(
"
\n
"
);
}
int
main
()
{
// list_create
element
*
list
=
list_create
();
assert
(
list
==
NULL
);
// list_insert_after
printf
(
"list_insert_after
\n
"
);
list
=
list_insert_after
(
list
,
1
);
assert
(
list
->
data
==
1
);
print_list
(
list
);
list
=
list_insert_after
(
list
,
3
);
assert
(
list
->
data
==
3
);
assert
(
list
->
next
->
data
==
1
);
assert
(
list
->
next
->
next
->
data
==
3
);
print_list
(
list
);
list
=
list_insert_before
(
list
,
2
);
printf
(
"list_insert_before
\n
"
);
assert
(
list
->
data
==
2
);
assert
(
list
->
next
->
data
==
3
);
assert
(
list
->
next
->
next
->
data
==
1
);
assert
(
list
->
next
->
next
->
next
->
data
==
2
);
print_list
(
list
);
// list_empty
element
*
empty_list
=
list_create
();
assert
(
list_empty
(
empty_list
)
==
true
);
assert
(
list_empty
(
list
)
==
false
);
// list_count
assert
(
list_count
(
empty_list
)
==
0
);
assert
(
list_count
(
list
)
==
3
);
// list_search
element
*
search_result_1
=
list_search
(
list
,
1
);
assert
(
search_result_1
->
data
==
1
);
element
*
search_result_2
=
list_search
(
list
,
4
);
assert
(
search_result_2
==
NULL
);
// list_process
printf
(
"list_process
\n
"
);
list_process
(
list
,
action_add_10
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
13
);
assert
(
list
->
next
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
next
->
data
==
12
);
print_list
(
list
);
// list_remove
printf
(
"list_remove
\n
"
);
element
*
removed_element_1
=
list_remove
(
list
,
13
);
assert
(
removed_element_1
->
data
==
13
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
data
==
12
);
free
(
removed_element_1
);
print_list
(
list
);
// list_move
printf
(
"list_move
\n
"
);
list
=
list_move
(
list
);
assert
(
list
->
data
==
11
);
assert
(
list
->
next
->
data
==
12
);
assert
(
list
->
next
->
next
->
data
==
11
);
print_list
(
list
);
list
=
list_move
(
list
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
data
==
12
);
print_list
(
list
);
// list_free
list_free
(
list
);
}
#include
<assert.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
"circ_list.h"
int
action_add_10
(
int
element
)
{
return
element
+
10
;
}
void
print_list
(
element
*
list
)
{
element
*
iterator
=
list
;
do
{
printf
(
"%d "
,
iterator
->
data
);
iterator
=
iterator
->
next
;
}
while
(
iterator
!=
list
);
printf
(
"
\n
"
);
}
int
main
()
{
// list_create
element
*
list
=
list_create
();
assert
(
list
==
NULL
);
// list_insert_after
printf
(
"list_insert_after
\n
"
);
list
=
list_insert_after
(
list
,
1
);
assert
(
list
->
data
==
1
);
print_list
(
list
);
list
=
list_insert_after
(
list
,
3
);
assert
(
list
->
data
==
3
);
assert
(
list
->
next
->
data
==
1
);
assert
(
list
->
next
->
next
->
data
==
3
);
print_list
(
list
);
list
=
list_insert_before
(
list
,
2
);
printf
(
"list_insert_before
\n
"
);
assert
(
list
->
data
==
2
);
assert
(
list
->
next
->
data
==
3
);
assert
(
list
->
next
->
next
->
data
==
1
);
assert
(
list
->
next
->
next
->
next
->
data
==
2
);
print_list
(
list
);
// list_empty
element
*
empty_list
=
list_create
();
assert
(
list_empty
(
empty_list
)
==
true
);
assert
(
list_empty
(
list
)
==
false
);
// list_count
assert
(
list_count
(
empty_list
)
==
0
);
assert
(
list_count
(
list
)
==
3
);
// list_search
element
*
search_result_1
=
list_search
(
list
,
1
);
assert
(
search_result_1
->
data
==
1
);
element
*
search_result_2
=
list_search
(
list
,
4
);
assert
(
search_result_2
==
NULL
);
// list_process
printf
(
"list_process
\n
"
);
list_process
(
list
,
action_add_10
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
13
);
assert
(
list
->
next
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
next
->
data
==
12
);
print_list
(
list
);
// list_remove
printf
(
"list_remove
\n
"
);
element
*
removed_element_1
=
list_remove
(
&
list
,
13
);
assert
(
removed_element_1
->
data
==
13
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
data
==
12
);
free
(
removed_element_1
);
print_list
(
list
);
// list_move
printf
(
"list_move
\n
"
);
list
=
list_move
(
list
);
assert
(
list
->
data
==
11
);
assert
(
list
->
next
->
data
==
12
);
assert
(
list
->
next
->
next
->
data
==
11
);
print_list
(
list
);
list
=
list_move
(
list
);
assert
(
list
->
data
==
12
);
assert
(
list
->
next
->
data
==
11
);
assert
(
list
->
next
->
next
->
data
==
12
);
print_list
(
list
);
// list_free
list_free
(
&
list
);
assert
(
list
==
NULL
);
}
This diff is collapsed.
Click to expand it.
joseph.c
+
4
−
11
View file @
0cba4092
...
...
@@ -9,20 +9,13 @@ element *add_number_to_circular_list(element *list, int number) {
}
element
*
remove_kth_element
(
element
*
head
,
int
k
)
{
if
(
head
->
next
==
head
)
{
printf
(
"%d
\n
"
,
head
->
next
->
data
);
free
(
head
);
return
NULL
;
}
for
(
int32_t
i
=
0
;
i
<
k
-
1
;
i
+=
1
)
{
for
(
int32_t
i
=
0
;
i
<
k
;
i
+=
1
)
{
head
=
list_move
(
head
);
}
printf
(
"%d
\n
"
,
head
->
next
->
data
);
element
*
tmp
=
head
->
next
;
head
->
next
=
head
->
next
->
next
;
free
(
tmp
);
element
*
removed_element
=
list_remove
(
&
head
,
head
->
data
);
printf
(
"%d
\n
"
,
removed_element
->
data
);
free
(
removed_element
);
return
head
;
}
...
...
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