Advertisement
Guest User

Lista y Tramite

a guest
Oct 24th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. TDATramite.h
  2.  
  3. #ifndef TDATRAMITE_H_INCLUDED
  4. #define TDATRAMITE_H_INCLUDED
  5.  
  6. typedef struct Tramite {
  7. char * nombre;
  8. int duracion;
  9. int llegada;
  10. } tTramite;
  11.  
  12. tTramite* crearTramite( char* nom,int d, int l );
  13.  
  14. void setNombre( tTramite* t, char* nom);
  15.  
  16. void setDuracion(tTramite* t, int d);
  17.  
  18. void setLlegada(tTramite* t, int l);
  19.  
  20. char* getNombre(tTramite t);
  21.  
  22. int getDuracion(tTramite t);
  23.  
  24. int getLlegada(tTramite t);
  25.  
  26. #endif // TDATRAMITE_H_INCLUDED
  27.  
  28.  
  29. /////////////////
  30.  
  31. TDATramite.c
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "TDATramite.h"
  36.  
  37.  
  38. tTramite* crearTramite( char* nom,int d, int l ){
  39. tTramite* nuevoTramite = (tTramite*) malloc(sizeof(tTramite));
  40. nuevoTramite->nombre=nom;
  41. nuevoTramite->duracion=d;
  42. nuevoTramite->llegada=l;
  43. return nuevoTramite;
  44. }
  45.  
  46. void setNombre( tTramite* t, char* nom){
  47. t->nombre=nom;
  48. }
  49.  
  50. void setDuracion(tTramite* t, int d){
  51. t->duracion=d;
  52. }
  53.  
  54. void setLlegada(tTramite* t, int l){
  55. t->llegada=l;
  56. }
  57.  
  58. char* getNombre(tTramite t){
  59. return t.nombre;
  60. }
  61.  
  62. int getDuracion(tTramite t){
  63. return t.duracion;
  64. }
  65.  
  66. int getLlegada(tTramite t){
  67. return t.llegada;
  68. }
  69.  
  70.  
  71. /////////////////////////////////
  72.  
  73. TDALista.h
  74.  
  75. #ifndef TDALISTA_H_INCLUDED
  76. #define TDALISTA_H_INCLUDED
  77. struct Nodo;
  78. typedef struct Nodo tNodo;
  79. struct Lista {
  80. tNodo* ppio; //puntero al primer nodo de la lista
  81. tNodo* fin; //puntero al último nodo de la lista
  82. int cant; // cantidad de elementos de la lista
  83. int (*comparador)(void*,void*);
  84. };
  85. typedef struct Lista tListaOrdenada;
  86.  
  87.  
  88. tListaOrdenada crearLista(int (*f)(void*,void*));
  89.  
  90. void insertar(tListaOrdenada* L, void* x);
  91.  
  92. tNodo* siguiente(tNodo* pos);
  93.  
  94. tNodo* primera(tListaOrdenada L);
  95.  
  96. tNodo* ultima(tListaOrdenada L);
  97.  
  98. void* elemento(tNodo* pos);
  99.  
  100. int listaVacia(tListaOrdenada L);
  101.  
  102.  
  103.  
  104. #endif // TDALISTA_H_INCLUDED
  105.  
  106.  
  107. ///////////////////////////////
  108.  
  109. TDALista.c
  110.  
  111. #include <stdio.h>
  112. #include <stdlib.h>
  113. #include "TDALista.h"
  114.  
  115. struct Nodo {
  116. void * elem;
  117. struct Nodo* next;
  118. };
  119.  
  120. tListaOrdenada crearLista(int (*f)(void*,void*)){
  121. tListaOrdenada Lista;
  122. Lista.ppio=NULL;
  123. Lista.fin=NULL;
  124. Lista.cant=0;
  125. Lista.comparador = f;
  126. return Lista;
  127. }
  128.  
  129. void insertar(tListaOrdenada* L, void* x) {
  130. tNodo* nuevoNodo = (tNodo*) malloc(sizeof(tNodo));
  131. nuevoNodo->elem = x;
  132. nuevoNodo->next= NULL;
  133.  
  134. tNodo* aux= L->ppio;
  135. if (L->cant==0) {
  136. L->ppio = nuevoNodo;
  137. L->fin = nuevoNodo;
  138. L->cant = 1;
  139. nuevoNodo->next= NULL;
  140. }
  141. else {
  142. if (L->cant==1){
  143. if (L->comparador(nuevoNodo->elem, aux->elem) < 0){
  144. nuevoNodo->next=aux;
  145. L->ppio=nuevoNodo;
  146. L->cant+=1;
  147. }
  148. else{
  149. aux->next=nuevoNodo;
  150. L->fin=nuevoNodo;
  151. L->cant+=1;
  152. }
  153. }
  154. else{
  155. //Comparo con el primer elemento de la lista
  156. if (L->comparador(nuevoNodo->elem,aux->elem)<0)
  157. {
  158. nuevoNodo->next=aux;
  159. L->ppio=nuevoNodo;
  160. L->cant+=1;
  161.  
  162. }
  163.  
  164. else{
  165.  
  166. int encontre=0;
  167. while ((aux->next!=NULL)&&!encontre) {
  168.  
  169.  
  170. if (L->comparador(nuevoNodo->elem, aux->next->elem) < 0) {
  171. nuevoNodo->next=aux->next;
  172. aux->next=nuevoNodo;
  173. L->cant+=1;
  174. encontre=1;
  175. }
  176. else {
  177. aux=aux->next;
  178. }
  179. }
  180.  
  181. if (!encontre) {
  182. aux->next=nuevoNodo;
  183. L->fin=nuevoNodo;
  184. L->cant+=1;
  185. }
  186. }
  187. }
  188. }
  189. }
  190.  
  191. tNodo* siguiente(tNodo* pos) {
  192. if (pos==NULL) return NULL;
  193. else return pos->next;
  194. }
  195.  
  196. tNodo* primera(tListaOrdenada L) {
  197. return L.ppio;
  198. }
  199.  
  200. tNodo* ultima(tListaOrdenada L) {
  201. return L.fin;
  202. }
  203.  
  204. void* elemento(tNodo* pos) {
  205. return pos->elem;
  206. }
  207.  
  208. int listaVacia(tListaOrdenada L) {
  209. if (L.cant==0) return 1;
  210. else return 0;
  211.  
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement