diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/tinyprintf/fifo | |
add: graphs et rushs
Diffstat (limited to 'rushs/tinyprintf/fifo')
| -rw-r--r-- | rushs/tinyprintf/fifo/Makefile | 16 | ||||
| -rw-r--r-- | rushs/tinyprintf/fifo/fifo.h | 28 | ||||
| -rw-r--r-- | rushs/tinyprintf/fifo/fifo_access.c | 66 | ||||
| -rw-r--r-- | rushs/tinyprintf/fifo/fifo_setup_destroy.c | 39 |
4 files changed, 149 insertions, 0 deletions
diff --git a/rushs/tinyprintf/fifo/Makefile b/rushs/tinyprintf/fifo/Makefile new file mode 100644 index 0000000..e5c9374 --- /dev/null +++ b/rushs/tinyprintf/fifo/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-std=c99 -Wall -Wextra -Wvla -Werror -pedantic + +.PHONY: library clean + +library: fifo_access.o fifo_setup_destroy.o + ar csr libfifo.a $^ + +fifo_access.o: fifo_access.c + $(CC) $(CFLAGS) -c -o fifo_access.o fifo_access.c + +fifo_setup_destroy.o: fifo_setup_destroy.c + $(CC) $(CFLAGS) -c -o fifo_setup_destroy.o fifo_setup_destroy.c + +clean: + rm *.o libfifo.a diff --git a/rushs/tinyprintf/fifo/fifo.h b/rushs/tinyprintf/fifo/fifo.h new file mode 100644 index 0000000..c4b0a6f --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo.h @@ -0,0 +1,28 @@ +#ifndef FIFO_H +#define FIFO_H + +#include <stddef.h> + +struct list +{ + int data; + struct list *next; +}; + +struct fifo +{ + struct list *head; + struct list *tail; + size_t size; +}; + +struct fifo *fifo_init(void); +size_t fifo_size(struct fifo *fifo); +void fifo_push(struct fifo *fifo, int elt); +int fifo_head(struct fifo *fifo); +void fifo_pop(struct fifo *fifo); +void fifo_clear(struct fifo *fifo); +void fifo_destroy(struct fifo *fifo); +void fifo_print(const struct fifo *fifo); + +#endif /* !FIFO_H */ diff --git a/rushs/tinyprintf/fifo/fifo_access.c b/rushs/tinyprintf/fifo/fifo_access.c new file mode 100644 index 0000000..5d31586 --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo_access.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "fifo.h" + +size_t fifo_size(struct fifo *fifo) +{ + return fifo->size; +} + +void fifo_push(struct fifo *fifo, int elt) +{ + struct list *new = malloc(sizeof(struct list)); + if (new == NULL) + { + return; + } + new->data = elt; + + if (fifo_size(fifo) == 0) + { + fifo->head = new; + } + new->next = NULL; + if (fifo_size(fifo) != 0) + { + fifo->tail->next = new; + } + fifo->tail = new; + + fifo->size++; +} + +int fifo_head(struct fifo *fifo) +{ + return fifo->head->data; +} + +void fifo_pop(struct fifo *fifo) +{ + if (fifo_size(fifo) == 0) + { + return; + } + if (fifo_size(fifo) == 1) + { + free(fifo->head); + fifo->head = NULL; + fifo->tail = NULL; + return; + } + + struct list *tmp = fifo->head->next; + free(fifo->head); + fifo->head = tmp; + + fifo->size--; +} + +void fifo_print(const struct fifo *fifo) +{ + for (struct list *l = fifo->head; l; l = l->next) + { + printf("%d\n", l->data); + } +} diff --git a/rushs/tinyprintf/fifo/fifo_setup_destroy.c b/rushs/tinyprintf/fifo/fifo_setup_destroy.c new file mode 100644 index 0000000..80820e1 --- /dev/null +++ b/rushs/tinyprintf/fifo/fifo_setup_destroy.c @@ -0,0 +1,39 @@ +#include <stdlib.h> + +#include "fifo.h" + +struct fifo *fifo_init(void) +{ + struct fifo *f = malloc(sizeof(struct fifo)); + if (f == NULL) + { + return NULL; + } + + f->size = 0; + f->head = NULL; + f->tail = NULL; + + return f; +} + +void fifo_clear(struct fifo *fifo) +{ + for (struct list *l = fifo->head; l != fifo->tail;) + { + struct list *tmp = l->next; + free(l); + l = tmp; + } + free(fifo->tail); + + fifo->head = NULL; + fifo->tail = NULL; + fifo->size = 0; +} + +void fifo_destroy(struct fifo *fifo) +{ + fifo_clear(fifo); + free(fifo); +} |
