Create linked list interface.
This commit is contained in:
parent
fadcafaeaf
commit
23592a98ed
61
src/llist.c
Normal file
61
src/llist.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "llist.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
57
src/llist.h
Normal file
57
src/llist.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
* Author: Ortega Froysa, Nicolás <nicolas@ortegas.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gmp.h>
|
||||
|
||||
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);
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "globals.h"
|
||||
#include "llist.h"
|
||||
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
|
Loading…
Reference in New Issue
Block a user