From 23592a98eddb8a4f7b1063bc7b86e96526e41957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 24 Sep 2019 08:20:09 +0200 Subject: [PATCH] Create linked list interface. --- src/llist.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/llist.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 1 + 3 files changed, 119 insertions(+) create mode 100644 src/llist.c create mode 100644 src/llist.h diff --git a/src/llist.c b/src/llist.c new file mode 100644 index 0000000..a7ac609 --- /dev/null +++ b/src/llist.c @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2019 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "llist.h" + +#include +#include + +void llist_init(struct llist *list) { + list->first = NULL; + list->last = NULL; + list->size = 0; +} + +void llist_deinit(struct llist *list) { + struct llist_item *aux = list->first; + while(aux) + { + mpz_clear(aux->n); + struct llist_item *tmp = aux; + aux = aux->next; + free(tmp); + } +} + +int llist_insert(struct llist *list, mpz_t n) { + struct llist_item *aux = + malloc(sizeof(struct llist_item)); + if(!aux) + { + fprintf(stderr, "ERROR llist_insert(): failed to allocate memory"); + return 0; + } + aux->prev = list->last; + aux->next = NULL; + // TODO: check time complexity of these GMP functions + mpz_init(aux->n); + mpz_set(aux->n, n); + + if(!list->first) + list->first = aux; + list->last = aux; + list->size++; + + return 1; +} diff --git a/src/llist.h b/src/llist.h new file mode 100644 index 0000000..f5c9cb2 --- /dev/null +++ b/src/llist.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2019 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +struct llist_item { + mpz_t n; // the number stored + struct llist_item *next; // the next element in the list + struct llist_item *prev; // the previous element in the list +}; + +struct llist { + struct llist_item *first; // first element of the list + struct llist_item *last; // last element of the list + size_t size; // number of elements in the list +}; + +/* + * initialize an empty linked list where `first` and `last` are + * equal to NULL and `size` is 0. + */ +void llist_init(struct llist *list); + +/* + * free all space allocated to the list. + */ +void llist_deinit(struct llist *list); + +/* + * insert an item at the end of the linked list with value `n`. + * + * returns 1 on success, 0 on failure. + */ +int llist_insert(struct llist *list, mpz_t n); +/* + * insert an item in the linked list in a sorted position. + * + * returns 1 on success, 0 on failure. + */ +int llist_sorted_insert(struct llist *list, mpz_t n); diff --git a/src/main.c b/src/main.c index 25af0a5..baf21f2 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ */ #include "globals.h" +#include "llist.h" #include #include