-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistaEnc.c
144 lines (134 loc) · 4.07 KB
/
listaEnc.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
/*
* File: ListaEnc.c
* Author: Fábio Tramasoli (0619132)
* About: Lista Encadeada agnóstica a tipos.
*
*/
#include <stdlib.h>
#include <string.h>
#include "listaEnc.h"
int inicializaListaEnc(tListaEnc *f, int bytes) {
if(f!=NULL) {
f->head=f->tail=NULL;
f->bytes=bytes;
return LISTAENC_OPERACAO_OK;
f->size=0;
}
return LISTAENC_OPERACAO_ERR;
}
int finalizaListaEnc(tListaEnc *f) {
char *aux = malloc(f->bytes);
while(removerListaEnc(f,1,aux)==LISTAENC_OPERACAO_OK) {
}
free(aux);
return LISTAENC_OPERACAO_OK;
}
int vaziaListaEnc(tListaEnc *f) {
if (f->head==NULL)
return LISTAENC_VAZIA;
else
return LISTAENC_OPERACAO_OK;
}
int cheiaListaEnc(tListaEnc *f) {
return LISTAENC_OPERACAO_OK;
}
int inserirListaEnc(tListaEnc *f, int pos, void *valor) {
if(f!=NULL && pos>0) {
tListaEncItem *newVal = malloc(sizeof(tListaEncItem));
newVal->content = malloc(f->bytes);
memcpy(newVal->content, valor, f->bytes);
newVal->next=newVal->previous=NULL;
if(vaziaListaEnc(f)==LISTAENC_VAZIA)
f->head=f->tail=newVal;
else {
int count;
tListaEncItem *aux=f->head;
for (count=1;count<pos&&aux->next!=NULL;count++)
aux=aux->next;
if(pos==1) {
newVal->next=aux;
aux->previous=newVal;
f->head=newVal;
}
else {
newVal->previous=aux;
newVal->next=aux->next;
aux->next=newVal;
if(newVal->next!=NULL)
((tListaEncItem*)(newVal->next))->previous=newVal;
if(count==tamanhoListaEnc(f)+1)
f->tail=newVal;
}
}
return LISTAENC_OPERACAO_OK;
}
return LISTAENC_OPERACAO_ERR;
}
int removerListaEnc(tListaEnc *f, int pos, void *content) {
if (vaziaListaEnc(f)==LISTAENC_VAZIA)
return LISTAENC_VAZIA;
else {
int count;
tListaEncItem *aux=f->head,*tmp;
for (count=1;count<pos&&aux->next!=NULL;count++)
aux=aux->next;
if(aux==NULL)
return LISTAENC_OPERACAO_ERR;
memcpy(content,aux->content, f->bytes);
if(f->head==aux)
f->head=aux->next;
if(f->tail==aux)
f->tail=aux->previous;
if(aux->next!=NULL)
((tListaEncItem *)aux->next)->previous=aux->previous;
if(aux->previous!=NULL)
((tListaEncItem *)aux->previous)->next=aux->next;
free(aux->content);
free(aux);
return LISTAENC_OPERACAO_OK;
}
return LISTAENC_OPERACAO_ERR;
}
int posicaoListaEnc(tListaEnc *f, void *valor) {
if (vaziaListaEnc(f)==LISTAENC_VAZIA)
return LISTAENC_VAZIA;
else {
int count=0,tamanho=tamanhoListaEnc(f), diff=1;
tListaEncItem *aux=f->head;
while(count<tamanho && diff && aux!=NULL) {
diff=memcmp(valor,aux->content,f->bytes);
aux=aux->next;
count++;
}
if(aux==NULL && diff!=0)
return LISTAENC_OPERACAO_ERR;
//cagada!
//memcpy(valor,&count, f->bytes);
return count;
}
return LISTAENC_OPERACAO_ERR;
}
int elementoListaEnc(tListaEnc *f, int pos, void *valor) {
if (vaziaListaEnc(f)==LISTAENC_VAZIA)
return LISTAENC_VAZIA;
else {
tListaEncItem *aux=f->head;
int count;
for (count=1;count<pos&&aux!=NULL;count++)
aux=aux->next;
if(aux==NULL)
return LISTAENC_OPERACAO_ERR;
memcpy(valor,aux->content, f->bytes);
return LISTAENC_OPERACAO_OK;
}
return LISTAENC_OPERACAO_ERR;
}
int tamanhoListaEnc(tListaEnc * f){
if(vaziaListaEnc(f)==LISTAENC_VAZIA)
return 0;
unsigned int retorno=0;
tListaEncItem *go;
for(go=f->head;go!=NULL;go=go->next)
retorno++;
return retorno;
}