Advertisement
CaduUnb

listalib.h

May 11th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. //Biblioteca de listas.
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. typedef struct item
  7. {
  8. char letra;
  9. int valor;
  10. struct item* prox;
  11. }celula;
  12.  
  13. celula* criaLista();
  14. celula* ListaNsimoItem(celula* ptrLista, int posicao);
  15. celula* ListaIncluiComeco(celula* ptrLista);
  16. int ListaTamanho(celula* ptrLista);
  17. celula* ListaOrdenaDecrescente(celula *ptrLista);
  18. celula* ListaOrdenaCrescente(celula* ptrLista);
  19.  
  20. //Definicao
  21. celula* ListaCria()
  22. {
  23. celula* ptrLista;
  24. ptrLista = (celula*) malloc(sizeof(celula));
  25. ptrLista->prox = NULL; //fim da lista
  26. return ptrLista;
  27. }
  28. celula* ListaIncluiComeco(celula* ptrLista)
  29. {
  30. celula* novo;
  31. novo = (celula*)malloc(sizeof(celula));
  32. novo->prox = (celula*) ptrLista;
  33. ptrLista = novo;
  34. return ptrLista;
  35. }
  36. // celula* incluiFinal(celula* ptrLista)
  37. // {
  38. // celula* novo;
  39. // novo = (celula*)malloc(sizeof(celula));
  40. // }
  41. int ListaTamanho(celula* ptrLista)
  42. {
  43. int contador=0;
  44. while(ptrLista!=NULL){
  45. contador++;
  46. ptrLista = ptrLista->prox;
  47. }
  48. return contador;
  49. }
  50. celula* ListaNsimoItem(celula* ptrLista, int posicao){
  51. celula* aux;
  52. aux = ptrLista;
  53. if(posicao <= 0){
  54. return aux;
  55. }
  56. while(((posicao-1) != 0) && (ptrLista != NULL)){
  57. aux = ptrLista->prox;
  58. posicao--;
  59. }
  60. return aux;
  61. }
  62.  
  63. celula* ListaOrdenaDecrescente(celula* ptrLista)
  64. {
  65. if(ptrLista == NULL)
  66. return ptrLista; // the list is sorted.
  67. //1- find largest node.
  68. celula *curr, *largest, *largestPrev, *prev;
  69. curr = ptrLista;
  70. largest = ptrLista;
  71. prev = ptrLista;
  72. largestPrev = ptrLista;
  73. while(curr != NULL) {
  74. if(curr->valor > largest->valor) {
  75. largestPrev = prev;
  76. largest = curr;
  77. }
  78. prev = curr;
  79. curr = curr->prox;
  80. }
  81. //2- switching firt node and largest node :
  82. celula* tmp;
  83. if(largest != ptrLista)
  84. {
  85. largestPrev->prox = ptrLista;
  86. tmp = ptrLista->prox;
  87. ptrLista->prox = largest->prox;
  88. largest->prox = tmp;
  89. }
  90.  
  91. // now largest is the first node of the list.
  92.  
  93. // calling the function again with the sub list;
  94. // list minus its first node.
  95. largest->prox = ListaOrdenaDecrescente(largest->prox);
  96.  
  97.  
  98. return largest;
  99. }
  100. celula* ListaOrdenaCrescente(celula* ptrLista)
  101. {
  102. if(ptrLista == NULL)
  103. return ptrLista; // the list is sorted.
  104. //1- find lowest node.
  105. celula *curr, *lowest, *lowestPrev, *prev;
  106. curr = ptrLista;
  107. lowest = ptrLista;
  108. prev = ptrLista;
  109. lowestPrev = ptrLista;
  110. while(curr != NULL) {
  111. if(curr->valor < lowest->valor) {
  112. lowestPrev = prev;
  113. lowest = curr;
  114. }
  115. prev = curr;
  116. curr = curr->prox;
  117. }
  118. //2- switching first node and lowest node :
  119. celula* tmp;
  120. if(lowest != ptrLista)
  121. {
  122. lowestPrev->prox = ptrLista;
  123. tmp = ptrLista->prox;
  124. ptrLista->prox = lowest->prox;
  125. lowest->prox = tmp;
  126. }
  127.  
  128. // now lowest is the first node of the list.
  129.  
  130. // calling the function again with the sub list;
  131. // list minus its first node.
  132. lowest->prox = ListaOrdenaCrescente(lowest->prox);
  133.  
  134.  
  135. return lowest;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement