-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforEach.c
55 lines (43 loc) · 1.11 KB
/
forEach.c
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
#include <stdio.h>
#include <stdlib.h>
//=============================linked List==============================//
struct node{
void* data;
struct node* next;
};
void set_int_value(struct node* node, int val){
int* integer = malloc(sizeof (int));
*integer = val;
node->data = integer;
}
struct node* create_basic_list(){
int node_size = sizeof (struct node);
struct node* element1 = malloc(node_size);
struct node* element2 = malloc(node_size);
struct node* element3 = malloc(node_size);
element1->next = element2;
element2->next = element3;
element3->next = NULL;
set_int_value(element1, 1);
set_int_value(element2, 2);
set_int_value(element3, 3);
return element1;
}
//=====================================forEach======================================//
void forEach(struct node* head, void(*fun)(void*)){
struct node* current = head;
while(current){
fun(current->data);
current = current->next;
}
}
void print_int(void* val){
printf("%d\n", *((int*)val));
}
void print(struct node* head){
forEach(head, print_int);
}
int main(void){
struct node* list = create_basic_list();
print(list);
}