Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct s_list
- {
- int content;
- struct s_list *next;
- } t_list;
- t_list *ft_lstnew(int data)
- {
- t_list *new;
- new = (t_list *)malloc(sizeof(t_list));
- if (new == NULL)
- return (NULL);
- new->content = data;
- new->next = NULL;
- return (new);
- }
- t_list *ft_lstlast(t_list *lst)
- {
- if (lst == NULL)
- return (NULL);
- while (lst->next != NULL)
- lst = lst->next;
- return (lst);
- }
- void ft_lstdelone(t_list *lst)
- {
- if (lst == NULL)
- return ;
- free(lst);
- lst = NULL;
- }
- int ft_lstsize(t_list *lst)
- {
- int size;
- size = 0;
- while (lst != NULL)
- {
- lst = lst->next;
- size++;
- }
- return (size);
- }
- void ft_lstadd_back(t_list **lst, t_list *new)
- {
- t_list *last;
- if (lst)
- {
- if (!*lst)
- *lst = new;
- else
- {
- last = ft_lstlast(*lst);
- last->next = new;
- }
- }
- }
- void ft_lstadd_front(t_list **lst, t_list *new)
- {
- if (lst == NULL || new == NULL)
- return ;
- new->next = *lst;
- *lst = new;
- }
- void ft_lstclear(t_list **lst)
- {
- t_list *curr;
- t_list *next;
- curr = *lst;
- while (curr)
- {
- next = curr->next;
- ft_lstdelone(curr);
- curr = next;
- }
- *lst = NULL;
- }
- void ft_lstiter(t_list *lst, void (*f)(int))
- {
- if (lst == NULL || f == NULL)
- return ;
- while (lst)
- {
- f(lst->content);
- lst = lst->next;
- }
- }
- void print_list( int data)
- {
- printf("data: %d \n",data);
- }
- int main()
- {
- t_list *root = ft_lstnew(0);
- t_list *n1 = ft_lstnew(1);
- t_list *n2 = ft_lstnew(2);
- t_list *n3 = ft_lstnew(3);
- t_list *n4 = ft_lstnew(4);
- t_list *n5 = ft_lstnew(10);
- ft_lstadd_front(&root,n1);
- ft_lstadd_front(&root,n2);
- ft_lstadd_front(&root,n3);
- ft_lstadd_front(&root,n4);
- ft_lstadd_back(&root,n5);
- ft_lstiter(root,print_list);
- ft_lstclear(&root);
- // printf("\n total ; %d \n",number);
- }
Advertisement
Add Comment
Please, Sign In to add comment