init
This commit is contained in:
commit
15934c4708
8 changed files with 157 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
l1
|
||||
.ccls-cache/
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# aisd_lab
|
||||
|
||||
this repo contains my solutions to algorithm and data structures labs exercises
|
21
lab01/zad1/.ccls
Normal file
21
lab01/zad1/.ccls
Normal file
|
@ -0,0 +1,21 @@
|
|||
gcc
|
||||
%c -std=c99
|
||||
%h
|
||||
-Iinclude
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wstrict-aliasing
|
||||
-Wfloat-equal
|
||||
-Wundef
|
||||
-Wshadow
|
||||
-Wpointer-arith
|
||||
-Wcast-align
|
||||
-Wstrict-prototypes
|
||||
-Wstrict-overflow=5
|
||||
-Wwrite-strings
|
||||
-Wcast-qual
|
||||
-Wswitch-default
|
||||
-Wswitch-enum
|
||||
-Wconversion
|
||||
-Wunreachable-code
|
3
lab01/zad1/.gitignore
vendored
Normal file
3
lab01/zad1/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
target/
|
||||
.ccls-cache/
|
||||
.direnv/
|
35
lab01/zad1/Makefile
Normal file
35
lab01/zad1/Makefile
Normal file
|
@ -0,0 +1,35 @@
|
|||
CC = gcc
|
||||
CFLAGS = -std=c99 -O3 -Wall -Wextra -Wpedantic -Wstrict-aliasing
|
||||
CFLAGS += -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align
|
||||
CFLAGS += -Wstrict-prototypes -Wstrict-overflow=5 -Wwrite-strings
|
||||
CFLAGS += -Wcast-qual -Wswitch-default -Wswitch-enum
|
||||
CFLAGS += -Wconversion -Wunreachable-code
|
||||
CFLAGS += -Iinclude
|
||||
LDFLAGS =
|
||||
|
||||
NAME = zad1
|
||||
|
||||
SRC = src
|
||||
BIN = target
|
||||
|
||||
_PROG = main.c queue.c
|
||||
PROG = $(addprefix $(SRC)/, $(_PROG))
|
||||
|
||||
OBJ = $(_PROG:.c=.o)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
all: dirs $(NAME)
|
||||
|
||||
dirs:
|
||||
mkdir -p $(BIN)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
$(CC) $(addprefix $(BIN)/, $^) $(LDFLAGS) -o $(BIN)/$@
|
||||
|
||||
%.o: src/%.c
|
||||
$(CC) -c $< $(CFLAGS) -o $(BIN)/$@
|
||||
|
||||
clean:
|
||||
rm -rf $(addprefix $(BIN)/, $(OBJ))
|
||||
rm -rf $(BIN)/$(NAME)
|
15
lab01/zad1/include/queue.h
Normal file
15
lab01/zad1/include/queue.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef _KIK_ZAD1_QUEUE_H
|
||||
#define _KIK_ZAD1_QUEUE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct Queue Queue;
|
||||
|
||||
Queue *queue_new(size_t size);
|
||||
|
||||
void *queue_read(Queue *queue);
|
||||
|
||||
bool queue_write(Queue *queue, void *value);
|
||||
|
||||
#endif // _KIK_ZAD1_QUEUE_H
|
34
lab01/zad1/src/main.c
Normal file
34
lab01/zad1/src/main.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <queue.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
Queue *q = queue_new(51);
|
||||
|
||||
for (size_t i = 0; i < 50; ++i) {
|
||||
long val = rand() % 1000;
|
||||
printf("%zu: writing %ld to queue...\n", i, val);
|
||||
if (queue_write(q, &val)) {
|
||||
printf("%zu: successfully wrote %ld to fifo!\n", i, val);
|
||||
} else {
|
||||
printf("%zu: failed writing %ld to fifo :-(\n", i, val);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 50; ++i) {
|
||||
void *val_ptr = queue_read(q);
|
||||
if (val_ptr == NULL) {
|
||||
printf("%zu: fifo is empty!\n", i);
|
||||
break;
|
||||
}
|
||||
|
||||
long val = *(long*)val_ptr;
|
||||
printf("%zu: read %ld from fifo\n", i, val);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
44
lab01/zad1/src/queue.c
Normal file
44
lab01/zad1/src/queue.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <queue.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Queue {
|
||||
size_t back;
|
||||
size_t front;
|
||||
size_t size;
|
||||
void **data;
|
||||
};
|
||||
|
||||
Queue *queue_new(size_t size) {
|
||||
Queue *ret = malloc(sizeof(Queue));
|
||||
ret->back = 0;
|
||||
ret->front = 0;
|
||||
ret->size = size;
|
||||
ret->data = malloc(size * sizeof(void*));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *queue_read(Queue *q) {
|
||||
if (q->front == q->back) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* ret = q->data[q->front];
|
||||
q->data[q->front] = NULL;
|
||||
|
||||
q->front = (q->front + 1) % q->size;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool queue_write(Queue *q, void *value) {
|
||||
if (((q->back + 1) % q->size) == q->front) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q->data[q->back] = value;
|
||||
q->back = (q->back + 1) % q->size;
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Reference in a new issue