-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
176 lines (152 loc) · 3.2 KB
/
list.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "list.h"
#include<stdio.h>
#include<stdlib.h>
typedef struct node_{
void * content;
struct node_ * next;
struct node_ * prev;
}Node;
struct list_{
Node * begin;
Node * end;
int size;
size_t data_size;
};
List * create_list(size_t content_size){
List * new = (List*)allocate(sizeof(List));
new->begin=NULL;
new->end=NULL;
new->size=0;
new->data_size=content_size;
return new;
}
void erase_nodes(Node * begin){
Node * aux=begin;
while(begin !=NULL){
begin=begin->next;
deallocate((void*)&(aux->content));
deallocate((void*)&aux);
aux=begin;
}
}
void erase_list(List ** ptr){
erase_nodes((*ptr)->begin);
deallocate((void *)ptr);
ptr=NULL;
}
bool list_empty(List * list){
if(list!=NULL)
return list->size==0?true:false;
return true;
}
Node * allocate_node(void * item, size_t data_size){
Node * new = (Node *)allocate(sizeof(Node));
new->content = allocate(data_size);
//transfer the data from item to new->content byte to byte
//TODO entender essa merda melhor
int i;
for (i=0; i<data_size; i++){
*(char *)(new->content + i) = *(char *)(item + i);
}
return new;
}
bool list_push_back(List * list,void * item){
if(list!=NULL){
Node * new = allocate_node(item,list->data_size);
if(!list_empty(list))
list->end->next = new;
else
list->begin = new;
new->next=NULL;
new->prev=list->end;
list->end = new;
list->size++;
return true;
}
return false;
}
void * list_front(List * list){
if(list != NULL && !list_empty(list))
return list->begin->content;
return NULL;
}
void * list_back(List * list){
if(list != NULL && !list_empty(list))
return list->end->content;
return NULL;
}
void list_pop_front(List * list){
if(list!=NULL && !list_empty(list)){
Node * p = list->begin;
list->begin=list->begin->next;
if(p == list->end){
list->end = NULL;
}
else{
list->begin->prev=NULL;
}
deallocate(&(p->content));
deallocate((void **)&p);
list->size--;
}
}
void list_pop_back(List * list){
if(list!= NULL && !list_empty(list)){
Node * p = list->end;
list->end = list->end->prev;
if(p == list->begin){
list->begin = NULL;
}
else
list->end->next = NULL;
deallocate(&(p->content));
deallocate((void **)&p);
list->size--;
}
}
bool list_push_front(List * list, void * item){
if(list!=NULL){
Node * new = allocate_node(item,list->data_size);
if(!list_empty(list)){
new->next = list->begin;
list->begin->prev=new;
}
else{
list->end=new;
new->next=NULL;
}
new->prev=NULL;
list->begin = new;
list->size++;
return true;
}
return false;
}
bool list_insert(List* list,void * item,int index){
return true;
}
void list_print(List* list, void (*printer_function)(void *)){
if(list != NULL && !list_empty(list)){
Node * aux = list->begin;
while(aux != NULL){
(*printer_function)(aux->content);
printf("->");
aux=aux->next;
}
printf("\n");
return;
}
printf("Non initialized or empty listy!\n");
}
void list_reverse_print(List* list, void (*printer_function)(void *)){
if(list !=NULL && !list_empty(list)){
Node * aux = list->end;
while(aux != NULL){
(*printer_function)(aux->content);
aux=aux->prev;
}
printf("\n");
return;
}
printf("Non initialized or empty listy!\n");
}