Herowaree

Lista_Encadeada.c

Nov 9th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "lista_encadeada.h"
  4.  
  5. void iniciarLista(struct lista_enc *lista){
  6. lista->cabeca = NULL;
  7. lista->tamanho = 0;
  8. }
  9.  
  10. int vaziaLista(struct lista_enc *lista){
  11. if(lista->tamanho == 0){
  12. return 1;
  13. }else{
  14. return 0;
  15. }
  16. }
  17.  
  18. int tamanhoLista(struct lista_enc *lista){
  19. return lista->tamanho;
  20. }
  21.  
  22. int buscaIndice(struct lista_enc *lista, int indice, int *dado){
  23. struct no *ptr = lista->cabeca;
  24. int count = 1;
  25.  
  26. if(lista->cabeca == NULL) return 0;
  27.  
  28. while((ptr!=NULL) && (count < indice)){
  29. ptr = ptr->proximo;
  30. count++;
  31. }
  32.  
  33. if(ptr == NULL) return 0;
  34.  
  35. *dado = ptr->valor;
  36.  
  37. return 1;
  38. }
  39.  
  40. int buscaValor(struct lista_enc *lista, int valor){
  41. if(lista->cabeca == NULL) return 0;
  42. struct no *ptr = lista->cabeca;
  43. int count = 1;
  44.  
  45. while(ptr != NULL){
  46. if(ptr->valor == valor){
  47. return count;
  48. }
  49. ptr = ptr->proximo;
  50. count++;
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. int insereLista(struct lista_enc *lista, int indice, int valor){
  57. int ret;
  58.  
  59. if(vaziaLista(lista)){
  60. if(indice != 1){
  61. return 0;
  62. }
  63. ret = insereVaziaLista(lista, valor);
  64. return ret;
  65. }else if(indice == 1){
  66. ret = insereComecoLista(lista, valor);
  67. return ret;
  68. }else if(indice == lista->tamanho){
  69. ret = insereFimLista(lista, valor);
  70. return ret;
  71. }else{
  72. ret = insereMeioLista(lista, indice, valor);
  73. return ret;
  74. }
  75. }
  76.  
  77. int insereVaziaLista(struct lista_enc *lista, int valor){
  78. struct no *novoNo;
  79. novoNo = malloc(sizeof(struct no));
  80.  
  81. if(novoNo == NULL) return 0;
  82.  
  83. novoNo->valor = valor;
  84. novoNo->proximo = NULL;
  85. lista->cabeca = novoNo;
  86. ++(lista->tamanho);
  87.  
  88. return 1;
  89. }
  90.  
  91. int insereComecoLista(struct lista_enc *lista, int valor){
  92. struct no *novoNo;
  93. novoNo = malloc(sizeof(struct no));
  94. if(novoNo == NULL){
  95. return 0;
  96. }
  97.  
  98. novoNo->valor = valor;
  99. novoNo->proximo = lista->cabeca;
  100. lista->cabeca = novoNo;
  101. ++(lista->tamanho);
  102.  
  103. return 1;
  104. }
  105.  
  106. int insereFimLista(struct lista_enc *lista, int valor){
  107. struct no *novoNo;
  108. novoNo = malloc(sizeof(struct no));
  109. if(novoNo == NULL){
  110. return 0;
  111. }
  112. novoNo->valor = valor;
  113.  
  114. struct no *ptr = lista->cabeca;
  115. while(ptr->proximo !=NULL){
  116. ptr = ptr->proximo;
  117. }
  118.  
  119. ptr->proximo = novoNo;
  120. novoNo->proximo = NULL;
  121. ++(lista->tamanho);
  122.  
  123. return 1;
  124. }
  125.  
  126. int insereMeioLista(struct lista_enc *lista, int indice, int valor){
  127. struct no *ptr = lista->cabeca;
  128. struct no *novoNo;
  129. int count = 1;
  130.  
  131. while((count < indice - 1) && (ptr!=NULL)){
  132. ptr = ptr->proximo;
  133. count++;
  134. }
  135.  
  136. if(ptr==NULL){
  137. return 0;
  138. }
  139.  
  140. novoNo = malloc(sizeof(struct no));
  141. if(novoNo == NULL) return 0;
  142.  
  143. novoNo->valor = valor;
  144. novoNo->proximo = ptr->proximo;
  145. ptr->proximo = novoNo;
  146. ++(lista->tamanho);
  147.  
  148. return 1;
  149. }
  150.  
  151. int removerLista(struct lista_enc *lista, int indice, int *dado){
  152. int ret;
  153.  
  154. if(vaziaLista(lista)){
  155. return 0;
  156. }
  157.  
  158. if(indice == 1){
  159. ret = removeComecoLista(lista, dado);
  160. return ret;
  161. }else{
  162. ret = removeMeioLista(lista, indice, dado);
  163. return ret;
  164. }
  165.  
  166. return ret;
  167. }
  168.  
  169. int removeComecoLista(struct lista_enc *lista, int *dado){
  170. struct no *ptr = lista->cabeca;
  171.  
  172. *dado = ptr->valor;
  173. lista->cabeca = ptr->proximo;
  174. --(lista->tamanho);
  175. free(ptr);
  176.  
  177. return 1;
  178. }
  179.  
  180. int removeMeioLista(struct lista_enc *lista, int indice, int *dado){
  181. struct no *aux, *ptr = lista->cabeca;
  182. int count = 1;
  183.  
  184. while((count <= indice - 1) && (ptr != NULL)){
  185. aux = ptr;
  186. ptr = ptr->proximo;
  187. count++;
  188. }
  189.  
  190. if(ptr == NULL) return 0;
  191.  
  192. *dado = ptr->valor;
  193. aux->proximo = ptr->proximo;
  194. --(lista->tamanho);
  195. free(ptr);
  196.  
  197. return 1;
  198. }
  199.  
  200. int mostrarLista(struct lista_enc *lista){
  201. struct no *ptr = lista->cabeca;
  202. int count = 1;
  203.  
  204. if(vaziaLista(lista)){
  205. return 0;
  206. }
  207.  
  208. printf("Tamanho da lista: %d elementos(s)\n", lista->tamanho);
  209. printf("-----------------------------------\n");
  210. while(ptr != NULL){
  211. printf("\n\tNO : %d\n", count);
  212. printf("\tVALOR : %d\n", ptr->valor);
  213. count++;
  214. ptr = ptr->proximo;
  215. }
  216. printf("\n");
  217. return 1;
  218. }
  219.  
  220. int dividirLista(struct lista_enc *lista, struct lista_enc *lista_par, struct lista_enc *lista_impar){
  221. struct no *ptr = lista->cabeca;
  222.  
  223. while(ptr!=NULL){
  224. if((ptr->valor % 2)== 0){
  225. insereLista(lista_par, lista_par->tamanho + 1, ptr->valor);
  226. }else{
  227. insereLista(lista_impar, lista_impar->tamanho + 1, ptr->valor);
  228. }
  229. ptr = ptr->proximo;
  230. }
  231.  
  232. mostrarLista(lista_par);
  233. mostrarLista(lista_impar);
  234.  
  235. return 1;
  236. }
  237.  
  238. int concatenaLista(struct lista_enc *lista, struct lista_enc *lista2, struct lista_enc *novalista){
  239. struct no *ptr = lista->cabeca;
  240. int count = 1;
  241.  
  242. if(vaziaLista(lista) || vaziaLista(lista2)){
  243. return 0;
  244. }
  245.  
  246. while(ptr!=NULL){
  247. insereLista(novalista, count, ptr->valor);
  248. ptr = ptr->proximo;
  249. count++;
  250. }
  251.  
  252. ptr = lista2->cabeca;
  253.  
  254. while(ptr!=NULL){
  255. insereLista(novalista, count, ptr->valor);
  256. ptr = ptr->proximo;
  257. count++;
  258. }
  259.  
  260. mostrarLista(novalista);
  261.  
  262. return 1;
  263. }
  264.  
  265. int modificarElemento(struct lista_enc *lista, int posicao, int valor){
  266. if(posicao > lista->tamanho || posicao <= 0){
  267. return 0;
  268. }
  269.  
  270. struct no *ptr = lista->cabeca;
  271. int count = 1;
  272.  
  273. while( (count < posicao) && (ptr != NULL) ){
  274. ptr = ptr->proximo;
  275. count++;
  276. }
  277.  
  278. ptr->valor = valor;
  279.  
  280. return 1;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment