Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct s_llist
- {
- size_t size;
- struct s_llnode *first;
- struct s_llnode *last;
- } t_llist;
- typedef struct s_llnode
- {
- void *val;
- size_t val_size;
- struct s_llnode *next;
- } t_llnode;
- t_llist *array_to_llist(void *array, size_t size, size_t val_size);
- void llist_add_last(t_llist *list, const void *val, size_t val_size);
- t_llnode *llist_create_node(const void *val, size_t val_size);
- int main(void)
- {
- char tab[5][2] = {"9", "2", "3", "4", "5"};
- t_llist *l1 = array_to_llist((void*)tab, 5, sizeof(char *));
- t_llnode *cur = l1->first;
- printf("-> ");
- while (cur)
- {
- printf("|%s|", (char *)(cur->val));
- cur = cur->next;
- }
- printf("\n");
- return (0);
- }
- t_llist *array_to_llist(void *array, size_t size, size_t val_size)
- {
- t_llist *list;
- size_t i;
- list = (t_llist *)malloc(sizeof(t_llist));
- list->size = 0;
- list->first = NULL;
- list->last = NULL;
- i = 0;
- while (size > 0)
- {
- llist_add_last(list, array + i, val_size);
- i += val_size;
- size--;
- }
- return (list);
- }
- void llist_add_last(t_llist *list, const void *val, size_t val_size)
- {
- t_llnode *node;
- node = llist_create_node(val, val_size);
- if (list->size > 0)
- list->last->next = node;
- else
- list->first = node;
- list->last = node;
- list->size++;
- }
- t_llnode *llist_create_node(const void *val, size_t val_size)
- {
- t_llnode *new;
- new = (t_llnode *)malloc(sizeof(t_llnode));
- new->val = (void *)malloc(val_size);
- memcpy(new->val, val, val_size);
- new->val_size = val_size;
- new->next = NULL;
- return (new);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement