Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct slist_node
- {
- void *data;
- struct slist_node *next;
- };
- typedef void (*datafree_t)(void *data);
- struct slist
- {
- struct slist_node *head;
- datafree_t datafree;
- };
- typedef void (*node_visit_t)(struct slist_node *node);
- typedef int (*datacmp_t)(void *a, void *b);
- void slist_walk(struct slist *list, node_visit_t visit)
- {
- struct slist_node *node = NULL;
- for (node = list->head; node; node = node->next)
- {
- visit(node);
- }
- }
- void slist_print_strings(struct slist *list)
- {
- slist_walk(list, snode_print_string_data);
- }
- struct slist_node *slist_node_new(void *data)
- {
- struct slist_node *new = malloc(sizeof(*new));
- // printf("slist_node_new() %p\n", new);
- new->data = data;
- new->next = NULL;
- return new;
- }
- void slist_add_left(struct slist *list, struct slist_node *node)
- {
- node->next = list->head;
- list->head = node;
- return;
- }
- void slist_node_free(struct slist_node *node, datafree_t datafree)
- {
- // printf("slist_node_free() %p\n", node);
- if (datafree)
- {
- datafree(node->data);
- }
- free(node);
- }
- void slist_delete_node(struct slist *list, void *key, datacmp_t compare)
- {
- if (list->head)
- {
- if (compare(list->head->data, key) == 0)
- {
- struct slist_node *node = list->head;
- list->head = list->head->next;
- slist_node_free(node, list->datafree);
- }
- else
- {
- struct slist_node *prev = list->head;
- struct slist_node *current = prev->next;
- while (current)
- {
- if (compare(current->data, key) == 0)
- {
- prev->next = current->next;
- slist_node_free(current, list->datafree);
- return;
- }
- prev = current;
- current = current->next;
- }
- }
- }
- }
- void slist_free(struct slist *list)
- {
- struct slist_node *next;
- struct slist_node *current = list->head;
- while (current)
- {
- next = current->next;
- slist_node_free(current, list->datafree);
- current = next;
- }
- }
- struct slist_node *slist_find_node(struct slist *list, void *key, datacmp_t compare)
- {
- struct slist_node *node;
- for (node = list->head; node; node = node->next)
- {
- if (compare(key, node->data) == 0)
- {
- return node;
- }
- }
- return NULL;
- }
- void snode_print_string_data(struct slist_node *node)
- {
- printf("data = %s, \n", node->data);
- }
- char *console_read_string(void)
- {
- char *str = malloc(100);
- printf("malloc() str = %p\n", str);
- scanf("%100s", str);
- fflush(stdin);
- return str;
- }
- void free_string(char *str)
- {
- printf("free_string() %p\n", str);
- free(str);
- }
- int main(void)
- {
- fflush(stdin);
- struct slist list = {0};
- list.datafree = free_string;
- for (int i = 0; i < 5; i++)
- {
- slist_add_left(&list, slist_node_new(console_read_string()));
- }
- struct slist_node *three = slist_find_node(&list, "three", strcmp);
- if (three)
- {
- printf("Found!\n");
- }
- else
- {
- printf("Not found!\n");
- }
- printf("Deleting 'one'!\n");
- slist_delete_node(&list, "one", strcmp);
- slist_print_strings(&list);
- printf("Deleting 'three'!\n");
- slist_delete_node(&list, "three", strcmp);
- slist_print_strings(&list);
- slist_free(&list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement