zad2 init with merge test
This commit is contained in:
parent
d452a7c5b0
commit
4245731593
6 changed files with 186 additions and 0 deletions
21
lab01/zad2/.ccls
Normal file
21
lab01/zad2/.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/zad2/.gitignore
vendored
Normal file
3
lab01/zad2/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
target/
|
||||
.ccls-cache/
|
||||
.direnv/
|
35
lab01/zad2/Makefile
Normal file
35
lab01/zad2/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 -g
|
||||
LDFLAGS =
|
||||
|
||||
NAME = zad2
|
||||
|
||||
SRC = src
|
||||
BIN = target
|
||||
|
||||
_PROG = main.c linked_list.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)
|
24
lab01/zad2/include/linked_list.h
Normal file
24
lab01/zad2/include/linked_list.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#ifndef _KIK_ZAD2_LINKED_LIST_H
|
||||
#define _KIK_ZAD2_LINKED_LIST_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct Node {
|
||||
void *value;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
size_t element_size;
|
||||
Node *head;
|
||||
Node *current;
|
||||
} LinkedList;
|
||||
|
||||
LinkedList linked_list_new(size_t element_size);
|
||||
|
||||
void linked_list_insert(LinkedList *l, void *value);
|
||||
|
||||
LinkedList linked_list_merge(LinkedList l1, LinkedList l2);
|
||||
|
||||
#endif // _KIK_ZAD2_LINKED_LIST_H
|
62
lab01/zad2/src/linked_list.c
Normal file
62
lab01/zad2/src/linked_list.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
#include <linked_list.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Node *node_new(void *value, size_t element_size) {
|
||||
Node *new = (Node*)malloc(sizeof(Node));
|
||||
new->value = memcpy(malloc(element_size), value, sizeof(element_size));
|
||||
new->next = NULL;
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
LinkedList linked_list_new(size_t element_size) {
|
||||
LinkedList ret;
|
||||
ret.length = 0;
|
||||
ret.element_size = element_size;
|
||||
ret.head = NULL;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void linked_list_insert(LinkedList *l, void *value) {
|
||||
if (l->head == NULL) {
|
||||
l->head = node_new(value, l->element_size);
|
||||
l->head->next = l->head;
|
||||
l->length += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
Node *current = l->head;
|
||||
while (current->next != l->head) {
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
current->next = node_new(value, l->element_size);
|
||||
current->next->next = l->head;
|
||||
l->length += 1;
|
||||
}
|
||||
|
||||
LinkedList linked_list_merge(LinkedList l1, LinkedList l2) {
|
||||
if (l1.head == NULL) return l2;
|
||||
if (l2.head == NULL) return l1;
|
||||
|
||||
Node *l1_tail = l1.head;
|
||||
while (l1_tail->next != l1.head) {
|
||||
l1_tail = l1_tail->next;
|
||||
}
|
||||
|
||||
l1_tail->next = l2.head;
|
||||
|
||||
Node *l2_tail = l2.head;
|
||||
while (l2_tail->next != l2.head) {
|
||||
l2_tail = l2_tail->next;
|
||||
}
|
||||
|
||||
l2_tail->next = l1.head;
|
||||
|
||||
l1.length += l2.length;
|
||||
|
||||
return l1;
|
||||
}
|
41
lab01/zad2/src/main.c
Normal file
41
lab01/zad2/src/main.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <linked_list.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void merge_test(void) {
|
||||
LinkedList l1 = linked_list_new(sizeof(uint64_t));
|
||||
for (size_t i = 0; i < 10; ++i) {
|
||||
uint64_t val = (uint64_t)rand() % 1000;
|
||||
printf("%zu: inserting %ld into l1\n", i, val);
|
||||
linked_list_insert(&l1, &val);
|
||||
}
|
||||
LinkedList l2 = linked_list_new(sizeof(uint64_t));
|
||||
for (size_t i = 0; i < 10; ++i) {
|
||||
uint64_t val = (uint64_t)rand() % 1000;
|
||||
printf("%zu: inserting %ld into l2\n", i, val);
|
||||
linked_list_insert(&l2, &val);
|
||||
}
|
||||
|
||||
printf("merging l1 and l2\n");
|
||||
LinkedList l = linked_list_merge(l1, l2);
|
||||
|
||||
Node *l_node = l.head;
|
||||
size_t i = 0;
|
||||
do {
|
||||
uint64_t val = *(uint64_t*)l_node->value;
|
||||
printf("%zu: %ld\n", i, val);
|
||||
l_node = l_node->next;
|
||||
i += 1;
|
||||
} while (l_node != l.head);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
merge_test();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue