-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlists.c
More file actions
107 lines (90 loc) · 1.8 KB
/
lists.c
File metadata and controls
107 lines (90 loc) · 1.8 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include "ft_list.h"
t_list *create_list(int data)
{
t_list *list;
list = malloc(sizeof(t_list));
if (list != NULL)
{
list->next = NULL;
list->data = data;
}
return (list);
}
void push_front(t_list **begin, int data)
{
t_list *new;
new = create_list(data);
new->next = (*begin);
(*begin) = new;
}
void print_list(t_list **begin)
{
t_list *tmp;
tmp = *begin;
while (tmp)
{
printf("%d ", tmp->data);
tmp = tmp->next;
}
printf("\n");
}
void delete_if(t_list **head, int data_ref)
{
t_list *tmp;
t_list *prev;
tmp = *head;
while (tmp->data == data_ref)
{
prev = tmp;
tmp = tmp->next;
*head = tmp;
free(prev);
}
while (tmp && head && tmp->next)
{
if (tmp->next->data == data_ref)
{
prev = tmp->next;
tmp->next = tmp->next->next;
free(prev);
}
tmp = tmp->next;
}
}
void push_back(t_list *head, int value)
{
t_list *last;
t_list *new;
while(head->next)
head = head->next;
last = head;
new = malloc(sizeof(t_list));
new->next = NULL;
new->data = value;
last->next = new;
}
int main(void)
{
t_list *list;
t_list **begin;
int data;
begin = &list;
data = 5;
list = create_list(data);
push_front(begin, 10);
push_front(begin, 17);
push_front(begin, 2);
push_front(begin, 9);
push_front(begin, 4);
push_back(list, 11);
push_back(list, 13);
push_front(begin, 6);
push_front(begin, 6);
push_front(begin, 6);
print_list(begin);
delete_if(begin, 6);
print_list(begin);
return (0);
}