Advertisement
juan_de99

Untitled

Oct 22nd, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /// cola dinamica.h
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define SIN_MEM -1
  7. #define MIN(x,y) ((x)<(y)?(x):(y))
  8.  
  9.  
  10. typedef struct sNodo
  11. {
  12. void *dato;
  13. unsigned tam;
  14. struct sNodo *sig;
  15. }tNodo;
  16.  
  17. typedef struct
  18. {
  19. tNodo *pri,
  20. *ult;
  21. }tCola;
  22.  
  23. void crearCola(tCola *c);
  24. int colaVacia(const tCola *c);
  25. int colaLlena(const tCola *c,unsigned tam);
  26. void vaciarCola(tCola *c);
  27. int ponerEnCola(tCola *c, unsigned tam, const void *dato);
  28. int sacarDeCola(tCola *c, unsigned tam, void *dato);
  29. int verPrimero(const tCola *c,unsigned tam, void *dato);
  30.  
  31. ///.c
  32.  
  33. #include "cDinamica.h"
  34.  
  35.  
  36. void crearCola(tCola *c)
  37. {
  38. c->pri=NULL;
  39. c->ult=NULL;
  40. }
  41. int colaVacia(const tCola *c)
  42. {
  43. return c->pri==NULL;
  44. }
  45. int colaLlena(const tCola *c,unsigned tam)
  46. {
  47. tNodo *nue=malloc(sizeof(tNodo));
  48.  
  49. if(!nue || !(nue->dato=malloc(tam)))
  50. {
  51. free(nue);
  52. return 1;
  53. }
  54. free(nue->dato);
  55. free(nue);
  56.  
  57. return 0;
  58. }
  59. int ponerEnCola(tCola *c, unsigned tam, const void *dato)
  60. {
  61. tNodo *nue;
  62.  
  63. if(!(nue=malloc(sizeof(tNodo))))
  64. return SIN_MEM;
  65. if(!(nue->dato=malloc(tam)))
  66. {
  67. free(nue);
  68. return SIN_MEM;
  69. }
  70.  
  71. memcpy(nue->dato,dato,tam);
  72. nue->tam=tam;
  73. nue->sig=NULL;
  74.  
  75. if(!c->pri)
  76. c->pri=nue;
  77. else
  78. c->ult->sig=nue;
  79. c->ult=nue;
  80.  
  81. return 1;
  82. }
  83. int sacarDeCola(tCola *c, unsigned tam, void *dato)
  84. {
  85. tNodo *elim=c->pri;
  86.  
  87. if(elim)
  88. return -1;
  89.  
  90. memcpy(dato,elim->dato,MIN(elim->tam,tam));
  91. c->pri=elim->sig;
  92. if(!c->pri)
  93. c->ult=NULL;
  94. free(elim->dato);
  95. free(elim);
  96.  
  97. return 1;
  98. }
  99. void vaciarCola(tCola *c)
  100. {
  101. tNodo *elim;
  102.  
  103. while(c->pri)
  104. {
  105. elim=c->pri;
  106. c->pri=elim->sig;
  107. free(elim->dato);
  108. free(elim);
  109. }
  110. c->ult=NULL;
  111. }
  112. int verPrimero(const tCola *c,unsigned tam, void *dato)
  113. {
  114. if(!c->pri)
  115. return -1;
  116.  
  117. memcpy(dato,c->pri->dato,MIN(c->pri->tam,tam));
  118.  
  119. return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement