-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
118 lines (95 loc) · 2.78 KB
/
queue.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
#include "queue.h"
/* creates a new queue with a given size */
queue_t* create_queue(int capacity){
queue_t *queue = malloc(sizeof(queue_t));
queue->capacity = capacity;
queue->size = 0;
queue->array = malloc(capacity * sizeof(void*));
queue->end = queue->array;
queue->start = queue->array;
return queue;
}
void realloc_queue(queue_t *queue, int new_capaity){
//printf("OLD SIZE: %d\nNEW SIZE: %d\n",queue->capacity, new_capaity);
void** new_arr = malloc(new_capaity * sizeof(void*));
for(int i = 0; i < queue->size; i++){
if( (queue->start + i) >= (queue->array + queue->capacity) ){
new_arr[i] = queue->array[i - ((queue->array + queue->capacity) - queue->start)];
}
else{
new_arr[i] = queue->start[i];
}
}
free(queue->array);
queue->capacity = new_capaity;
queue->array = new_arr;
queue->start = queue->array;
queue->end = queue->array + queue->size;
}
/* deletes the queue and all allocated memory */
void delete_queue(queue_t *queue){
free(queue->array);
free(queue);
}
/*
* inserts a reference to the element into the queue
* returns: true on success; false otherwise
*/
bool push_to_queue(queue_t *queue, void *data){
if(queue->size == queue->capacity){
realloc_queue(queue,queue->capacity*2);
}
*queue->end = data;
if(queue->end == (queue->array + (queue->capacity-1)) ){
queue->end = queue->array;
}
else{
queue->end++;
}
queue->size++;
return true;
}
/*
* gets the first element from the queue and removes it from the queue
* returns: the first element on success; NULL otherwise
*/
void* pop_from_queue(queue_t *queue){
if(queue->size == 0){
return NULL;
}
if(queue->size < ((2 *queue->capacity)/3) && queue->capacity > 5){
realloc_queue(queue,(2 *queue->capacity)/3);
}
void* ret = *queue->start;
if(queue->start+1 >= (queue->array + queue->capacity)){
queue->start = queue->array;
}
else{
queue->start++;
}
queue->size--;
return ret;
}
/*
* gets idx-th element from the queue, i.e., it returns the element that
* will be popped after idx calls of the pop_from_queue()
* returns: the idx-th element on success; NULL otherwise
*/
void* get_from_queue(queue_t *queue, int idx){
if(idx >= queue->size || idx < 0){
return NULL;
};
void** ret;
if( (queue->start + idx) >= (queue->array + queue->capacity) ){
idx-= ((queue->array + queue->capacity) - queue->start);
ret = queue->array+idx;
}
else{
ret = queue->start+idx;
}
return *ret;
}
/* gets number of stored elements */
int get_queue_size(queue_t *queue){
return queue->size;
}