Copyright © 2016 CGi. Aucune reproduction, même partielle,... > Je vais demander l'autorisation.
This commit is contained in:
parent
b64df19b3b
commit
e5d1c124e6
113
include/util.h
113
include/util.h
|
@ -36,100 +36,37 @@
|
||||||
|
|
||||||
char *util_read_file (char *filename);
|
char *util_read_file (char *filename);
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// https://chgi.developpez.com/liste/ < ^c^v
|
// https://chgi.developpez.com/liste/ < ^c^v
|
||||||
|
// Copyright © 2016 CGi.
|
||||||
|
// Aucune reproduction, même partielle, ne peut être faite de ce site
|
||||||
|
// ni de l'ensemble de son contenu : textes, documents, images, etc.
|
||||||
|
// sans l'autorisation expresse de l'auteur.
|
||||||
|
// Sinon vous encourez selon la loi jusqu'à trois ans de prison
|
||||||
|
// et jusqu'à 300 000 € de dommages et intérêts.
|
||||||
|
|
||||||
|
typedef struct pile {int valeur; struct pile *prec;} pile; // structure d'un élément
|
||||||
/* Structure représentant un élément de la pile. */
|
void util_pile_push (pile **, int); // empile une valeur sur la pile
|
||||||
|
int util_pile_pop (pile **); // pop retire la dernière valeur empilée
|
||||||
typedef struct pile
|
void util_pile_clear (pile **); // vide la pile
|
||||||
{
|
int util_pile_length (pile *p); // retourne le nombre d'éléments de la pile
|
||||||
int valeur;
|
void util_pile_view (pile *); // affiche la pile en commençant par le sommet
|
||||||
struct pile *prec;
|
|
||||||
} pile ;
|
|
||||||
|
|
||||||
|
|
||||||
/* Push empile une valeur sur la pile. */
|
|
||||||
|
|
||||||
void util_pile_push(pile **, int);
|
|
||||||
|
|
||||||
|
|
||||||
/* Pop retire la dernière valeur empilée sur la pile. */
|
|
||||||
|
|
||||||
int util_pile_pop(pile **);
|
|
||||||
|
|
||||||
|
|
||||||
/* Clear vide la pile. */
|
|
||||||
|
|
||||||
void util_pile_clear(pile **);
|
|
||||||
|
|
||||||
|
|
||||||
/* Length retourne le nombre d'éléments de la pile. */
|
|
||||||
|
|
||||||
int util_pile_length(pile *p);
|
|
||||||
|
|
||||||
|
|
||||||
/* Affiche la totalité de la pile en commençant par le sommet. */
|
|
||||||
|
|
||||||
void util_pile_view(pile *);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
void util_pile_test(); // def: scr/util/tests
|
typedef struct elem {int value; struct elem *prev; struct elem *next;} elem ;
|
||||||
|
typedef struct {elem *first; elem *last;} dblist; // structure d'accès à la liste
|
||||||
|
void list_2x2_init (dblist *l); // initialisation
|
||||||
|
void list_2x2_push_back (dblist *l, int val); // ajout d'une valeur en fin de liste
|
||||||
|
void list_2x2_push_front (dblist *l, int val); // ajout d'une valeur en début de liste
|
||||||
|
int list_2x2_pop_back (dblist *l); // retrait d'une valeur en fin de liste
|
||||||
|
int list_2x2_pop_front (dblist *l); // retrait d'une valeur en début de liste
|
||||||
|
void list_2x2_view (dblist l); // affichage de toute la liste
|
||||||
|
void list_2x2_clear (dblist *l); // vidage de toute la liste
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void util_pile_test();
|
||||||
void util_double_list_test();
|
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);
|
|
||||||
|
|
||||||
|
|
|
@ -103,7 +103,7 @@ void fsm_measures_list_init ()
|
||||||
{
|
{
|
||||||
fsm_debug_msg (0,0, "fsm_measures_list_init()", 2); // sub_automaton 2
|
fsm_debug_msg (0,0, "fsm_measures_list_init()", 2); // sub_automaton 2
|
||||||
if (0) util_pile_test();
|
if (0) util_pile_test();
|
||||||
util_double_list_test();
|
if (0) util_double_list_test();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -28,27 +28,16 @@
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "../../include/util.h"
|
#include "../../include/util.h"
|
||||||
|
|
||||||
|
|
||||||
|
void list_2x2_init(dblist *l)
|
||||||
|
|
||||||
// https://chgi.developpez.com/liste/ < ^c^v
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************/
|
|
||||||
void Init(dblist *l)
|
|
||||||
{
|
{
|
||||||
l->first = NULL;
|
l->first = NULL;
|
||||||
l->last = NULL;
|
l->last = NULL;
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
void PushBack(dblist *l, int val)
|
void list_2x2_push_back(dblist *l, int val)
|
||||||
{
|
{
|
||||||
elem *nouv = malloc (sizeof(elem));
|
elem *nouv = malloc (sizeof(elem));
|
||||||
if (! nouv) exit (EXIT_FAILURE);
|
if (! nouv) exit (EXIT_FAILURE);
|
||||||
|
@ -59,9 +48,8 @@ void PushBack(dblist *l, int val)
|
||||||
else l->first = nouv;
|
else l->first = nouv;
|
||||||
l->last = nouv;
|
l->last = nouv;
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
void PushFront(dblist *l, int val)
|
void list_2x2_push_front(dblist *l, int val)
|
||||||
{
|
{
|
||||||
elem *nouv = malloc(sizeof(elem));
|
elem *nouv = malloc(sizeof(elem));
|
||||||
if (! nouv) exit (EXIT_FAILURE);
|
if (! nouv) exit (EXIT_FAILURE);
|
||||||
|
@ -72,9 +60,8 @@ void PushFront(dblist *l, int val)
|
||||||
else l->last = nouv;
|
else l->last = nouv;
|
||||||
l->first = nouv;
|
l->first = nouv;
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
int PopBack(dblist *l)
|
int list_2x2_pop_back(dblist *l)
|
||||||
{
|
{
|
||||||
int val;
|
int val;
|
||||||
elem *tmp = l->last;
|
elem *tmp = l->last;
|
||||||
|
@ -86,9 +73,8 @@ int PopBack(dblist *l)
|
||||||
free (tmp);
|
free (tmp);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
int PopFront(dblist *l)
|
int list_2x2_pop_front(dblist *l)
|
||||||
{
|
{
|
||||||
int val;
|
int val;
|
||||||
elem *tmp = l->first;
|
elem *tmp = l->first;
|
||||||
|
@ -100,9 +86,8 @@ int PopFront(dblist *l)
|
||||||
free (tmp);
|
free (tmp);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
void View(dblist l)
|
void list_2x2_view(dblist l)
|
||||||
{
|
{
|
||||||
elem *pelem = l.first;
|
elem *pelem = l.first;
|
||||||
while (pelem)
|
while (pelem)
|
||||||
|
@ -111,9 +96,8 @@ void View(dblist l)
|
||||||
pelem = pelem->next;
|
pelem = pelem->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/******************************************************************************/
|
|
||||||
|
|
||||||
void Clear(dblist *l)
|
void list_2x2_clear(dblist *l)
|
||||||
{
|
{
|
||||||
elem *tmp;
|
elem *tmp;
|
||||||
elem *pelem = l->first;
|
elem *pelem = l->first;
|
||||||
|
|
|
@ -1,125 +0,0 @@
|
||||||
/* * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
||||||
* *
|
|
||||||
* 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 <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
|
||||||
// https://learntutorials.net/fr/c/topic/560/listes-liees < ^c^v vvv
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct doubly_node
|
|
||||||
{
|
|
||||||
struct doubly_node * prev;
|
|
||||||
struct doubly_node * next;
|
|
||||||
};
|
|
||||||
|
|
||||||
void doubly_node_bind (struct doubly_node * prev, struct doubly_node * next)
|
|
||||||
{
|
|
||||||
prev->next = next;
|
|
||||||
next->prev = prev;
|
|
||||||
}
|
|
||||||
|
|
||||||
void doubly_node_make_empty_circularly_list (struct doubly_node * head)
|
|
||||||
{
|
|
||||||
doubly_node_bind (head, head);
|
|
||||||
}
|
|
||||||
|
|
||||||
void doubly_node_insert_between
|
|
||||||
(struct doubly_node * prev, struct doubly_node * next, struct doubly_node * insertion)
|
|
||||||
{
|
|
||||||
doubly_node_bind (prev, insertion);
|
|
||||||
doubly_node_bind (insertion, next);
|
|
||||||
}
|
|
||||||
|
|
||||||
void doubly_node_insert_before
|
|
||||||
(struct doubly_node * tail, struct doubly_node * insertion)
|
|
||||||
{
|
|
||||||
doubly_node_insert_between (tail->prev, tail, insertion);
|
|
||||||
}
|
|
||||||
|
|
||||||
void doubly_node_insert_after
|
|
||||||
(struct doubly_node * head, struct doubly_node * insertion)
|
|
||||||
{
|
|
||||||
doubly_node_insert_between (head, head->next, insertion);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Node {
|
|
||||||
int data;
|
|
||||||
struct Node *next;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
void insert_node (struct Node **head, int nodeValue);
|
|
||||||
void print_list (struct Node *head);
|
|
||||||
|
|
||||||
int any () {
|
|
||||||
struct Node* headNode;
|
|
||||||
headNode = NULL; /* Initialize our first node pointer to be NULL. */
|
|
||||||
size_t listSize, i;
|
|
||||||
do {
|
|
||||||
printf("How many numbers would you like to input?\n");
|
|
||||||
} while(1 != scanf("%zu", &listSize));
|
|
||||||
|
|
||||||
for (i = 0; i < listSize; i++) {
|
|
||||||
int numToAdd;
|
|
||||||
do {
|
|
||||||
printf("Enter a number:\n");
|
|
||||||
} while (1 != scanf("%d", &numToAdd));
|
|
||||||
|
|
||||||
insert_node (&headNode, numToAdd);
|
|
||||||
printf("Current list after your inserted node: \n");
|
|
||||||
print_list(headNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_list (struct Node *head) {
|
|
||||||
struct Node* currentNode = head;
|
|
||||||
|
|
||||||
/* Iterate through each link. */
|
|
||||||
while (currentNode != NULL) {
|
|
||||||
printf("Value: %d\n", currentNode->data);
|
|
||||||
currentNode = currentNode -> next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert_node (struct Node **head, int nodeValue) {
|
|
||||||
struct Node *currentNode = malloc(sizeof *currentNode);
|
|
||||||
currentNode->data = nodeValue;
|
|
||||||
currentNode->next = (*head);
|
|
||||||
|
|
||||||
*head = currentNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -32,39 +32,27 @@
|
||||||
#include "../../include/util.h"
|
#include "../../include/util.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// https://chgi.developpez.com/liste/ < ^c^v
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
void util_pile_push (pile **p, int Val)
|
void util_pile_push (pile **p, int Val)
|
||||||
{
|
{
|
||||||
pile *element = malloc (sizeof(pile));
|
pile *element = malloc (sizeof(pile));
|
||||||
if(!element) exit(EXIT_FAILURE); /* Si l'allocation a échoué. */
|
if (!element) exit (EXIT_FAILURE); // Si l'allocation a échoué.
|
||||||
element->valeur = Val;
|
element->valeur = Val;
|
||||||
element->prec = *p;
|
element->prec = *p;
|
||||||
*p = element; /* Le pointeur pointe sur le dernier élément. */
|
*p = element; // Le pointeur pointe sur le dernier élément.
|
||||||
}
|
}
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
int util_pile_pop (pile **p)
|
int util_pile_pop (pile **p)
|
||||||
{
|
{
|
||||||
int Val;
|
int Val;
|
||||||
pile *tmp;
|
pile *tmp;
|
||||||
if(!*p) return -1; /* Retourne -1 si la pile est vide. */
|
if (!*p) return -1; // Retourne -1 si la pile est vide.
|
||||||
tmp = (*p)->prec;
|
tmp = (*p)->prec;
|
||||||
Val = (*p)->valeur;
|
Val = (*p)->valeur;
|
||||||
free (*p);
|
free (*p);
|
||||||
*p = tmp; /* Le pointeur pointe sur le dernier élément. */
|
*p = tmp; // Le pointeur pointe sur le dernier élément.
|
||||||
return Val; /* Retourne la valeur soutirée de la pile. */
|
return Val; // Retourne la valeur soutirée de la pile.
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
void util_pile_clear (pile **p)
|
void util_pile_clear (pile **p)
|
||||||
{
|
{
|
||||||
|
@ -76,7 +64,6 @@ void util_pile_clear(pile **p)
|
||||||
*p = tmp;
|
*p = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
int util_pile_length (pile *p)
|
int util_pile_length (pile *p)
|
||||||
{
|
{
|
||||||
|
@ -89,8 +76,6 @@ int util_pile_length(pile *p)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*************************************************************************/
|
|
||||||
|
|
||||||
void util_pile_view (pile *p)
|
void util_pile_view (pile *p)
|
||||||
{
|
{
|
||||||
while (p)
|
while (p)
|
||||||
|
|
|
@ -32,66 +32,66 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void util_pile_test() // https://chgi.developpez.com/liste/ < ^c^v
|
void util_pile_test()
|
||||||
{
|
{
|
||||||
pile *MaPile = NULL; /* Impératif de l'initialiser à NULL */
|
pile *MaPile = NULL; // Impératif de l'initialiser à NULL
|
||||||
|
|
||||||
util_pile_push(&MaPile, 10);
|
util_pile_push(&MaPile, 10);
|
||||||
util_pile_push(&MaPile, 25);
|
util_pile_push(&MaPile, 25);
|
||||||
util_pile_push(&MaPile, 33);
|
util_pile_push(&MaPile, 33);
|
||||||
util_pile_push(&MaPile, 12); /* Empile 4 valeurs. */
|
util_pile_push(&MaPile, 12); // Empile 4 valeurs.
|
||||||
|
|
||||||
puts("Affichage de la pile :");
|
puts("Affichage de la pile :");
|
||||||
util_pile_view(MaPile); /* Affiche la totalité de la pile. */
|
util_pile_view(MaPile); // Affiche la totalité de la pile.
|
||||||
puts("------");
|
puts("------");
|
||||||
|
|
||||||
printf("Nb d'elements : %d\n",util_pile_length(MaPile));
|
printf("Nb d'elements : %d\n",util_pile_length(MaPile));
|
||||||
puts("------");
|
puts("------");
|
||||||
|
|
||||||
puts("Deux valeurs soutirees de la pile :");
|
puts("Deux valeurs soutirées de la pile :");
|
||||||
printf("%d\n",util_pile_pop(&MaPile)); /* Affiche deux valeurs */
|
printf("%d\n",util_pile_pop(&MaPile)); // Affiche deux valeurs
|
||||||
printf("%d\n",util_pile_pop(&MaPile)); /* soutirées de la pile. */
|
printf("%d\n",util_pile_pop(&MaPile)); // soutirées de la pile.
|
||||||
puts("------");
|
puts("------");
|
||||||
|
|
||||||
puts("Affichage de la pile :");
|
puts("Affichage de la pile :");
|
||||||
util_pile_view(MaPile); /* Affiche la totalité de la pile. */
|
util_pile_view(MaPile); // Affiche la totalité de la pile.
|
||||||
puts("------");
|
puts("------");
|
||||||
|
|
||||||
util_pile_clear(&MaPile); /* Vide la pile. */
|
util_pile_clear(&MaPile); // Vide la pile.
|
||||||
|
|
||||||
util_pile_push(&MaPile, 18); /* Empile une valeur. */
|
util_pile_push(&MaPile, 18); // Empile une valeur.
|
||||||
|
|
||||||
puts("Affichage de la pile apres vidage et ajout d'une valeur :");
|
puts("Affichage de la pile apres vidage et ajout d'une valeur :");
|
||||||
util_pile_view(MaPile); /* Affiche la totalité de la pile. */
|
util_pile_view(MaPile); // Affiche la totalité de la pile.
|
||||||
puts("------\n");
|
puts("------\n");
|
||||||
|
|
||||||
util_pile_clear(&MaPile); /* Vider la pile avant de quitter. */
|
util_pile_clear(&MaPile); // Vider la pile avant de quitter.
|
||||||
}
|
}
|
||||||
|
|
||||||
void util_double_list_test() // https://chgi.developpez.com/liste/ < ^c^v
|
void util_double_list_test()
|
||||||
{
|
{
|
||||||
dblist MaListe;
|
dblist MaListe;
|
||||||
|
|
||||||
Init(&MaListe);
|
list_2x2_init(&MaListe);
|
||||||
|
|
||||||
PushFront(&MaListe,10);
|
list_2x2_push_front(&MaListe,10);
|
||||||
PushBack(&MaListe,20);
|
list_2x2_push_back(&MaListe,20);
|
||||||
PushBack(&MaListe,40);
|
list_2x2_push_back(&MaListe,40);
|
||||||
PushFront(&MaListe,50);
|
list_2x2_push_front(&MaListe,50);
|
||||||
|
|
||||||
View(MaListe);
|
list_2x2_view(MaListe);
|
||||||
puts("--------------");
|
puts("--------------");
|
||||||
|
|
||||||
printf("%d\n",PopFront(&MaListe));
|
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||||
printf("%d\n",PopFront(&MaListe));
|
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||||
printf("%d\n",PopBack(&MaListe));
|
printf("%d\n",list_2x2_pop_back(&MaListe));
|
||||||
puts("--------------");
|
puts("--------------");
|
||||||
|
|
||||||
PushBack(&MaListe,30);
|
list_2x2_push_back(&MaListe,30);
|
||||||
|
|
||||||
printf("%d\n",PopFront(&MaListe));
|
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||||
printf("%d\n",PopFront(&MaListe));
|
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||||
puts("--------------");
|
puts("--------------");
|
||||||
|
|
||||||
Clear(&MaListe);
|
list_2x2_clear(&MaListe);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue