Skip to content
Snippets Groups Projects
Commit aa10b067 authored by florian.burgener's avatar florian.burgener
Browse files

Validation ex3

parent e76caac0
Branches
No related tags found
No related merge requests found
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment