fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Node {
  7. char *data;
  8. struct Node *prev;
  9. struct Node *next;
  10. };
  11.  
  12. struct Node* createNode(const char *data) {
  13. struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
  14. if (!newNode) return NULL;
  15.  
  16. newNode->data = (char*)malloc(10 * sizeof(char));
  17. strncpy(newNode->data, data, 9);
  18. newNode->data[9] = '\0';
  19.  
  20. newNode->prev = NULL;
  21. newNode->next = NULL;
  22.  
  23. return newNode;
  24. }
  25.  
  26. void insertAfter(struct Node *node, const char *data) {
  27. if (!node) return;
  28.  
  29. struct Node *newNode = createNode(data);
  30.  
  31. newNode->next = node->next;
  32. newNode->prev = node;
  33.  
  34. if (node->next)
  35. node->next->prev = newNode;
  36.  
  37. node->next = newNode;
  38. }
  39.  
  40. void insertBefore(struct Node **head, struct Node *node, const char *data) {
  41. if (!node || !head) return;
  42.  
  43. struct Node *newNode = createNode(data);
  44.  
  45. newNode->next = node;
  46. newNode->prev = node->prev;
  47.  
  48. if (node->prev)
  49. node->prev->next = newNode;
  50. else
  51. *head = newNode;
  52.  
  53. node->prev = newNode;
  54. }
  55.  
  56. void deleteNode(struct Node **head, struct Node *node) {
  57. if (!node || !head || !*head) return;
  58.  
  59. if (node->prev)
  60. node->prev->next = node->next;
  61. else
  62. *head = node->next;
  63.  
  64. if (node->next)
  65. node->next->prev = node->prev;
  66.  
  67. free(node->data);
  68. free(node);
  69. }
  70.  
  71. void printList(struct Node *head) {
  72. printf("List: ");
  73. while (head) {
  74. printf("%s <-> ", head->data);
  75. head = head->next;
  76. }
  77. printf("NULL\n");
  78. }
  79.  
  80. void freeList(struct Node *head) {
  81. struct Node *tmp;
  82. while (head) {
  83. tmp = head;
  84. head = head->next;
  85. free(tmp->data);
  86. free(tmp);
  87. }
  88. }
  89.  
  90. int main() {
  91.  
  92. printf("==== POPULATE LIST ====\n");
  93.  
  94. struct Node *head = createNode("A");
  95. struct Node *b = createNode("B");
  96. struct Node *c = createNode("C");
  97.  
  98. head->next = b;
  99. b->prev = head;
  100.  
  101. b->next = c;
  102. c->prev = b;
  103.  
  104. printList(head);
  105.  
  106. printf("\n==== INSERT AFTER B ====\n");
  107. insertAfter(b, "X");
  108. printList(head);
  109.  
  110. printf("\n==== INSERT BEFORE B ====\n");
  111. insertBefore(&head, b, "Y");
  112. printList(head);
  113.  
  114. printf("\n==== DELETE NODE B ====\n");
  115. deleteNode(&head, b);
  116. printList(head);
  117.  
  118. printf("\n==== INSERT AT HEAD (before A) ====\n");
  119. insertBefore(&head, head, "HEAD");
  120. printList(head);
  121.  
  122. printf("\n==== INSERT AFTER TAIL ====\n");
  123. struct Node *tail = head;
  124. while (tail->next) tail = tail->next;
  125. insertAfter(tail, "TAIL+1");
  126. printList(head);
  127.  
  128. printf("\n==== CLEANUP MEMORY ====\n");
  129. freeList(head);
  130.  
  131. return 0;
  132. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
==== POPULATE LIST ====
List: A <-> B <-> C <-> NULL

==== INSERT AFTER B ====
List: A <-> B <-> X <-> C <-> NULL

==== INSERT BEFORE B ====
List: A <-> Y <-> B <-> X <-> C <-> NULL

==== DELETE NODE B ====
List: A <-> Y <-> X <-> C <-> NULL

==== INSERT AT HEAD (before A) ====
List: HEAD <-> A <-> Y <-> X <-> C <-> NULL

==== INSERT AFTER TAIL ====
List: HEAD <-> A <-> Y <-> X <-> C <-> TAIL+1 <-> NULL

==== CLEANUP MEMORY ====