Alx09

lstd

May 8th, 2022
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #if !defined(LISTAD_NUME) || !defined(LISTAD_ELEMENT)
  5. #error "LISTAD_NUME si LISTAD_ELEMENT trebuie definite inainte de a include listad.h"
  6. #endif
  7.  
  8. typedef struct {
  9.     LISTAD_ELEMENT *prim;
  10.     LISTAD_ELEMENT *ultim;
  11. }LISTAD_NUME;
  12.  
  13. #ifndef LISTAD_FN
  14. // aceste macrouri sunt definite doar daca listad.h nu a mai fost inclus anterior
  15. #define LISTAD_FNAUX(NUME,OPERATIE)     NUME##_##OPERATIE
  16. #define LISTAD_FN(NUME,OPERATIE)     LISTAD_FNAUX(NUME,OPERATIE)
  17. #endif
  18.  
  19. void LISTAD_FN(LISTAD_NUME, init)(LISTAD_NUME *lista)
  20. {
  21.     lista->prim = lista->ultim = NULL;
  22. }
  23.  
  24.  
  25. void LISTAD_FN(LISTAD_NUME, adauga)(LISTAD_NUME *lista, LISTAD_ELEMENT *e)
  26. {  
  27.    
  28.     e->pred = lista->ultim;
  29.     if (lista->ultim) {
  30.         lista->ultim->urm = e;
  31.     }
  32.     else {
  33.         lista->prim = e;
  34.     }
  35.     lista->ultim = e;
  36.     e->urm = NULL;
  37. }
  38. void LISTAD_FN(LISTAD_NUME, adaugaElementLaPoz)(LISTAD_NUME *lista, LISTAD_ELEMENT *e, LISTAD_ELEMENT *elementAnterior)
  39. {  
  40.    
  41.     if(e == lista->ultim  || lista->prim == null){
  42.     e->pred = lista->ultim;
  43.     if (lista->ultim) {
  44.         lista->ultim->urm = e;
  45.     }
  46.     lista->ultim = e;
  47.     e->urm = NULL;
  48.     }
  49.     else{
  50.         e->urm = elementAnterior->urm;
  51.         e->pred = elementAnterior->pred;
  52.         elementAnterior->urm = e;
  53.     }
  54.  
  55. }
  56.  
  57. void LISTAD_FN(LISTAD_NUME, sterge)(LISTAD_NUME *lista, LISTAD_ELEMENT *e)
  58. {
  59.     if (e->pred) {
  60.         e->pred->urm = e->urm;
  61.     }
  62.     else {
  63.         lista->prim = e->urm;
  64.     }
  65.     if (e->urm) {
  66.         e->urm->pred = e->pred;
  67.     }
  68.     else {
  69.         lista->ultim = e->pred;
  70.     }
  71.     free(e);
  72. }
  73.  
  74. void LISTAD_FN(LISTAD_NUME, elibereaza)(LISTAD_NUME *lista)
  75. {
  76.     LISTAD_ELEMENT *e, *urm;
  77.     for (e = lista->prim; e; e = urm) {
  78.         urm = e->urm;
  79.         free(e);
  80.     }
  81.     LISTAD_FN(LISTAD_NUME, init)(lista);
  82. }
  83.  
  84. // definitiile trebuie sterse pentru a putea refolosi listad.h de mai multe ori in cadrul aceluiasi fisier
  85. #undef LISTAD_NUME
  86. #undef LISTAD_ELEMENT
Advertisement
Add Comment
Please, Sign In to add comment