-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_oo.c
109 lines (82 loc) · 2.16 KB
/
linked_list_oo.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
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
108
109
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define linkedList_malloc(generic_ptr) \
(generic_ptr *)malloc(sizeof(generic_ptr));
typedef struct self_node
{
int this_info;
struct self_node *next;
} Node;
typedef struct self_queue
{
Node *head;
// Methods
void (*push_head)(struct self_queue *, const int);
void (*push_rear)(struct self_queue *, const int);
bool (*pop_head)(Node *);
bool (*pop_rear)(Node *);
bool (*remove_who)(Node *, const int);
bool (*search_who)(Node *, const int);
void (*show)(Node *);
int (*size)(Node *);
} generic_linkedList;
void linkedList_push_rear(generic_linkedList *self, const int info)
{
Node *node_next = linkedList_malloc(Node);
node_next->this_info = info;
node_next->next = NULL;
if (self->head != NULL)
{
Node *tmp_ptr = self->head;
while (tmp_ptr->next != NULL)
tmp_ptr = tmp_ptr->next;
tmp_ptr->next = node_next;
}
else
self->head = node_next;
}
void linkedList_push_head(generic_linkedList *self, const int info)
{
Node *node_next = linkedList_malloc(Node);
node_next->this_info = info;
node_next->next = self->head;
self->head = node_next;
}
void linkedList_show(Node *self)
{
if (self != NULL)
{
printf("%d\n", self->this_info);
linkedList_show(self->next);
}
}
int linkedList_size(Node *self)
{
if (self != NULL)
return linkedList_size(self->next) + 1;
return 0;
}
generic_linkedList *initialize_linkedList()
{
generic_linkedList *tmp = linkedList_malloc(generic_linkedList);
tmp->head = NULL;
// Iniciando os métodos de acesso
tmp->push_head = linkedList_push_head;
tmp->push_rear = linkedList_push_rear;
tmp->show = linkedList_show;
tmp->size = linkedList_size;
return tmp;
}
int main(int argc, char **argv)
{
generic_linkedList *ptr = initialize_linkedList();
for (int generic_value = 0; generic_value < 5; generic_value++)
{
ptr->push_head(ptr, generic_value);
ptr->push_rear(ptr, generic_value);
}
ptr->show(ptr->head);
printf("size_q: %d\n", ptr->size(ptr->head));
return 0;
}