aboutsummaryrefslogtreecommitdiff
path: root/sem1/algo/mm2/queue.c
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
committerJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
commit6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 (patch)
tree2be8fae8ce82d708ed9f00f376dda14420850e80 /sem1/algo/mm2/queue.c
parent57305119e05559c1c37e903aef89cd43f44c42c9 (diff)
Rename and cleanup
Diffstat (limited to 'sem1/algo/mm2/queue.c')
-rw-r--r--sem1/algo/mm2/queue.c100
1 files changed, 0 insertions, 100 deletions
diff --git a/sem1/algo/mm2/queue.c b/sem1/algo/mm2/queue.c
deleted file mode 100644
index a93f76a..0000000
--- a/sem1/algo/mm2/queue.c
+++ /dev/null
@@ -1,100 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#define EFULL 2
-#define EMPTY 3
-
-/* Queue stuff */
-typedef struct {
- int head;
- int tail;
- int len;
- int cap;
- int *buff;
-} queue_t;
-
-/* Queue functions */
-int queue_init(queue_t *q, size_t cap) {
- /* Make the struct and set i to zero */
- memset(q, 0, sizeof(queue_t));
-
- /* Allocate the buffer */
- q->buff = (int *) malloc(cap * sizeof(int));
- if( q->buff == NULL ) {
- return 1;
- }
-
- /* Set capacity, the rest should be zero form memset */
- q->cap = cap;
- return 0;
-}
-
-void queue_free(queue_t *q) {
- /* Free the heap buffer */
- free(q->buff);
-}
-
-int queue_place(queue_t *q, int val) {
- /* Check if full */
- printf("len: %d\n", q->len);
- if( q->len >= q->cap) {
- printf("ERR: Full\n");
- return EFULL;
- }
-
- /* Add to queue */
- q->buff[q->head] = val;
-
- /* Increase values */
- q->head = (q->head+1) % q->cap;
- q->len++;
-
- return 0;
-}
-
-int queue_get(queue_t *q, int *val) {
- /* Check if empty */
- if( !q->len ) {
- printf("ERR: Empty\n");
- return EMPTY;
- }
-
- /* Read value */
- if( val != NULL ){
- *val = q->buff[q->tail];
- }
-
- /* Decrease values */
- q->tail = (q->tail+1) % q->cap;
- q->len--;
-
- return 0;
-}
-
-int main(void) {
- int in;
- char com;
-
- queue_t q;
- queue_init(&q, 16);
-
- for(;;) {
- /* Read a command */
- scanf("%c", &com);
-
- if( com == 'w') {
- printf("> ");
- scanf("%d", &in);
- queue_place(&q, in);
- } else if( com == 'r' ) {
- queue_get(&q, &in);
- printf("%d\n", in);
- } else if( com == 'q' ) {
- break;
- }
- }
-
- queue_free(&q);
-
-}