-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_lstlast_bonus.c
More file actions
88 lines (75 loc) · 2 KB
/
Copy pathft_lstlast_bonus.c
File metadata and controls
88 lines (75 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 18:37:09 by simarcha #+# #+# */
/* Updated: 2024/10/05 23:15:43 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//we just want to return the last node of the list
t_list *ft_lstlast(t_list *lst)
{
t_list *ptr;
ptr = lst;
if (!lst)
return (NULL);
while (ptr->next != NULL)
ptr = ptr->next;
return (ptr);
}
/*
//1. we want to create a function that's create nodes
//2. we want to create a function that link the second node with the first one
//3. we create the main
t_list *ft_lstnew(void *content)
{
t_list *lst;
lst = malloc(sizeof(t_list));
if (!lst)
return (NULL);
lst->content = content;
lst->next = NULL;
return (lst);
}
void ft_lstaddfront(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}
#include <stdio.h>
void ft_lstprint(t_list *lst)
{
while (lst != NULL)
{
printf("%s\n", (char *)lst->content);
lst = lst->next;
}
}
int main(void)
{
t_list *lst;
t_list *node1;
t_list *node2;
t_list *tmp;
node1 = ft_lstnew("a");
node2 = ft_lstnew("b");
ft_lstaddfront(&node1, node2);
lst = node2;
ft_lstprint(lst);
printf("\n");
tmp = ft_lstlast(lst);
printf("%s\n", (char *)tmp->content);
while (lst != NULL)
{
tmp = lst;
lst = lst->next;
free(tmp);
}
free(lst);
return (0);
}
*/