diff --git a/ex3/main.c b/ex3/main.c index 6550e291ef06e7676d8dfb2bf08dd772777aedc7..50885ef06a86adafb99cb531d25985ce6fa568c8 100644 --- a/ex3/main.c +++ b/ex3/main.c @@ -1,7 +1,104 @@ #include <stdio.h> #include <stdlib.h> -int main(){ +typedef struct _element { + int data; + struct _element *next; +} element; + +void cl_push(element* head, element* new_elem, int data){ + element* current = head; + while (current->next != NULL){ + current = current->next; + } + + new_elem->data = data; + new_elem->next = NULL; + current->next = new_elem; +} + +void cl_insert_at(element* head, element* new_elem, int val, uint index){ + element* current = head; + + + for (uint i = 0; i < index; i++) + { + element* next = current->next; + if (next == NULL){ + return; + } + current = next; + } + + element* next = current->next; + current->next = new_elem; + new_elem->next = next; + new_elem->data = val; +} + +void cl_remove_at(element* head, uint index){ + element* current = head; + for (uint i = 0; i < index; i++) + { + element* next = current->next; + if (next == NULL){ + return; + } + current = next; + } + element* next = current->next; + element* n_next = next->next; + current->data = next->data; + current->next = n_next; +} + +int paskal(int row, int col){ + if (col == 0){ + return 1; + } else if (row == 0){ + return 0; + } + + return (paskal(row-1, col) + paskal(row-1, col-1)); +} + +void cl_paskal(element* head, int row){ + + element** list = (element**) malloc(row * sizeof(element*)); + for (int i = 0; i < row; i++){ + list[i] = (element*) malloc(row * sizeof(element)); + } + + for (int i = 1; i < row; i++) + { + cl_push(head, list[i], paskal(row, i)); + } + element lastElem; + cl_push(head, &lastElem, 1); + + element* index = head; + while (index != NULL){ + printf("%d ", index->data); + index = index->next; + } + printf("\n"); + + for (int i = 0; i < row; i++){ + free(list[i]); + } + free(list); +} + +int main(int argc, char const *argv[]) +{ + if (argc < 2 || argc >= 3){ + return -1; + } + int line = atoi(argv[1]); + element first = {.data = 1, .next = NULL}; + element* head = &first; + + cl_paskal(head, line); return 0; -} \ No newline at end of file +}