Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include "lista.h"
  2.  
  3. typedef struct Node{
  4. void* dato;
  5. struct Node* prev;
  6. struct Node* next;
  7. } Node;
  8.  
  9. typedef struct List{
  10. Node* first;
  11. Node* last;
  12. Node* current;
  13. } List;
  14.  
  15.  
  16. List* createList(){
  17. List* L = (List*) malloc(sizeof(List));
  18. L->first = NULL;
  19. L->last = NULL;
  20. L->current = NULL;
  21. return L;
  22. }
  23.  
  24. void pushFront(List* L, void* data){
  25. Node* nodo = (Node*) malloc(sizeof(Node));
  26. nodo->dato = data;
  27. if(!L->first){
  28. L->first = nodo;
  29. L->last = nodo;
  30. nodo->next = nodo;
  31. nodo->prev = nodo;
  32. }
  33. else{
  34. nodo->next = L->first;
  35. nodo->prev = L->last;
  36.  
  37. L->last->next = nodo;
  38. L->first->prev = nodo;
  39.  
  40. L->first = nodo;
  41. }
  42. }
  43.  
  44. void pushBack(List* L, void* data){
  45. Node* nodo = (Node*) malloc(sizeof(Node));
  46. nodo->dato = data;
  47. if(L->first == NULL){
  48. L->first = nodo;
  49. L->last = nodo;
  50. nodo->next = nodo;
  51. nodo->prev = nodo;
  52. }
  53. else{
  54. nodo->next = L->first;
  55. nodo->prev = L->last;
  56.  
  57. L->last->next = nodo;
  58. L->first->prev = nodo;
  59.  
  60. L->last = L->first->prev;
  61. }
  62. }
  63.  
  64. void popFront(List* L){
  65. if(L->first){
  66. Node* aux=NULL;
  67. aux = L->first;
  68. L->first->next->prev = L->last;
  69. L->last->next = L->first->next;
  70. L->first = L->first->next;
  71. free(aux);
  72. }
  73. }
  74.  
  75. void popBack(List* L){
  76. if(L->first){
  77. Node* aux = (Node*) malloc(sizeof(Node));
  78. aux = L->last;
  79. L->last->prev->next = L->first;
  80. L->first->prev = L->last->prev;
  81. L->last = L->last->prev;
  82. free(aux);
  83. }
  84. }
  85.  
  86. void popCurrent(List* L){
  87. if(L->first){
  88. Node* aux = (Node*) malloc(sizeof(Node));
  89. aux = L->current;
  90. L->current->next->prev = L->current->prev;
  91. L->current->prev->next = L->current->next;
  92. if(L->first == L->current){
  93. L->first=L->first->next;
  94. }
  95. L->current = L->current->next;
  96. free(aux);
  97. }
  98. }
  99.  
  100. void* First(List* L){
  101. if(L->first == NULL){
  102. return NULL;
  103. }
  104. else{
  105. L->current = L->first;
  106. return L->first->dato;
  107. }
  108. }
  109.  
  110. void* Last(List* L){
  111. if(L->last == NULL){
  112. return NULL;
  113. }
  114. else{
  115. L->current = L->last;
  116. return L->last->dato;
  117. }
  118. }
  119.  
  120. void* Next(List* L){
  121. if(L->first == NULL){
  122. return NULL;
  123. }
  124. else{
  125. L->current = L->current->next;
  126. return L->current->dato;
  127. }
  128. }
  129.  
  130. void* Current(List* L){
  131. if(L->current == NULL){
  132. return NULL;
  133. }
  134. else{
  135. return L->current->dato;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement