Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include "Lista.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. void inicializa_lista(Lista *l, int t)
  6. {
  7. l->tamInfo = t;
  8. l->cabeca = NULL;
  9. }
  10.  
  11. int insereNoInicio(Lista *l, void *info)
  12. {
  13. Elemento *p = aloca_elemento(l->tamInfo, info);
  14.  
  15. if(p == NULL)
  16. return 0;/*Erro na alocação.*/
  17.  
  18. p->info = malloc(l->tamInfo);
  19.  
  20. if(p->info == NULL)
  21. {
  22. free(p);
  23. return 0;/*Erro.*/
  24. }
  25.  
  26. memcpy(p->info, info, l->tamInfo);
  27.  
  28. p->proximo = l->cabeca;
  29.  
  30. l->cabeca = p;
  31.  
  32. return 1;
  33. }
  34.  
  35. int insereNoFim(Lista *l, void *info)
  36. {
  37. if(lista_vazia(*l))
  38. return insereNoInicio(l, info);
  39.  
  40. Elemento *p = aloca_elemento(l->tamInfo, info);
  41.  
  42. if(p == NULL)
  43. return 0;
  44.  
  45. Elemento *aux = l->cabeca;
  46.  
  47. while(aux->proximo != NULL)
  48. aux = aux->proximo;
  49.  
  50. p->proximo = NULL;
  51.  
  52. aux->proximo = p;
  53.  
  54. return 1;
  55. }
  56.  
  57. int removeNoInicio(Lista *l, void *info)
  58. {
  59. if(lista_vazia(*l))
  60. return ERRO_LISTA_VAAZIA;
  61.  
  62. Elemento *p = l->cabeca;
  63.  
  64. l->cabeca = p->proximo;/*equivalentes l->cabeca = l->cabeca->proximo;*/
  65.  
  66. memcpy(info, p->info, l->tamInfo);
  67.  
  68. free(p->info);
  69.  
  70. free(p);
  71.  
  72. return 1;
  73. }
  74.  
  75. int removeNoFim(Lista *l, void *info)
  76. {
  77. if(lista_vazia(*l))
  78. return ERRO_LISTA_VAAZIA;
  79.  
  80. if(l->cabeca->proximo == NULL)/* somente quando a lista tem um elemento */
  81. return removeNoInicio(l, info);
  82.  
  83. Elemento *p = l->cabeca;
  84.  
  85. while(p->proximo->proximo != NULL)
  86. p = p->proximo;
  87.  
  88. memcpy(info, p->proximo->info, l->tamInfo);
  89.  
  90. free(p->proximo->info);
  91. free(p->proximo);
  92.  
  93. p->proximo = NULL;
  94.  
  95. return 1;
  96. }
  97.  
  98. int lista_vazia(Lista l)
  99. {
  100. return l.cabeca == NULL;
  101. }
  102.  
  103. Elemento *aloca_elemento(int tamInfo, void *info)
  104. {
  105. Elemento *p = malloc(sizeof(Elemento));
  106.  
  107. if(p == NULL)
  108. return NULL;
  109.  
  110. p->info = malloc(tamInfo);
  111.  
  112. if(p->info == NULL)
  113. {
  114. free(p);
  115.  
  116. return NULL;
  117. }
  118.  
  119. memcpy(p->info, info, tamInfo);
  120.  
  121. return p;
  122. }
  123.  
  124. void mostra_lista(Lista l, void (*mostra_info)(void *))
  125. {
  126. if(lista_vazia(l))
  127. printf("A lista está vazian");
  128. else
  129. {
  130. Elemento *p = l.cabeca;
  131.  
  132. printf("Dados da Lista:n");
  133.  
  134. while(p != NULL)
  135. {
  136. mostra_info(p->info);
  137. p = p->proximo;
  138. }
  139. }
  140. }
  141.  
  142. void limpa_lista(Lista *l)
  143. {
  144. Elemento *p = l->cabeca;
  145.  
  146. while(p != NULL)
  147. {
  148. Elemento *aux = p->proximo;
  149.  
  150. free(p->info);
  151. free(p);
  152.  
  153. p = aux;
  154. }
  155.  
  156. l->cabeca = NULL;
  157. }
  158.  
  159. int insereNaPosicao(Lista *l,void *info,int pos){
  160. if(pos<0)
  161. return ERRO_POSICAO_INVALIDA;
  162. if(pos==0)
  163. return insereNoFim(l,info);
  164. Elemento *p=l->cabeca;
  165. int cont =0;
  166. while(cont<pos-1 && p->proximo!=NULL){
  167. p=p->proximo;
  168. cont++;
  169. }
  170. if(cont!=pos-1)
  171. return ERRO_POSICAO_INVALIDA;
  172. Elemento *novo = aloca_elemento(l->tamInfo,info);
  173. if(novo==NULL)
  174. return 0; // ERRO ALOCACAO
  175. novo->proximo=p->proximo;
  176. p->proximo=novo;
  177. return 1;
  178. }
  179.  
  180. int removeNaPosicao(Lista *l,void *info,int pos){
  181. if(lista_vazia(*l)) return ERRO_LISTA_VAAZIA;
  182. if(pos<0) return ERRO_POSICAO_INVALIDA;
  183. Elemento *p = l->cabeca;
  184. if(pos==0){
  185. removeNoInicio(l,info);
  186. }
  187. int cont;
  188. while(cont<pos-1 &&p->proximo!=NULL){
  189. p=p->proximo;
  190. cont++;
  191. }
  192. if(cont!=pos-1) return ERRO_POSICAO_INVALIDA;
  193. Elemento *aux = p->proximo;
  194. p->proximo = aux ->proximo;
  195. free(aux->info);
  196. free(aux);
  197. return 1;
  198. }
  199.  
  200.  
  201. int compara_float(void *a,void *b){
  202. float *p1=a,*p2=b;
  203. if(*p1>*p2) return 1;
  204. if(*p1<*p2) return -1;
  205. return 0;
  206. }
  207.  
  208.  
  209. int insereEmOrdem(Lista *l,void *info,int(*compara)(void*,void*)){
  210. int cont =0;
  211. Elemento *p = l->cabeca;
  212. while(p!=NULL && compara(info,p->info)>0){
  213. cont++;
  214. p=p->proximo;
  215. }
  216. return insereNaPosicao(l,info,cont);
  217.  
  218. }
  219.  
  220. typedef struct{
  221. char nome[24];
  222. int matricula;
  223. int notas;
  224. int faltas;
  225. }Diario;
  226.  
  227. typedef struct ele
  228. {
  229. void *info;
  230. struct ele *proximo;
  231. }Elemento;
  232.  
  233. typedef struct
  234. {
  235. int tamInfo;
  236. Elemento *cabeca;
  237. }Lista;
  238.  
  239. void mostra_registro(Diario *p){
  240. FILE *fp;
  241. fp = fopen("vaicaber.bin","a+b");
  242. fwrite(p,sizeof(Diario), sizeof(Diario)*1024,fp);
  243. fclose(fp);
  244. // printf("%d %22sn", p->matricula, p->nome, p->matricula);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement