Select Git revision
hyperdrive.sql
Forked from
Développement Web Avancé / 2019_TP2
Source project has a limited visibility.
ex2.c 1.83 KiB
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define INSERT_VALUE 0
typedef struct _link {
int value;
struct _link *next;
} elem;
typedef struct _list {
elem *head;
} list;
list list_create() {
list ll;
ll.head = NULL;
return ll;
}
void list_destroy(list* ll) {
elem *e = ll->head;
while (ll->head != NULL) {
ll->head = ll->head->next;
free(e);
e = ll->head;
}
}
void list_push(list* ll, const int v) {
elem *e = malloc(sizeof(elem));
e->value = v;
e->next = ll->head;
ll->head = e;
}
void list_insert(list* ll, const int n, const int v) {
if (n < 0) { return; }
elem *e = malloc(sizeof(elem));
e->value = v;
e->next = NULL;
if (0 == n) {
e->next = ll->head;
ll->head = e;
} else {
int count = 1;
elem *prev = ll->head;
while (count < n) {
if (NULL == prev->next) { return; }
prev = prev->next;
++count;
}
elem *follow = prev->next;
e->next = follow;
prev->next = e;
}
}
void list_remove(list *ll, const int n) {
if (n < 0) { return; }
if (0 == n) {
elem *e = ll->head;
ll->head = ll->head->next;
free(e);
} else {
int count = 1;
elem *prev = ll->head;
elem *e = prev->next;
while (count < n) {
if (NULL == e) { return; }
prev = e;
e = e->next;
++count;
}
if (e != NULL) { prev->next = e->next; }
else { prev->next = NULL; }
free(e);
}
}
void list_print(const list *ll) {
elem *e = ll->head;
while (e != NULL) {
printf("%d", e->value);
if (e->next != NULL) { printf(" "); }
e = e->next;
}
printf("\n");
}
int main() {
int n;
list ll = list_create();
for (int i = 0; i < 4; ++i) {
scanf("%d", &n);
list_push(&ll, n);
}
int i, r;
scanf("%d", &i);
scanf("%d", &r);
printf("\n");
list_insert(&ll, i, INSERT_VALUE);
list_print(&ll);
list_remove(&ll, r);
list_print(&ll);
list_destroy(&ll);
return EXIT_SUCCESS;
}