Advertisement
Guest User

library para Linked List .h

a guest
Mar 26th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include "LinkedListFuncs.h"
  6.  
  7. void initList(List * l) {
  8. *l = NULL;
  9. }
  10.  
  11. Boolean VerifyEmptyList(List l) {
  12. return (l == NULL) ? TRUE : FALSE;
  13. }
  14.  
  15. ListNode* NewNode(void* data) {
  16. ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
  17.  
  18. if (newNode) {
  19. newNode->next = NULL;
  20. newNode->pData = data;
  21. }
  22. return(newNode);
  23.  
  24. }
  25.  
  26. ListNode* NewNode() {
  27. ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
  28.  
  29. if (newNode) {
  30. newNode->next = NULL;
  31. newNode->pData = NULL;
  32. }
  33. return(newNode);
  34.  
  35. }
  36.  
  37. Status InsertIni(List* l, void* data) {
  38. ListNode* newNode = NewNode(data);
  39.  
  40. if (newNode != NULL) {
  41. newNode->next = *l;
  42. *l = newNode;
  43. return OK;
  44. }
  45. return ERROR;
  46. }
  47.  
  48. Status InsertEnd(List* l, void* data) {
  49. ListNode* newNode = NewNode(data), *auxNode;
  50.  
  51. void* s = newNode->pData;
  52.  
  53. if (newNode != NULL) {
  54. if (*l == NULL) {
  55. *l = newNode;
  56. }
  57. else {
  58. auxNode = *l;
  59. while (auxNode != NULL) {
  60. if (auxNode->next == NULL) {
  61. auxNode->next = newNode;
  62.  
  63. return OK;
  64. }
  65. else {
  66. auxNode = auxNode->next;
  67. }
  68.  
  69. }
  70. }
  71. }
  72. return ERROR;
  73. }
  74.  
  75. int ListSize(List l) {
  76. int count = 0;
  77.  
  78. while (l != NULL) {
  79. count++;
  80. l = l->next;
  81. }
  82. return count;
  83. }
  84.  
  85. void PrintNodes(List l, char*(*f)(void* data)) {
  86. if (VerifyEmptyList(l) == TRUE) {
  87. printf("\nThe list is empty!\n");
  88. return;
  89. }
  90.  
  91. while (l != NULL) {
  92.  
  93. char* funcRet = f(l->pData);
  94.  
  95. printf("\n%s", funcRet);
  96.  
  97. free(funcRet);
  98. l = l->next;
  99. }
  100. }List findPreviousNode(List l, List node) {
  101.  
  102. List aux = l;
  103.  
  104. while (l != NULL) {
  105. if (l->next == node) {
  106. return l;
  107. }
  108. else {
  109. l = l->next;
  110. }
  111. }
  112.  
  113. return NULL;
  114.  
  115. }
  116.  
  117.  
  118. void removeNode(List* l, List prevNode, List nodeRm) {
  119.  
  120. ListNode* adressAux = nodeRm->next;
  121. List* listAux = l;
  122.  
  123. while (l != NULL) {
  124. if (*l == nodeRm) {
  125. free((*l)->pData);
  126. free(l);
  127. break;
  128. }
  129. else {
  130. *l = (*l)->next;
  131. }
  132. }
  133.  
  134. l = listAux;
  135.  
  136. while (l != NULL) {
  137. if (*l == prevNode) {
  138. prevNode->next = adressAux;
  139. break;
  140. }
  141. else {
  142. (*l) = (*l)->next;
  143. }
  144. }
  145.  
  146.  
  147. }
  148. //
  149. //Apagar elementos da lista :
  150. //
  151. //aux = L;
  152. //while (aux != NULL) {
  153. // naux = aux->next;
  154. // free(aux->data);
  155. // free(aux);
  156. // aux = naux;
  157. //}
  158. //L = NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement