Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
controle-c-002
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
controle-c-002
Commits
4925e2f4
Commit
4925e2f4
authored
3 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Exercice 1
parent
460fae1a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex2/ex2.c
+193
-25
193 additions, 25 deletions
ex2/ex2.c
with
193 additions
and
25 deletions
ex2/ex2.c
+
193
−
25
View file @
4925e2f4
...
@@ -16,30 +16,198 @@
...
@@ -16,30 +16,198 @@
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
// LinkedListNode.h
typedef
struct
LinkedListNode
{
struct
LinkedListNode
*
next
;
int32_t
value
;
}
LinkedListNode
;
LinkedListNode
*
linked_list_node_new
(
int32_t
value
);
void
linked_list_node_delete
(
LinkedListNode
**
node
);
// LinkedListNode.c
LinkedListNode
*
linked_list_node_new
(
int32_t
value
)
{
LinkedListNode
*
node
=
(
LinkedListNode
*
)
malloc
(
sizeof
(
LinkedListNode
));
node
->
next
=
NULL
;
node
->
value
=
value
;
return
node
;
}
void
linked_list_node_delete
(
LinkedListNode
**
node
)
{
free
(
*
node
);
*
node
=
NULL
;
}
// LinkedList.h
typedef
struct
{
LinkedListNode
*
root
;
}
LinkedList
;
LinkedList
*
linked_list_new
();
void
linked_list_delete
(
LinkedList
**
list
);
int32_t
linked_list_count
(
LinkedList
*
list
);
bool
linked_list_is_empty
(
LinkedList
*
list
);
LinkedListNode
*
linked_list_first
(
LinkedList
*
list
);
LinkedListNode
*
linked_list_last
(
LinkedList
*
list
);
LinkedListNode
*
linked_list_add_last
(
LinkedList
*
list
,
int32_t
value
);
void
linked_list_remove
(
LinkedList
*
list
,
LinkedListNode
**
node
);
void
linked_list_print
(
LinkedList
*
list
);
// LinkedList.c
LinkedList
*
linked_list_new
()
{
LinkedList
*
list
=
(
LinkedList
*
)
malloc
(
sizeof
(
LinkedList
));
list
->
root
=
NULL
;
return
list
;
}
void
linked_list_delete
(
LinkedList
**
list
)
{
LinkedListNode
*
current_node
=
(
*
list
)
->
root
;
while
(
current_node
!=
NULL
)
{
LinkedListNode
*
current_node_copy
=
current_node
;
current_node
=
current_node
->
next
;
free
(
current_node_copy
);
}
free
(
*
list
);
*
list
=
NULL
;
}
int32_t
linked_list_count
(
LinkedList
*
list
)
{
LinkedListNode
*
current_node
=
list
->
root
;
int32_t
count
=
0
;
while
(
current_node
!=
NULL
)
{
count
+=
1
;
current_node
=
current_node
->
next
;
}
return
count
;
}
bool
linked_list_is_empty
(
LinkedList
*
list
)
{
return
linked_list_count
(
list
)
==
0
;
}
LinkedListNode
*
linked_list_add_last
(
LinkedList
*
list
,
int32_t
value
)
{
LinkedListNode
*
new_node
=
linked_list_node_new
(
value
);
if
(
linked_list_is_empty
(
list
))
{
list
->
root
=
new_node
;
}
else
{
LinkedListNode
*
current_node
=
list
->
root
;
while
(
current_node
->
next
!=
NULL
)
{
current_node
=
current_node
->
next
;
}
current_node
->
next
=
new_node
;
}
return
new_node
;
}
void
linked_list_remove
(
LinkedList
*
list
,
LinkedListNode
**
node
)
{
if
(
list
->
root
==
(
*
node
))
{
list
->
root
=
(
*
node
)
->
next
;
}
else
{
LinkedListNode
*
current_node
=
list
->
root
;
while
(
current_node
->
next
!=
(
*
node
))
{
current_node
=
current_node
->
next
;
}
current_node
->
next
=
current_node
->
next
->
next
;
}
free
(
*
node
);
*
node
=
NULL
;
}
void
linked_list_print
(
LinkedList
*
list
)
{
LinkedListNode
*
current_node
=
list
->
root
;
while
(
current_node
!=
NULL
)
{
printf
(
"%d "
,
current_node
->
value
);
current_node
=
current_node
->
next
;
}
printf
(
"
\n
"
);
}
void
linked_list_insert_at_index
(
LinkedList
*
list
,
int32_t
index
,
int32_t
value
)
{
if
(
index
==
0
)
{
LinkedListNode
*
new_node
=
linked_list_node_new
(
value
);
new_node
->
next
=
list
->
root
;
list
->
root
=
new_node
;
return
;
}
LinkedListNode
*
current_node
=
list
->
root
;
while
(
current_node
!=
NULL
&&
index
!=
1
)
{
current_node
=
current_node
->
next
;
index
-=
1
;
}
if
(
current_node
==
NULL
)
{
return
;
}
LinkedListNode
*
new_node
=
linked_list_node_new
(
value
);
new_node
->
next
=
current_node
->
next
;
current_node
->
next
=
new_node
;
}
void
linked_list_remove_at_index
(
LinkedList
*
list
,
int32_t
index
)
{
LinkedListNode
*
current_node
=
list
->
root
;
while
(
current_node
!=
NULL
&&
index
!=
0
)
{
current_node
=
current_node
->
next
;
index
-=
1
;
}
if
(
current_node
==
NULL
)
{
return
;
}
linked_list_remove
(
list
,
&
current_node
);
}
int
main
()
{
int
main
()
{
// int32_t values_length = 5;
int32_t
values_length
=
4
;
// double values[values_length];
int32_t
values
[
values_length
];
// for (int32_t i = 0; i < values_length; i += 1) {
for
(
int32_t
i
=
0
;
i
<
values_length
;
i
+=
1
)
{
// double value;
int32_t
value
;
// scanf("%lf", &value);
scanf
(
"%d"
,
&
value
);
// values[i] = value;
values
[
i
]
=
value
;
// }
}
// int32_t values_length = 5;
int32_t
insert_index
;
// int32_t values[values_length];
scanf
(
"%d"
,
&
insert_index
);
// for (int32_t i = 0; i < values_length; i += 1) {
int32_t
remove_index
;
// int32_t value;
scanf
(
"%d"
,
&
remove_index
);
// scanf("%d", &value);
// values[i] = value;
printf
(
"
\n
"
);
// }
LinkedList
*
list
=
linked_list_new
();
// char a[100];
// int32_t b;
for
(
int32_t
i
=
4
-
1
;
i
>=
0
;
i
-=
1
)
{
// scanf("%s %d", a, &b);
linked_list_add_last
(
list
,
values
[
i
]);
// printf("%s %d\n", a, b);
}
printf
(
"ex2
\n
"
);
linked_list_insert_at_index
(
list
,
insert_index
,
0
);
linked_list_print
(
list
);
linked_list_remove_at_index
(
list
,
remove_index
);
linked_list_print
(
list
);
linked_list_delete
(
&
list
);
return
EXIT_SUCCESS
;
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