double chained list ^c^v success from https://chgi.developpez.com/liste/

This commit is contained in:
Jean Sirmai 2024-08-25 11:35:43 +02:00
parent 6545c587d2
commit b64df19b3b
Signed by: jean
GPG Key ID: FB3115C340E057E3
5 changed files with 210 additions and 1 deletions

View File

@ -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);

View File

@ -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();
}

128
src/util/list_2x2.c Normal file
View File

@ -0,0 +1,128 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Gem-graph client *
* *
* Finite State Machine (fsm) header *
* *
* Copyright © 2021 Libre en Communs <contact@a-lec.org> *
* Copyright © 2021 Adrien Bourmault <neox@a-lec.org> *
* Copyright © 2021 Jean Sirmai <jean@a-lec.org> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * */
#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;
}

View File

@ -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);
}