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