diff --git a/include/util.h b/include/util.h index c75dc35..2628e13 100644 --- a/include/util.h +++ b/include/util.h @@ -79,5 +79,57 @@ void util_pile_view(pile *); //------------------------------------------------------------------------------ -void util_pile_test(); // def: scr/util/tests +void util_pile_test(); // def: scr/util/tests +void util_double_list_test(); + + + +//============================================================================== + + + /* Structure représentant un élément de la liste. */ + + typedef struct elem + { + int value; + struct elem *prev; + struct elem *next; + } elem ; + + /* Structure d'accès à la liste. */ + + typedef struct + { + elem *first; + elem *last; + }dblist; + + + /* Initialisation de la liste. */ + + void Init(dblist *l); + + /* Ajout d'une valeur en fin de liste. */ + + void PushBack(dblist *l, int val); + + /* Ajout d'une valeur en début de liste. */ + + void PushFront(dblist *l, int val); + + /* Retrait d'une valeur en fin de liste. */ + + int PopBack(dblist *l); + + /* Retrait d'une valeur en début de liste. */ + + int PopFront(dblist *l); + + /* Affichage de toute la liste. */ + + void View(dblist l); + + /* Vidage de toute la liste. */ + + void Clear(dblist *l); diff --git a/src/fsm/measure.c b/src/fsm/measure.c index 6623949..d11fc7a 100644 --- a/src/fsm/measure.c +++ b/src/fsm/measure.c @@ -103,6 +103,7 @@ void fsm_measures_list_init () { fsm_debug_msg (0,0, "fsm_measures_list_init()", 2); // sub_automaton 2 if (0) util_pile_test(); + util_double_list_test(); } diff --git a/src/util/list_2x2.c b/src/util/list_2x2.c new file mode 100644 index 0000000..e35d083 --- /dev/null +++ b/src/util/list_2x2.c @@ -0,0 +1,128 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * +* * +* Gem-graph client * +* * +* Finite State Machine (fsm) header * +* * +* Copyright © 2021 Libre en Communs * +* Copyright © 2021 Adrien Bourmault * +* Copyright © 2021 Jean Sirmai * +* * +* This file is part of Gem-graph. * +* * +* This program is free software: you can redistribute it and/or modify it * +* under the terms of the GNU Affero 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 Affero General Public License for more details. * +* * +* You should have received a copy of the GNU Affero General Public License * +* along with this program. If not, see . * +* * +* * * * * * * * * * * * * * * * * * * * * * * * * * */ + + + +#include "../../include/util.h" + + + + +// https://chgi.developpez.com/liste/ < ^c^v + + + + + +/*************************************************************************/ +void Init(dblist *l) +{ + l->first = NULL; + l->last = NULL; +} +/******************************************************************************/ + +void PushBack(dblist *l, int val) +{ + elem *nouv = malloc(sizeof(elem)); + if(!nouv) exit(EXIT_FAILURE); + nouv->value = val; + nouv->prev = l->last; + nouv->next = NULL; + if(l->last) l->last->next = nouv; + else l->first = nouv; + l->last = nouv; +} +/******************************************************************************/ + +void PushFront(dblist *l, int val) +{ + elem *nouv = malloc(sizeof(elem)); + if(!nouv) exit(EXIT_FAILURE); + nouv->value = val; + nouv->next = l->first; + nouv->prev = NULL; + if(l->first) l->first->prev = nouv; + else l->last = nouv; + l->first = nouv; +} +/******************************************************************************/ + +int PopBack(dblist *l) +{ + int val; + elem *tmp = l->last; + if(!tmp) return -1; + val = tmp->value; + l->last = tmp->prev; + if(l->last) l->last->next = NULL; + else l->first = NULL; + free(tmp); + return val; +} +/******************************************************************************/ + +int PopFront(dblist *l) +{ + int val; + elem *tmp = l->first; + if(!tmp) return -1; + val = tmp->value; + l->first = tmp->next; + if(l->first)l->first->prev = NULL; + else l->last = NULL; + free(tmp); + return val; +} +/******************************************************************************/ + +void View(dblist l) +{ + elem *pelem = l.first; + while(pelem) + { + printf("%d\n",pelem->value); + pelem = pelem->next; + } +} +/******************************************************************************/ + +void Clear(dblist *l) +{ + elem *tmp; + elem *pelem = l->first; + while(pelem) + { + tmp = pelem; + pelem = pelem->next; + free(tmp); + } + l->first = NULL; + l->last = NULL; +} \ No newline at end of file diff --git a/src/util/list_ch2x.c b/src/util/list_ch2x.c.deprec similarity index 100% rename from src/util/list_ch2x.c rename to src/util/list_ch2x.c.deprec diff --git a/src/util/tests.c b/src/util/tests.c index 26b7641..71f1f81 100644 --- a/src/util/tests.c +++ b/src/util/tests.c @@ -67,3 +67,31 @@ void util_pile_test() // https://chgi.developpez.com/liste/ < ^c^v util_pile_clear(&MaPile); /* Vider la pile avant de quitter. */ } + +void util_double_list_test() // https://chgi.developpez.com/liste/ < ^c^v +{ + dblist MaListe; + + Init(&MaListe); + + PushFront(&MaListe,10); + PushBack(&MaListe,20); + PushBack(&MaListe,40); + PushFront(&MaListe,50); + + View(MaListe); + puts("--------------"); + + printf("%d\n",PopFront(&MaListe)); + printf("%d\n",PopFront(&MaListe)); + printf("%d\n",PopBack(&MaListe)); + puts("--------------"); + + PushBack(&MaListe,30); + + printf("%d\n",PopFront(&MaListe)); + printf("%d\n",PopFront(&MaListe)); + puts("--------------"); + + Clear(&MaListe); +}