fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Node {
  7. char *value;
  8. struct Node *prev;
  9. struct Node *next;
  10. };
  11.  
  12. // allocate node with 10-char buffer
  13. struct Node* createNode(const char *value)
  14. {
  15. struct Node *newNode = malloc(sizeof(struct Node));
  16.  
  17. newNode->value = malloc(10);
  18. strncpy(newNode->value, value, 9);
  19. newNode->value[9] = '\0'; // safety null termination
  20. newNode->prev = NULL;
  21. newNode->next = NULL;
  22. return newNode;
  23. }
  24.  
  25. // print list
  26. void printList(struct Node *head)
  27. {
  28. struct Node *curr = head;
  29.  
  30. printf("NULL <-> ");
  31. while (curr)
  32. {
  33. printf("%s <-> ", curr->value);
  34. curr = curr->next;
  35. }
  36. printf("NULL\n");
  37. }
  38. ////////////////////////////////////////////////////
  39. // insert at end
  40. void pushBack(struct Node **head, const char *value)
  41. {
  42. struct Node *n = createNode(value);
  43.  
  44. if (*head == NULL)
  45. {
  46. *head = n;
  47. return;
  48. }
  49.  
  50. struct Node *curr = *head;
  51. while (curr->next)
  52. curr = curr->next;
  53.  
  54. curr->next = n;
  55. n->prev = curr;
  56. }
  57. ////////////////////////////////////////////////////////
  58. // insert after
  59. void insertAfter(struct Node **head, const char *target, const char *value)
  60. {
  61. struct Node *curr = *head;
  62.  
  63. while (curr)
  64. {
  65. if (strcmp(curr->value, target) == 0)
  66. {
  67. struct Node *n = createNode(value);
  68.  
  69. n->next = curr->next;
  70. n->prev = curr;
  71.  
  72. if (curr->next)
  73. curr->next->prev = n;
  74.  
  75. curr->next = n;
  76. return;
  77. }
  78. curr = curr->next;
  79. }
  80. }
  81. /////////////////////////////////////////////////////////
  82. // insert before
  83. void insertBefore(struct Node **head, const char *target, const char *value)
  84. {
  85. struct Node *curr = *head;
  86.  
  87. while (curr)
  88. {
  89. if (strcmp(curr->value, target) == 0)
  90. {
  91. struct Node *n = createNode(value);
  92.  
  93. n->next = curr;
  94. n->prev = curr->prev;
  95.  
  96. if (curr->prev)
  97. curr->prev->next = n;
  98. else
  99. *head = n;
  100.  
  101. curr->prev = n;
  102. return;
  103. }
  104. curr = curr->next;
  105. }
  106. }
  107.  
  108. // delete after
  109. void deleteAfter(struct Node **head, const char *target)
  110. {
  111. struct Node *curr = *head;
  112.  
  113. while (curr)
  114. {
  115. if (strcmp(curr->value, target) == 0 && curr->next)
  116. {
  117. struct Node *del = curr->next;
  118.  
  119. curr->next = del->next;
  120. if (del->next)
  121. del->next->prev = curr;
  122.  
  123. free(del->value); // FREE STRING
  124. free(del); // FREE NODE
  125. return;
  126. }
  127. curr = curr->next;
  128. }
  129. }
  130.  
  131. // delete before
  132. void deleteBefore(struct Node **head, const char *target)
  133. {
  134. struct Node *curr = *head;
  135.  
  136. while (curr)
  137. {
  138. if (strcmp(curr->value, target) == 0 && curr->prev)
  139. {
  140. struct Node *del = curr->prev;
  141.  
  142. if (del->prev)
  143. {
  144. del->prev->next = curr;
  145. curr->prev = del->prev;
  146. }
  147. else
  148. {
  149. *head = curr;
  150. curr->prev = NULL;
  151. }
  152.  
  153. free(del->value); // FREE STRING
  154. free(del); // FREE NODE
  155. return;
  156. }
  157. curr = curr->next;
  158. }
  159. }
  160.  
  161. // reverse list
  162. void reverseList(struct Node **head)
  163. {
  164. struct Node *curr = *head;
  165. struct Node *temp = NULL;
  166.  
  167. while (curr)
  168. {
  169. temp = curr->prev;
  170. curr->prev = curr->next;
  171. curr->next = temp;
  172. curr = curr->prev;
  173. }
  174.  
  175. if (temp)
  176. *head = temp->prev;
  177. }
  178.  
  179. // FREE ENTIRE LIST (IMPORTANT)
  180. void freeList(struct Node **head)
  181. {
  182. struct Node *curr = *head;
  183. struct Node *next = NULL;
  184.  
  185. while (curr)
  186. {
  187. next = curr->next;
  188. free(curr->value);
  189. free(curr);
  190. curr = next;
  191. }
  192.  
  193. *head = NULL;
  194. }
  195.  
  196. // demo
  197. int main()
  198. {
  199. struct Node *head = NULL;
  200.  
  201. pushBack(&head, "A");
  202. pushBack(&head, "B");
  203. pushBack(&head, "C");
  204. pushBack(&head, "D");
  205.  
  206. printList(head);
  207.  
  208. insertAfter(&head, "B", "X");
  209. printList(head);
  210.  
  211. insertBefore(&head, "C", "Y");
  212. printList(head);
  213.  
  214. deleteAfter(&head, "A");
  215. printList(head);
  216.  
  217. deleteBefore(&head, "D");
  218. printList(head);
  219.  
  220. reverseList(&head);
  221. printList(head);
  222.  
  223. freeList(&head); // CLEAN MEMORY
  224.  
  225. return 0;
  226. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
NULL <-> A <-> B <-> C <-> D <-> NULL
NULL <-> A <-> B <-> X <-> C <-> D <-> NULL
NULL <-> A <-> B <-> X <-> Y <-> C <-> D <-> NULL
NULL <-> A <-> X <-> Y <-> C <-> D <-> NULL
NULL <-> A <-> X <-> Y <-> D <-> NULL
NULL <-> D <-> Y <-> X <-> A <-> NULL