-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.c
96 lines (84 loc) · 2.11 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DequeType;
typedef struct {
DequeType *values;
int maxSize;
int size;
int head;
int tail;
} Deque;
Deque *dequeCreate(int size)
{
Deque *deque = (Deque *)malloc(sizeof(Deque));
deque->maxSize = size;
deque->size = 0;
deque->head = 0;
deque->tail = deque->maxSize - 1;
deque->values = (DequeType *)malloc(sizeof(DequeType) * deque->maxSize);
return deque;
}
static inline int _formalize(int number, int max)
{
return (number + max) % max;
}
static inline void _dequeRealloc(Deque *deque)
{
DequeType *temp = NULL;
int firstHalf = deque->head;
int secondHalf = deque->maxSize - deque->head;
deque->maxSize += 100;
temp = deque->values;
deque->values = (DequeType *)malloc(sizeof(DequeType) * deque->maxSize);
/* bai change */
memcpy(deque->values, &temp[deque->head], secondHalf * sizeof(DequeType));
memcpy(&deque->values[secondHalf], temp, firstHalf * sizeof(DequeType));
deque->head = 0;
deque->tail = deque->size - 1;
free(temp);
}
static inline void dequeAddHead(Deque *deque, DequeType value)
{
if (deque->size >= deque->maxSize) {
_dequeRealloc(deque);
}
deque->head = _formalize(deque->head - 1, deque->maxSize);
deque->values[deque->head] = value;
deque->size++;
}
static inline void dequeAddTail(Deque *deque, DequeType value)
{
if (deque->size >= deque->maxSize) {
_dequeRealloc(deque);
}
deque->tail = _formalize(deque->tail + 1, deque->maxSize);
deque->values[deque->tail] = value;
deque->size++;
}
static inline DequeType dequePeekHead(Deque *deque)
{
return deque->values[deque->head];
}
static inline DequeType dequePeekTail(Deque *deque)
{
return deque->values[deque->tail];
}
static inline void dequePopHead(Deque *deque)
{
deque->head = _formalize(deque->head + 1, deque->maxSize);
deque->size--;
}
static inline void dequePopTail(Deque *deque)
{
deque->tail = _formalize(deque->tail - 1, deque->maxSize);
deque->size--;
}
static inline DequeType dequeGet(Deque *deque, int index)
{
return deque->values[(deque->head + index) % deque->maxSize];
}
static inline int getSize(Deque *deque)
{
return deque->size;
}