Select Git revision
-
florian.burgener authoredflorian.burgener authored
ex2.c 5.00 KiB
/**
* @file ex2.c
* @author Florian Burgener
* @brief Exercice 2
* @version 1.0
* @date 2022-01-25
*
* @copyright Copyright (c) 2021
*
*/
#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;
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, ¤t_node);
}
int main() {
int32_t values_length = 4;
int32_t values[values_length];
for (int32_t i = 0; i < values_length; i += 1) {
int32_t value;
scanf("%d", &value);
values[i] = value;
}
int32_t insert_index;
scanf("%d", &insert_index);
int32_t remove_index;
scanf("%d", &remove_index);
printf("\n");
LinkedList *list = linked_list_new();
for (int32_t i = 4 - 1; i >= 0; i -= 1) {
linked_list_add_last(list, values[i]);
}
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;
}