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

Exercice 1

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