From bf9914308497839c1d05905f6f156ee4165fdae1 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Mon, 19 Apr 2021 10:18:30 +0200 Subject: Linked list finally working --- Semestr 4/sieci/pracownia2/router/config.h | 2 +- Semestr 4/sieci/pracownia2/router/linked_list.c | 25 +++++++++++++++++++++---- Semestr 4/sieci/pracownia2/router/linked_list.h | 4 ++++ Semestr 4/sieci/pracownia2/router/makefile | 9 +++++++++ Semestr 4/sieci/pracownia2/router/test.c | 9 ++++++++- 5 files changed, 43 insertions(+), 6 deletions(-) (limited to 'Semestr 4/sieci/pracownia2/router') diff --git a/Semestr 4/sieci/pracownia2/router/config.h b/Semestr 4/sieci/pracownia2/router/config.h index abf7d23..3537c03 100644 --- a/Semestr 4/sieci/pracownia2/router/config.h +++ b/Semestr 4/sieci/pracownia2/router/config.h @@ -2,7 +2,7 @@ #define CONFIG_H #define SERVER_PORT 54321 -#define TURN_LEN_S 20 +#define TURN_LEN_S 5 #define TURN_LEN_MS (1000 * TURN_LEN_S) #define TURN_LEN_US (1000000 * TURN_LEN_S) diff --git a/Semestr 4/sieci/pracownia2/router/linked_list.c b/Semestr 4/sieci/pracownia2/router/linked_list.c index b6b5225..f3bccc9 100644 --- a/Semestr 4/sieci/pracownia2/router/linked_list.c +++ b/Semestr 4/sieci/pracownia2/router/linked_list.c @@ -16,18 +16,21 @@ void _insert(node_t **head, void *data, size_t data_size) { *head = new_node; } +void _free_node(node_t *node) { + free(node->data); + free(node); +} + void _erase(node_t **head) { node_t *next_node = _next(*head); - free((*head)->data); - free(*head); + _free_node(*head); *head = next_node; } void _free_list(node_t *head) { if (head == NULL) return; _free_list(head->next); - free(head->data); - free(head); + _free_node(head); } list_t create_list() { @@ -45,11 +48,25 @@ void erase(list_t *list) { _erase(&list->head); } +void erase_it(list_t *list) { + if(list->it == NULL) return; + if(list->prev_it == NULL) { + erase(list); + reset(list); + return; + } + list->prev_it->next = _next(list->it); + _free_node(list->it); + list->it = list->prev_it->next; +} + void iterate(list_t *list) { + list->prev_it = list->it; list->it = _next(list->it); } void reset(list_t *list) { + list->prev_it = NULL; list->it = list->head; } diff --git a/Semestr 4/sieci/pracownia2/router/linked_list.h b/Semestr 4/sieci/pracownia2/router/linked_list.h index f662dbe..28e8ef7 100644 --- a/Semestr 4/sieci/pracownia2/router/linked_list.h +++ b/Semestr 4/sieci/pracownia2/router/linked_list.h @@ -12,6 +12,7 @@ typedef struct node { typedef struct list_t { node_t *head; node_t *it; + node_t *prev_it; } list_t; /* Creates an empty list */ @@ -23,6 +24,9 @@ void insert(list_t *list, void *data, size_t data_size); /* Erases first node from the list. */ void erase(list_t *list); +/* Erases element under iterator and sets iterator to the next one. */ +void erase_it(list_t *list); + /* Moves iterator one step. */ void iterate(list_t *list); diff --git a/Semestr 4/sieci/pracownia2/router/makefile b/Semestr 4/sieci/pracownia2/router/makefile index 7d8aaef..87bc15a 100644 --- a/Semestr 4/sieci/pracownia2/router/makefile +++ b/Semestr 4/sieci/pracownia2/router/makefile @@ -1,13 +1,19 @@ CC := gcc CFLAGS := -Og -std=gnu17 -Wall -Wall -fsanitize=address -fsanitize=undefined TARGET := router +TEST := test DEPS := config.h ODIR := obj _OBJ := router.o utils.o linked_list.o network_addr.o dist_vector.o OBJ := $(patsubst %,$(ODIR)/%,$(_OBJ)) +_TEST_OBJ := test.o linked_list.o +TEST_OBJ := $(patsubst %,$(ODIR)/%,$(_TEST_OBJ)) + + all: $(TARGET) +test: $(TEST) $(ODIR)/%.o: %.c $(DEPS) $(CC) $(CFLAGS) -c -o $@ $< @@ -15,6 +21,9 @@ $(ODIR)/%.o: %.c $(DEPS) $(TARGET): $(OBJ) $(CC) -o $@ $^ $(CFLAGS) +$(TEST): $(TEST_OBJ) + $(CC) -o $@ $^ $(CFLAGS) + clean: rm -rf $(TARGET) rm -rf test diff --git a/Semestr 4/sieci/pracownia2/router/test.c b/Semestr 4/sieci/pracownia2/router/test.c index ebc6df1..e086e55 100644 --- a/Semestr 4/sieci/pracownia2/router/test.c +++ b/Semestr 4/sieci/pracownia2/router/test.c @@ -27,11 +27,18 @@ int main() { int val = 0; scanf("%d", &val); insert(&list, &val, sizeof(int)); + reset(&list); } if (t == 1) { - erase(&list); + iterate(&list); + if (list.it != NULL) + printf("it: %d\n", *(int *)list.it->data); + else printf("End of list.\n"); } if (t == 2) { + erase_it(&list); + } + if (t == 3) { print_list(list); } } -- cgit v1.2.3