Skip to content
Snippets Groups Projects
Commit 229c6f29 authored by root's avatar root
Browse files

ex3

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