arraylist.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * File: arraylist.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 14 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "str.h"
  10. #include "arraylist.h"
  11. /* --- Fonctions publiques --- */
  12. void arraylist_ini(arraylist* al){
  13. al->first = NULL;
  14. al->last = NULL;
  15. al->size = 0;
  16. }
  17. al_node* arraylist_add(arraylist* al, char* key, int(*handler)(char*)){
  18. int length;
  19. al_node* aln;
  20. //Regarde si la clef existe
  21. if((aln = arraylist_search(al, key)) != NULL){
  22. arraylist_delete(al, aln);
  23. }
  24. //Ajout des valeurs
  25. aln = malloc(sizeof(al_node));
  26. length = strlen(key);
  27. aln->key = malloc(sizeof(char) * (length + 1));
  28. memset(aln->key, 0, length + 1);
  29. strncpy(aln->key, key, length);
  30. aln->handler = handler;
  31. //Lien entre les noeuds
  32. aln->next = NULL;
  33. if (al->first == NULL) {
  34. aln->prev = NULL;
  35. al->first = aln;
  36. al->last = aln;
  37. } else {
  38. aln->prev = al->last;
  39. al->last->next = aln;
  40. al->last = aln;
  41. }
  42. al->size++;
  43. return aln;
  44. }
  45. al_node* arraylist_search(arraylist* al, char* key){
  46. //Liste vide
  47. if(al->first == NULL){
  48. return NULL;
  49. }
  50. //Cherche dans la liste
  51. al_node* aln = al->first;
  52. int length = strlen(aln->key) + 1;
  53. while (aln != NULL) {
  54. if (strncmp(aln->key, key, length) == 0) {
  55. return aln;
  56. }
  57. aln = aln->next;
  58. }
  59. return NULL;
  60. }
  61. int arraylist_call(arraylist* al, char* key, char* arg){
  62. al_node* aln = arraylist_search(al, key);
  63. if(aln == NULL){
  64. return -1;
  65. }
  66. return aln->handler(arg);
  67. }
  68. void arraylist_delete(arraylist* al, al_node* aln){
  69. //Liste vide
  70. if(al->first == NULL){
  71. return;
  72. }
  73. //Si 1er et seul
  74. if (aln->prev == NULL && aln->next == NULL) {
  75. al->first = NULL;
  76. al->last = NULL;
  77. }
  78. //Si 1er et non seul
  79. else if (aln->prev == NULL && aln->next != NULL) {
  80. aln->next->prev = NULL;
  81. al->first = aln->next;
  82. }
  83. //Si dernier
  84. else if (aln->next == NULL) {
  85. aln->prev->next = NULL;
  86. al->last = aln->prev;
  87. }
  88. //Sinon si il est au milieu
  89. else {
  90. aln->prev->next = aln->next;
  91. aln->next->prev = aln->prev;
  92. }
  93. //Free
  94. free(aln->key);
  95. free(aln);
  96. al->size--;
  97. }
  98. boolean arraylist_remove(arraylist* al, char* key){
  99. al_node* aln = arraylist_search(al, key);
  100. if(aln == NULL){
  101. return false;
  102. }
  103. arraylist_delete(al, aln);
  104. return true;
  105. }
  106. void arraylist_clean(arraylist* al){
  107. //Liste vide
  108. if(al->first == NULL){
  109. return;
  110. }
  111. //Vide liste
  112. al_node* tmp, * aln = al->first;
  113. while (aln != NULL) {
  114. tmp = aln->next;
  115. arraylist_delete(al, aln);
  116. aln = tmp;
  117. }
  118. al->first = NULL;
  119. al->last = NULL;
  120. al->size = 0;
  121. }