-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.c
152 lines (140 loc) · 3.01 KB
/
deque.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
#include <stdlib.h>
#include <stdbool.h>
#include "deque.h"
error_t *initDeque(Deque *deque)
{
deque = (Deque *)malloc(sizeof(Deque));
if (deque == NULL)
{
return E_ALLOCATION;
}
deque->buffer = (void **)malloc(sizeof(void *) * DEFAULT_CAPACITY);
if (deque->buffer == NULL)
{
return E_ALLOCATION;
}
deque->front = -1;
deque->back = -1;
deque->capacity = DEFAULT_CAPACITY;
deque->size = 0;
return E_SUCCESS;
}
bool isEmpty(Deque *deque)
{
return deque->size == 0;
}
bool isFull(Deque *deque)
{
return deque->size == deque->capacity;
}
error_t __resize(Deque *deque)
{
int32_t capacity = deque->capacity * 2;
void **buffer = (void **)malloc(sizeof(void *) * capacity);
if (buffer == NULL)
{
return E_ALLOCATION;
}
int32_t oldIndex = deque->front;
int32_t newDequeIndex = 0;
while (newDequeIndex < deque->size)
{
buffer[newDequeIndex] = deque->buffer[oldIndex];
oldIndex = (oldIndex + 1) % deque->capacity;
newDequeIndex++;
}
free(deque->buffer);
deque->buffer = buffer;
deque->front = 0;
deque->back = deque->size - 1;
deque->capacity = capacity;
}
void pushFront(Deque *deque, void *insert_data)
{
if (isFull(deque))
{
__resize(deque);
}
if (isEmpty(deque))
{
deque->front = deque->back = 0;
}
else
{
deque->front = (deque->front - 1 + deque->capacity) % deque->capacity;
}
deque->buffer[deque->front] = insert_data;
deque->size++;
}
void pushBack(Deque *deque, void *insert_data)
{
if (isFull(deque))
{
__resize(deque);
}
if (isEmpty(deque))
{
deque->front = deque->back = 0;
}
else
{
deque->back = (deque->back + 1) % deque->capacity;
}
deque->buffer[deque->back] = insert_data;
deque->size++;
}
error_t popFront(Deque *deque, void *return_data)
{
if (isEmpty(deque))
{
return E_EMPTY_DEQUE;
}
void *removed = deque->buffer[deque->front];
if (deque->front == deque->back)
{
deque->front = deque->back = -1;
}
else
{
deque->front = (deque->front + 1) % deque->capacity;
}
deque->size--;
return E_SUCCESS;
}
error_t popBack(Deque *deque, void *return_data)
{
if (isEmpty(deque))
{
return E_EMPTY_DEQUE;
}
void *removed = deque->buffer[deque->back];
if (deque->front == deque->back)
{
deque->front = deque->back = -1;
}
else
{
deque->back = (deque->back - 1 + deque->capacity) % deque->capacity;
}
deque->size--;
return E_SUCCESS;
}
void destroyDeque(Deque *deque)
{
if (deque)
{
free(deque->buffer);
free(deque);
}
}
char *getErrorDescription(error_t error_code)
{
for (size_t i = 0; i < (sizeof(errordesc) / sizeof(errordesc[0])); i++)
{
if (errordesc[i].code == error_code)
{
return errordesc[i].description;
}
}
return "Unknown error";
}