fork download
  1.  
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. struct Node
  9. {
  10. char *value;
  11. struct Node *prev;
  12. struct Node *next;
  13. };
  14.  
  15. #define MAX_STRINGS 10
  16. #define MAX_LENGTH 100
  17. char *stringPool[MAX_STRINGS];
  18.  
  19. void initStringPool()
  20. {
  21. for (int i = 0; i < MAX_STRINGS; i++)
  22. {
  23. stringPool[i] =
  24. (char *)malloc(MAX_LENGTH * sizeof(char));
  25. if (!stringPool[i])
  26. {
  27. fprintf(stderr, "Memory allocation failed for stringPool[%d]\n", i);
  28. exit(1);
  29. }
  30. stringPool[i][0] = '\0';
  31. }
  32. }
  33. // Create node using preallocated buffer
  34. struct Node* createNode (const char *value)
  35. {
  36. struct Node *node =
  37. (struct Node*)
  38. malloc(sizeof(struct Node));
  39. if (!node)
  40. {
  41. fprintf(stderr, "Memory allocation failed for node\n");
  42. exit(1);
  43. }
  44.  
  45. // Find an empty buffer in stringPool
  46. int found = 0;
  47. for (int i = 0; i < MAX_STRINGS; i++)
  48. {
  49. if (stringPool[i][0] == '\0')
  50. { // empty
  51. strncpy(stringPool[i], value, MAX_LENGTH - 1);
  52. stringPool[i][MAX_LENGTH - 1] = '\0';
  53. node->value = stringPool[i];
  54. found = 1;
  55. break;
  56. }
  57. }
  58.  
  59. if (!found) {
  60. fprintf(stderr, "No free string buffer available\n");
  61. free(node);
  62. exit(1);
  63. }
  64.  
  65. node->prev = node->next = NULL;
  66. return node;
  67. }
  68. ////////////////////////////////////////////////////
  69. // Insert at end (pointer-to-pointer style)
  70.  
  71. void insert(struct Node **head, const char *value) {
  72. struct Node **current = head;
  73. struct Node *prev = NULL;
  74.  
  75. while (*current)
  76. {
  77. prev = *current;
  78. current = &((*current)->next);
  79. }
  80.  
  81. struct Node *newNode = createNode(value);
  82. newNode->prev = prev;
  83. *current = newNode;
  84. }
  85. /////////////////////////////////////////////////////////
  86. // Delete node with exact value
  87. void deleteValue(struct Node **head, const char *value)
  88. {
  89. struct Node **current = head;
  90. while (*current)
  91. {
  92. if
  93. (strcmp((*current)->value, value) == 0)
  94. {
  95. struct Node *tmp = *current;
  96. if (tmp->next)
  97. tmp->next->prev = tmp->prev;
  98.  
  99. *current = tmp->next;
  100.  
  101. // Reset the string buffer
  102. tmp->value[0] = '\0';
  103. free(tmp);
  104. return;
  105. }
  106. current = &((*current)->next);
  107. }
  108. }
  109. /////////////////////////////////////////////////////////
  110. // Delete node before a given value
  111. void deleteBefore(struct Node **head, const char *value)
  112. {
  113. struct Node **current = head;
  114. struct Node *prev = NULL;
  115.  
  116. while (*current)
  117. {
  118. if (strcmp((*current)->value, value) == 0 && prev)
  119. {
  120. struct Node *tmp = prev;
  121.  
  122. if (tmp->prev)
  123. {
  124. tmp->prev->next = *current;
  125. (*current)->prev = tmp->prev;
  126. }
  127. else
  128. {
  129. *head = *current;
  130. (*current)->prev = NULL;
  131. }
  132.  
  133. tmp->value[0] = '\0';
  134. free(tmp);
  135. return;
  136. }
  137.  
  138. prev = *current;
  139. current = &((*current)->next);
  140. }
  141. }
  142.  
  143. // Delete node after a given value
  144. void deleteAfter(struct Node **head, const char *value) {
  145. struct Node **current = head;
  146.  
  147. while (*current) {
  148. if (strcmp((*current)->value, value) == 0 && (*current)->next) {
  149. struct Node *tmp = (*current)->next;
  150.  
  151. (*current)->next = tmp->next;
  152.  
  153. if (tmp->next)
  154. tmp->next->prev = *current;
  155.  
  156. tmp->value[0] = '\0';
  157. free(tmp);
  158. return;
  159. }
  160.  
  161. current = &((*current)->next);
  162. }
  163. }
  164.  
  165. // Print list
  166. void printList(struct Node *head) {
  167. while (head) {
  168. printf("%s ", head->value);
  169. head = head->next;
  170. }
  171. printf("\n");
  172. }
  173.  
  174. // Free list
  175. void freeList(struct Node *head) {
  176. while (head) {
  177. struct Node *tmp = head;
  178. head = head->next;
  179. tmp->value[0] = '\0';
  180. free(tmp);
  181. }
  182.  
  183. // Free the string pool
  184. for (int i = 0; i < MAX_STRINGS; i++) {
  185. free(stringPool[i]);
  186. }
  187. }
  188.  
  189. // Example usage
  190. int main() {
  191. struct Node *head = NULL;
  192. initStringPool();
  193.  
  194. insert(&head, "apple");
  195. insert(&head, "banana");
  196. insert(&head, "cherry");
  197. insert(&head, "date");
  198.  
  199. printf("List: ");
  200. printList(head);
  201.  
  202. deleteValue(&head, "banana");
  203. printf("After deleteValue(banana): ");
  204. printList(head);
  205.  
  206. deleteBefore(&head, "cherry");
  207. printf("After deleteBefore(cherry): ");
  208. printList(head);
  209.  
  210. deleteAfter(&head, "apple");
  211. printf("After deleteAfter(apple): ");
  212. printList(head);
  213.  
  214. freeList(head);
  215.  
  216. return 0;
  217. }
  218.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
List: apple banana cherry date 
After deleteValue(banana): apple cherry date 
After deleteBefore(cherry): cherry date 
After deleteAfter(apple): cherry date