summaryrefslogtreecommitdiff
path: root/rushs/evalexpr/fifo
diff options
context:
space:
mode:
Diffstat (limited to 'rushs/evalexpr/fifo')
-rw-r--r--rushs/evalexpr/fifo/Makefile16
-rw-r--r--rushs/evalexpr/fifo/fifo.h28
-rw-r--r--rushs/evalexpr/fifo/fifo_access.c66
-rw-r--r--rushs/evalexpr/fifo/fifo_setup_destroy.c39
4 files changed, 149 insertions, 0 deletions
diff --git a/rushs/evalexpr/fifo/Makefile b/rushs/evalexpr/fifo/Makefile
new file mode 100644
index 0000000..e5c9374
--- /dev/null
+++ b/rushs/evalexpr/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/evalexpr/fifo/fifo.h b/rushs/evalexpr/fifo/fifo.h
new file mode 100644
index 0000000..c4b0a6f
--- /dev/null
+++ b/rushs/evalexpr/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/evalexpr/fifo/fifo_access.c b/rushs/evalexpr/fifo/fifo_access.c
new file mode 100644
index 0000000..5d31586
--- /dev/null
+++ b/rushs/evalexpr/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/evalexpr/fifo/fifo_setup_destroy.c b/rushs/evalexpr/fifo/fifo_setup_destroy.c
new file mode 100644
index 0000000..80820e1
--- /dev/null
+++ b/rushs/evalexpr/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);
+}