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
aa10b067
Commit
aa10b067
authored
3 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Validation ex3
parent
e76caac0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ex3/ex3.c
+93
-13
93 additions, 13 deletions
ex3/ex3.c
with
93 additions
and
13 deletions
ex3/ex3.c
+
93
−
13
View file @
aa10b067
...
...
@@ -9,23 +9,35 @@
*
*/
#include
<math.h>
#include
<stdbool.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
// LinkedListNode.h
typedef
struct
LinkedListNode
{
struct
LinkedListNode
*
next
;
int32_t
value
;
}
LinkedListNode
;
/**
* @brief Creation of a new node.
*
* @param value
* @return LinkedListNode*
*/
LinkedListNode
*
linked_list_node_new
(
int32_t
value
);
/**
* @brief Deletion of a node.
*
* @param node
*/
void
linked_list_node_delete
(
LinkedListNode
**
node
);
// LinkedListNode.c
LinkedListNode
*
linked_list_node_new
(
int32_t
value
)
{
LinkedListNode
*
node
=
(
LinkedListNode
*
)
malloc
(
sizeof
(
LinkedListNode
));
...
...
@@ -41,20 +53,78 @@ void linked_list_node_delete(LinkedListNode **node) {
}
// LinkedList.h
typedef
struct
{
LinkedListNode
*
root
;
}
LinkedList
;
/**
* @brief Creation of a new linked list.
*
* @return LinkedList*
*/
LinkedList
*
linked_list_new
();
/**
* @brief Deletion of the linked list.
*
* @param list
*/
void
linked_list_delete
(
LinkedList
**
list
);
/**
* @brief Count the number of nodes in the list.
*
* @param list
* @return int32_t
*/
int32_t
linked_list_count
(
LinkedList
*
list
);
/**
* @brief Checks if the list is empty or not.
*
* @param list
* @return true
* @return false
*/
bool
linked_list_is_empty
(
LinkedList
*
list
);
/**
* @brief Adds a value at the end of the list.
*
* @param list
* @param value
* @return LinkedListNode*
*/
LinkedListNode
*
linked_list_add_last
(
LinkedList
*
list
,
int32_t
value
);
/**
* @brief Displays the list.
*
* @param list
*/
void
linked_list_print
(
LinkedList
*
list
);
/**
* @brief Inserts a value at the desired index, if the index does not exist does nothing.
*
* @param list
* @param index
* @param value
*/
void
linked_list_insert_at_index
(
LinkedList
*
list
,
int32_t
index
,
int32_t
value
);
/**
* @brief Retrieves the value at the specified index.
*
* @param list
* @param index
* @return int32_t
*/
int32_t
linked_list_peek_at_index
(
LinkedList
*
list
,
int32_t
index
);
// LinkedList.c
LinkedList
*
linked_list_new
()
{
LinkedList
*
list
=
(
LinkedList
*
)
malloc
(
sizeof
(
LinkedList
));
...
...
@@ -156,38 +226,48 @@ int32_t linked_list_peek_at_index(LinkedList *list, int32_t index) {
return
current_node
->
value
;
}
LinkedList
*
compute_pascal_triangle_row
(
int32_t
row_number
)
{
/**
* @brief Compute the desired line of Pascal's triangles.
*
* @param line_number
* @return LinkedList*
*/
LinkedList
*
compute_pascal_triangle_line
(
int32_t
line_number
)
{
LinkedList
*
list
=
linked_list_new
();
// Always 1 at the beginning.
linked_list_add_last
(
list
,
1
);
if
(
row
_number
==
1
)
{
if
(
line
_number
==
1
)
{
return
list
;
}
// Always 1 at the end.
linked_list_add_last
(
list
,
1
);
if
(
row
_number
==
2
)
{
if
(
line
_number
==
2
)
{
return
list
;
}
LinkedList
*
previous_row
=
compute_pascal_triangle_row
(
row_number
-
1
);
// Retrieves the previous line to calculate the new line.
LinkedList
*
previous_line
=
compute_pascal_triangle_line
(
line_number
-
1
);
for
(
int32_t
i
=
0
;
i
<
row_number
-
2
;
i
+=
1
)
{
int32_t
value
=
linked_list_peek_at_index
(
previous_row
,
i
+
1
);
value
+=
linked_list_peek_at_index
(
previous_row
,
i
);
// Calculates the values inside the line (ignores the first and last value).
for
(
int32_t
i
=
0
;
i
<
line_number
-
2
;
i
+=
1
)
{
int32_t
value
=
linked_list_peek_at_index
(
previous_line
,
i
+
1
);
value
+=
linked_list_peek_at_index
(
previous_line
,
i
);
linked_list_insert_at_index
(
list
,
i
+
1
,
value
);
}
linked_list_delete
(
&
previous_
row
);
linked_list_delete
(
&
previous_
line
);
return
list
;
}
int
main
()
{
int32_t
row
_number
;
scanf
(
"%d"
,
&
row
_number
);
int32_t
line
_number
;
scanf
(
"%d"
,
&
line
_number
);
printf
(
"
\n
"
);
LinkedList
*
list
=
compute_pascal_triangle_
row
(
row
_number
);
LinkedList
*
list
=
compute_pascal_triangle_
line
(
line
_number
);
linked_list_print
(
list
);
linked_list_delete
(
&
list
);
...
...
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