-
Notifications
You must be signed in to change notification settings - Fork 0
/
bicola.c
135 lines (115 loc) · 3.08 KB
/
bicola.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
#include <stdio.h>
#include <stdlib.h>
#include "bicola.h"
typedef struct Bicola Bicola;
Bicola* crearBicola(int talla){
Bicola* new = (Bicola*)malloc(sizeof(Bicola));
new->array = (void**)malloc(sizeof(void*) * talla);
new->head = 0;
new->tail = 0;
new->size = talla;
return new;
}
void pushBack (void* dato, Bicola* bicola){
if(EsVacia(bicola) == 1){
bicola->array[bicola->head] = dato;
}
else{
if(bicola->tail == bicola->size -1){
bicola->array[bicola->tail] = dato;
bicola->tail = 0;
}
else{
bicola->array[bicola->tail +1] = dato;
bicola->tail += 1;
}
}
// comprobar si esta llena
if (bicola->head == 0){
if (bicola->tail +1 == bicola->size) aumentar(bicola);
}
else{
if (bicola->tail +1 == bicola->head) aumentar(bicola);
}
}
void pushFront(void* dato, Bicola* bicola){
if(EsVacia(bicola) == 1){
bicola->array[bicola->head] = dato;
}
else{
if (bicola->head == 0){
bicola->head = bicola->size -1;
bicola->array[bicola->head] = dato;
}
else{
bicola->array[bicola->head -1] = dato;
bicola->head -= 1;
}
}
if ((bicola->tail +1 == bicola->head) || (bicola->tail +1 == bicola->size && bicola->head == 0) ) aumentar(bicola);
}
void* popFront(Bicola* bicola){
if (EsVacia(bicola) == 1) return NULL;
void* dato = bicola->array[bicola->head];
bicola->array[bicola->head] = NULL;
if (bicola->head == bicola->size -1 || bicola->array[bicola->head + 1] == NULL) bicola->head = 0;
else bicola->head += 1;
return dato;
}
void* popBack(Bicola* bicola){
if (EsVacia(bicola) == 1) return NULL;
void* dato = bicola->array[bicola->tail];
bicola->array[bicola->tail] = NULL;
if(bicola->tail == 0) bicola->tail = bicola->size -1;
else bicola->tail -= 1;
return dato;
}
void aumentar(Bicola* bicola){
bicola->size *= 2;
bicola->array = (void**)realloc(bicola->array ,bicola->size * sizeof(void*));
//ordenar
if (bicola->tail < bicola->head){
void** auxArray = (void**)malloc(sizeof(void*) * (bicola->size /2 - bicola->head));
int pos = 0;
while(pos < bicola->size /2 - bicola->head){
auxArray[pos] = bicola->array[bicola->head + pos];
bicola->array[bicola->head + pos] = NULL;
pos++;
}
bicola->head = bicola->size - (bicola->size /2 - bicola->head);
pos = 0;
while(pos < bicola->size - bicola->head){
bicola->array[bicola->head + pos] = auxArray[pos];
pos++;
}
free(auxArray);
}
}
void* get_i (int pos, Bicola* bicola){
if(bicola->head + pos -1 < bicola->size ){
return bicola->array[bicola->head + pos -1];
}
else{
return bicola->array[bicola->head + pos -1 - bicola->size] ;
}
}
void mostrar(Bicola* bicola){
int pos = 0;
while (pos < bicola->size){
if (bicola->array[pos] != NULL){
int* dato = bicola->array[pos];
printf("/%d", *dato);
}
else{
printf("/#");
}
pos++;
}
printf("/");
}
int EsVacia(Bicola* bicola){
if (bicola->array[bicola->head] == NULL && bicola->array[bicola->tail] == NULL) {
return 1;
}
else return 0;
}