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. // Create node
  13. struct Node* createNode(const char *data) {
  14. struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
  15. if (!newNode) return NULL;
  16.  
  17. newNode->data = (char*)malloc(10);
  18. strncpy(newNode->data, data, 9);
  19. newNode->data[9] = '\0';
  20.  
  21. newNode->prev = NULL;
  22. newNode->next = NULL;
  23.  
  24. return newNode;
  25. }
  26.  
  27. // Insert at end (helper)
  28. void insertEnd(struct Node **head, const char *data) {
  29. struct Node *newNode = createNode(data);
  30.  
  31. if (*head == NULL) {
  32. *head = newNode;
  33. return;
  34. }
  35.  
  36. struct Node *temp = *head;
  37. while (temp->next)
  38. temp = temp->next;
  39.  
  40. temp->next = newNode;
  41. newNode->prev = temp;
  42. }
  43.  
  44. // Insert after a given value
  45. void insertAfter(struct Node **head, const char *target, const char *data) {
  46. struct Node *temp = *head;
  47.  
  48. while (temp && strcmp(temp->data, target) != 0)
  49. temp = temp->next;
  50.  
  51. if (!temp) return;
  52.  
  53. struct Node *newNode = createNode(data);
  54.  
  55. newNode->next = temp->next;
  56. newNode->prev = temp;
  57.  
  58. if (temp->next)
  59. temp->next->prev = newNode;
  60.  
  61. temp->next = newNode;
  62. }
  63.  
  64. // Insert before a given value
  65. void insertBefore(struct Node **head, const char *target, const char *data) {
  66. struct Node *temp = *head;
  67.  
  68. while (temp && strcmp(temp->data, target) != 0)
  69. temp = temp->next;
  70.  
  71. if (!temp) return;
  72.  
  73. struct Node *newNode = createNode(data);
  74.  
  75. newNode->next = temp;
  76. newNode->prev = temp->prev;
  77.  
  78. if (temp->prev)
  79. temp->prev->next = newNode;
  80. else
  81. *head = newNode; // updating head
  82.  
  83. temp->prev = newNode;
  84. }
  85.  
  86. // Delete node by value
  87. void deleteNode(struct Node **head, const char *target) {
  88. struct Node *temp = *head;
  89.  
  90. while (temp && strcmp(temp->data, target) != 0)
  91. temp = temp->next;
  92.  
  93. if (!temp) return;
  94.  
  95. if (temp->prev)
  96. temp->prev->next = temp->next;
  97. else
  98. *head = temp->next;
  99.  
  100. if (temp->next)
  101. temp->next->prev = temp->prev;
  102.  
  103. free(temp->data);
  104. free(temp);
  105. }
  106.  
  107. // Reverse DLL
  108. void reverse(struct Node **head) {
  109. struct Node *temp = NULL;
  110. struct Node *current = *head;
  111.  
  112. while (current) {
  113. temp = current->prev;
  114. current->prev = current->next;
  115. current->next = temp;
  116. current = current->prev;
  117. }
  118.  
  119. if (temp)
  120. *head = temp->prev;
  121. }
  122.  
  123. // Print list
  124. void printList(struct Node *head) {
  125. printf("List: ");
  126. while (head) {
  127. printf("%s <-> ", head->data);
  128. head = head->next;
  129. }
  130. printf("NULL\n");
  131. }
  132.  
  133. // Free memory
  134. void freeList(struct Node **head) {
  135. struct Node *temp;
  136.  
  137. while (*head) {
  138. temp = *head;
  139. *head = (*head)->next;
  140. free(temp->data);
  141. free(temp);
  142. }
  143. }
  144.  
  145. int main() {
  146. struct Node *head = NULL;
  147.  
  148. printf("==== CREATE LIST ====\n");
  149. insertEnd(&head, "A");
  150. insertEnd(&head, "B");
  151. insertEnd(&head, "C");
  152. printList(head);
  153.  
  154. printf("\n==== INSERT AFTER B ====\n");
  155. insertAfter(&head, "B", "X");
  156. printList(head);
  157.  
  158. printf("\n==== INSERT BEFORE B ====\n");
  159. insertBefore(&head, "B", "Y");
  160. printList(head);
  161.  
  162. printf("\n==== DELETE NODE B ====\n");
  163. deleteNode(&head, "B");
  164. printList(head);
  165.  
  166. printf("\n==== INSERT AT HEAD (before A) ====\n");
  167. insertBefore(&head, "A", "HEAD");
  168. printList(head);
  169.  
  170. printf("\n==== REVERSE LIST ====\n");
  171. reverse(&head);
  172. printList(head);
  173.  
  174. printf("\n==== CLEANUP ====\n");
  175. freeList(&head);
  176.  
  177. return 0;
  178. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
==== CREATE 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

==== REVERSE LIST ====
List: C <-> X <-> Y <-> A <-> HEAD <-> NULL

==== CLEANUP ====