prog_avancee_C/TP1/part_3.c
2024-10-22 13:03:17 +02:00

71 lines
1.3 KiB
C

#include "part_3.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
Liste * create_list() {
Liste * list = malloc(sizeof(Liste));
list->nb_elements_ = 0;
return list;
}
int list_length(Liste list) {
return list.nb_elements_;
}
void append(Liste * list, int val) {
Element * el = malloc(sizeof(Element));
el->val_ = val;
el->prev_ = list->tail_;
if (list->tail_ != NULL) {
list->tail_->next_ = el;
}
list->tail_ = el;
list->nb_elements_ += 1;
if (list->nb_elements_==1) {
list->head_ = el;
}
}
void append_head(Liste * list, int val) {
Element * el = malloc(sizeof(Element));
list->head_->prev_ = el;
list->head_ = el;
list->nb_elements_ += 1;
}
int first_value(Liste * list) {
return list->head_->val_;
}
int last_value(Liste * list) {
return list->tail_->val_;
}
void print_list(Liste * list) {
if (list->head_ == NULL) {
printf("[]\n");
return;
}
Element* current_el = list->head_;
printf("[");
printf("%lu", (unsigned long)current_el);
while (current_el != NULL) {
printf("%d, ", current_el->val_);
current_el = current_el->next_;
}
printf("]\n");
}
int main(int argc, char *argv[])
{
Liste * list = create_list();
append(list, 69);
print_list(list);
return 0;
}