Guest User

Untitled

a guest
Mar 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4.  
  5. List *List_create() {
  6. List *list = (List *)malloc(sizeof(List));
  7. list->counter = 0;
  8. list->head = NULL;
  9. return list;
  10. }
  11.  
  12. // You still need to add the remaining function definitions, as defined in the
  13. // tasks
  14. void List_print(List *list) {
  15. int i = 0;
  16. Node *end = list->head;
  17. while (i < list->counter) {
  18. printf("-> id: %i\n", end->item->id);
  19. end = end->next;
  20. i++;
  21. }
  22. }
  23.  
  24. void List_append(List *list) {
  25. Node *node = (Node *)malloc(sizeof(Node));
  26. Item *item = (Item *)malloc(sizeof(Item));
  27. node->item = item;
  28. list->counter++;
  29.  
  30. item->id = list->counter;
  31.  
  32. if (list->head != NULL) {
  33. Node *end = list->head->prev;
  34.  
  35. node->prev = end;
  36. node->next = list->head;
  37. end->next = node;
  38. list->head->prev = node;
  39. } else {
  40. node->next = node;
  41. node->prev = node;
  42. list->head = node;
  43. }
  44. }
  45.  
  46. void List_insert(List *list) {
  47. Node *node = (Node *)malloc(sizeof(Node));
  48. Item *item = (Item *)malloc(sizeof(Item));
  49. node->item = item;
  50. list->counter++;
  51. item->id = list->counter;
  52.  
  53. if (list->head != NULL) {
  54. Node *second = list->head;
  55. Node *end = list->head->prev;
  56.  
  57. node->next = second;
  58. node->prev = second->prev;
  59. end->next = node;
  60. second->prev = node;
  61.  
  62. list->head = node;
  63. } else {
  64. node->next = node;
  65. node->prev = node;
  66. list->head = node;
  67. }
  68. }
  69.  
  70. Node *List_find(List *list, int id) {
  71. Node *current = list->head;
  72.  
  73. if (list->head == NULL)
  74. return NULL;
  75.  
  76. int i = 0;
  77. while (i < list->counter) {
  78. if (current->item->id == id) {
  79. return current;
  80. }
  81. current = current->next;
  82. i++;
  83. }
  84.  
  85. return NULL;
  86. }
  87.  
  88. void List_remove(List *list, Node *node) {
  89. if (node != NULL && list->head != NULL) {
  90. Node *prevNode = node->prev;
  91. Node *nextNode = node->next;
  92.  
  93. prevNode->next = nextNode;
  94. nextNode->prev = prevNode;
  95.  
  96. if (list->head == node)
  97. list->head = nextNode;
  98.  
  99. free(node->item);
  100. free(node);
  101.  
  102. list->counter--;
  103.  
  104. if (list->counter == 0)
  105. list->head = NULL;
  106. }
  107. }
  108.  
  109. void List_destroy(List *list) {
  110. if (list->head != NULL) {
  111. while (list->counter != 0) {
  112. Node *end = list->head->prev;
  113. List_remove(list, end);
  114. }
  115. }
  116. }
  117.  
  118. void List_putfirst(List *list, Node *node) {
  119. if (node != NULL && list->counter > 1) {
  120. Node *prevNode = node->prev;
  121. Node *nextNode = node->next;
  122.  
  123. prevNode->next = nextNode;
  124. nextNode->prev = prevNode;
  125.  
  126. Node *head = list->head;
  127. Node *end = head->prev;
  128.  
  129. node->next = head;
  130. node->prev = end;
  131. head->prev = node;
  132. end->next = node;
  133.  
  134. list->head = node;
  135. }
  136. }
  137.  
  138. void List_sort(List *list) {
  139. // If the linked list has at least 2 items
  140. if (list->counter > 1) {
  141.  
  142. bool still_sorting = true;
  143. while (still_sorting) {
  144. still_sorting = false;
  145.  
  146. // The "Compare Node" is the first node in the set of the two compared nodes
  147. Node *cmpNode = list->head;
  148.  
  149. // As long as we haven't hit the end of the list yet
  150. while (cmpNode->next != list->head) {
  151. // If the cmpNode has a higher ID than the next node
  152. if (cmpNode->item->id > cmpNode->next->item->id) {
  153. // Then swap these two nodes
  154. Node *swapNode = cmpNode->next;
  155. Node *nextNode = swapNode->next;
  156. Node *prevNode = cmpNode->prev;
  157.  
  158. swapNode->next = cmpNode;
  159. cmpNode->prev = swapNode;
  160. cmpNode->next = nextNode;
  161. swapNode->prev = prevNode;
  162. prevNode->next = swapNode;
  163. nextNode->prev = cmpNode;
  164.  
  165. // And update the list head pointer if necessary
  166. if (list->head == cmpNode) {
  167. list->head = swapNode;
  168. }
  169.  
  170. still_sorting = true;
  171. }
  172.  
  173. // Now move the cmpNode one space to the right
  174. cmpNode = cmpNode->next;
  175. }
  176.  
  177.  
  178. }
  179. }
  180. }
Add Comment
Please, Sign In to add comment