Copyright © 2016 CGi. Aucune reproduction, même partielle,... > Je vais demander l'autorisation.
This commit is contained in:
parent
b64df19b3b
commit
e5d1c124e6
117
include/util.h
117
include/util.h
|
@ -34,102 +34,39 @@
|
|||
#include <gtk-4.0/gtk/gtk.h>
|
||||
|
||||
|
||||
char *util_read_file(char *filename);
|
||||
|
||||
char *util_read_file (char *filename);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// https://chgi.developpez.com/liste/ < ^c^v
|
||||
|
||||
|
||||
/* Structure représentant un élément de la pile. */
|
||||
|
||||
typedef struct pile
|
||||
{
|
||||
int valeur;
|
||||
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 *);
|
||||
|
||||
// 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
|
||||
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
|
||||
void util_pile_clear (pile **); // vide la pile
|
||||
int util_pile_length (pile *p); // retourne le nombre d'éléments de la pile
|
||||
void util_pile_view (pile *); // affiche la pile en commençant par le sommet
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
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();
|
||||
|
||||
|
||||
|
||||
//==============================================================================
|
||||
|
||||
|
||||
/* 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
|
||||
if (0) util_pile_test();
|
||||
util_double_list_test();
|
||||
if (0) util_double_list_test();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -28,92 +28,76 @@
|
|||
* * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
|
||||
|
||||
#include "../../include/util.h"
|
||||
|
||||
|
||||
|
||||
|
||||
// https://chgi.developpez.com/liste/ < ^c^v
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*************************************************************************/
|
||||
void Init(dblist *l)
|
||||
void list_2x2_init(dblist *l)
|
||||
{
|
||||
l->first = NULL;
|
||||
l->last = NULL;
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
void PushBack(dblist *l, int val)
|
||||
void list_2x2_push_back(dblist *l, int val)
|
||||
{
|
||||
elem *nouv = malloc(sizeof(elem));
|
||||
if(!nouv) exit(EXIT_FAILURE);
|
||||
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;
|
||||
if (l->last) l->last->next = nouv;
|
||||
else l->first = nouv;
|
||||
l->last = nouv;
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
void PushFront(dblist *l, int val)
|
||||
void list_2x2_push_front(dblist *l, int val)
|
||||
{
|
||||
elem *nouv = malloc(sizeof(elem));
|
||||
if(!nouv) exit(EXIT_FAILURE);
|
||||
if (! nouv) exit (EXIT_FAILURE);
|
||||
nouv->value = val;
|
||||
nouv->next = l->first;
|
||||
nouv->prev = NULL;
|
||||
if(l->first) l->first->prev = nouv;
|
||||
if (l->first) l->first->prev = nouv;
|
||||
else l->last = nouv;
|
||||
l->first = nouv;
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
int PopBack(dblist *l)
|
||||
int list_2x2_pop_back(dblist *l)
|
||||
{
|
||||
int val;
|
||||
elem *tmp = l->last;
|
||||
if(!tmp) return -1;
|
||||
if (! tmp) return -1;
|
||||
val = tmp->value;
|
||||
l->last = tmp->prev;
|
||||
if(l->last) l->last->next = NULL;
|
||||
if (l->last) l->last->next = NULL;
|
||||
else l->first = NULL;
|
||||
free(tmp);
|
||||
free (tmp);
|
||||
return val;
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
int PopFront(dblist *l)
|
||||
int list_2x2_pop_front(dblist *l)
|
||||
{
|
||||
int val;
|
||||
elem *tmp = l->first;
|
||||
if(!tmp) return -1;
|
||||
if (! tmp) return -1;
|
||||
val = tmp->value;
|
||||
l->first = tmp->next;
|
||||
if(l->first)l->first->prev = NULL;
|
||||
if (l->first)l->first->prev = NULL;
|
||||
else l->last = NULL;
|
||||
free(tmp);
|
||||
free (tmp);
|
||||
return val;
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
void View(dblist l)
|
||||
void list_2x2_view(dblist l)
|
||||
{
|
||||
elem *pelem = l.first;
|
||||
while(pelem)
|
||||
while (pelem)
|
||||
{
|
||||
printf("%d\n",pelem->value);
|
||||
printf ("%d\n", pelem->value);
|
||||
pelem = pelem->next;
|
||||
}
|
||||
}
|
||||
/******************************************************************************/
|
||||
|
||||
void Clear(dblist *l)
|
||||
void list_2x2_clear(dblist *l)
|
||||
{
|
||||
elem *tmp;
|
||||
elem *pelem = l->first;
|
||||
|
@ -125,4 +109,4 @@ void Clear(dblist *l)
|
|||
}
|
||||
l->first = NULL;
|
||||
l->last = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,56 +32,43 @@
|
|||
#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));
|
||||
if(!element) exit(EXIT_FAILURE); /* Si l'allocation a échoué. */
|
||||
pile *element = malloc (sizeof(pile));
|
||||
if (!element) exit (EXIT_FAILURE); // Si l'allocation a échoué.
|
||||
element->valeur = Val;
|
||||
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;
|
||||
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;
|
||||
Val = (*p)->valeur;
|
||||
free(*p);
|
||||
*p = tmp; /* Le pointeur pointe sur le dernier élément. */
|
||||
return Val; /* Retourne la valeur soutirée de la pile. */
|
||||
free (*p);
|
||||
*p = tmp; // Le pointeur pointe sur le dernier élément.
|
||||
return Val; // Retourne la valeur soutirée de la pile.
|
||||
}
|
||||
|
||||
/*************************************************************************/
|
||||
|
||||
void util_pile_clear(pile **p)
|
||||
void util_pile_clear (pile **p)
|
||||
{
|
||||
pile *tmp;
|
||||
while(*p)
|
||||
while (*p)
|
||||
{
|
||||
tmp = (*p)->prec;
|
||||
free(*p);
|
||||
free (*p);
|
||||
*p = tmp;
|
||||
}
|
||||
}
|
||||
/*************************************************************************/
|
||||
|
||||
int util_pile_length(pile *p)
|
||||
int util_pile_length (pile *p)
|
||||
{
|
||||
int n=0;
|
||||
while(p)
|
||||
while (p)
|
||||
{
|
||||
n++;
|
||||
p = p->prec;
|
||||
|
@ -89,13 +76,11 @@ int util_pile_length(pile *p)
|
|||
return n;
|
||||
}
|
||||
|
||||
/*************************************************************************/
|
||||
|
||||
void util_pile_view(pile *p)
|
||||
void util_pile_view (pile *p)
|
||||
{
|
||||
while(p)
|
||||
while (p)
|
||||
{
|
||||
printf("%d\n",p->valeur);
|
||||
printf ("%d\n", p->valeur);
|
||||
p = p->prec;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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, 25);
|
||||
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 :");
|
||||
util_pile_view(MaPile); /* Affiche la totalité de la pile. */
|
||||
util_pile_view(MaPile); // Affiche la totalité de la pile.
|
||||
puts("------");
|
||||
|
||||
printf("Nb d'elements : %d\n",util_pile_length(MaPile));
|
||||
puts("------");
|
||||
|
||||
puts("Deux valeurs soutirees de la pile :");
|
||||
printf("%d\n",util_pile_pop(&MaPile)); /* Affiche deux valeurs */
|
||||
printf("%d\n",util_pile_pop(&MaPile)); /* soutirées 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)); // soutirées de la pile.
|
||||
puts("------");
|
||||
|
||||
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("------");
|
||||
|
||||
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 :");
|
||||
util_pile_view(MaPile); /* Affiche la totalité de la pile. */
|
||||
util_pile_view(MaPile); // Affiche la totalité de la pile.
|
||||
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);
|
||||
PushBack(&MaListe,20);
|
||||
PushBack(&MaListe,40);
|
||||
PushFront(&MaListe,50);
|
||||
list_2x2_push_front(&MaListe,10);
|
||||
list_2x2_push_back(&MaListe,20);
|
||||
list_2x2_push_back(&MaListe,40);
|
||||
list_2x2_push_front(&MaListe,50);
|
||||
|
||||
View(MaListe);
|
||||
puts("--------------");
|
||||
list_2x2_view(MaListe);
|
||||
puts("--------------");
|
||||
|
||||
printf("%d\n",PopFront(&MaListe));
|
||||
printf("%d\n",PopFront(&MaListe));
|
||||
printf("%d\n",PopBack(&MaListe));
|
||||
puts("--------------");
|
||||
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||
printf("%d\n",list_2x2_pop_back(&MaListe));
|
||||
puts("--------------");
|
||||
|
||||
PushBack(&MaListe,30);
|
||||
list_2x2_push_back(&MaListe,30);
|
||||
|
||||
printf("%d\n",PopFront(&MaListe));
|
||||
printf("%d\n",PopFront(&MaListe));
|
||||
puts("--------------");
|
||||
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||
printf("%d\n",list_2x2_pop_front(&MaListe));
|
||||
puts("--------------");
|
||||
|
||||
Clear(&MaListe);
|
||||
list_2x2_clear(&MaListe);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue