Advertisement
Guest User

TDALista

a guest
Oct 22nd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. TDALista.h
  2.  
  3. #ifndef TDALISTA_H_INCLUDED
  4. #define TDALISTA_H_INCLUDED
  5. struct Nodo;
  6. typedef struct Nodo tNodo;
  7. struct Lista {
  8. tNodo* ppio; //puntero al primer nodo de la lista
  9. tNodo* fin; //puntero al último nodo de la lista
  10. int cant; // cantidad de elementos de la lista
  11. int (*comparador)(void*,void*);
  12. };
  13. typedef struct Lista tListaOrdenada;
  14.  
  15.  
  16. tListaOrdenada crearLista(int (*f)(void*,void*));
  17.  
  18. void insertar(tListaOrdenada* L, void* x);
  19.  
  20. tNodo* siguiente(tNodo* pos);
  21.  
  22. tNodo* primera(tListaOrdenada L);
  23.  
  24. tNodo* ultima(tListaOrdenada L);
  25.  
  26. void* elemento(tNodo* pos);
  27.  
  28. int listaVacia(tListaOrdenada L);
  29.  
  30.  
  31.  
  32. #endif // TDALISTA_H_INCLUDED
  33.  
  34.  
  35. /////////////////////////
  36.  
  37. TDALista.c
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "TDALista.h"
  42.  
  43. struct Nodo {
  44. void * elem;
  45. struct Nodo* next;
  46. };
  47.  
  48. tListaOrdenada crearLista(int (*f)(void*,void*)){
  49. tListaOrdenada Lista;
  50. Lista.ppio=NULL;
  51. Lista.fin=NULL;
  52. Lista.cant=0;
  53. Lista.comparador = f;
  54. return Lista;
  55. }
  56.  
  57. void insertar(tListaOrdenada* L, void* x) {
  58. tNodo* nuevoNodo = (tNodo*) malloc(sizeof(tNodo));
  59. nuevoNodo->elem = x;
  60. nuevoNodo->next= NULL;
  61.  
  62. tNodo* aux= L->ppio;
  63. if (aux==NULL) {
  64. L->ppio = nuevoNodo;
  65. L->fin = nuevoNodo;
  66. L->cant = 1;
  67. nuevoNodo->next= NULL;
  68. }
  69. else {
  70. if (L->cant==1){
  71. if (L->comparador(nuevoNodo->elem, aux->elem < 0)){
  72. nuevoNodo->next=aux;
  73. L->ppio=nuevoNodo;
  74. L->cant+=1;
  75. }
  76. else{
  77. aux->next=nuevoNodo;
  78. L->fin=nuevoNodo;
  79. L->cant+=1;
  80. }
  81. }
  82. else{
  83. int encontre=0;
  84. while ((aux!=NULL)&&!encontre) {
  85. if (aux->next!=NULL){
  86. if (L->comparador(nuevoNodo->elem, aux->next->elem < 0)) {
  87. nuevoNodo->next=aux->next;
  88. aux->next=nuevoNodo;
  89. L->cant+=1;
  90. encontre=1;
  91. }
  92. else {
  93. aux=aux->next;
  94. }
  95. }
  96. }
  97. if (!encontre) {
  98. aux->next=nuevoNodo;
  99. L->fin=nuevoNodo;
  100. L->cant+=1;
  101. }
  102. }
  103. }
  104. }
  105.  
  106. tNodo* siguiente(tNodo* pos) {
  107. if (pos==NULL) return NULL;
  108. else return pos->next;
  109. }
  110.  
  111. tNodo* primera(tListaOrdenada L) {
  112. return L.ppio;
  113. }
  114.  
  115. tNodo* ultima(tListaOrdenada L) {
  116. return L.fin;
  117. }
  118.  
  119. void* elemento(tNodo* pos) {
  120. return pos->elem;
  121. }
  122.  
  123. int listaVacia(tListaOrdenada L) {
  124. if (L.cant==0) return 1;
  125. else return 0;
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement