-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
143 lines (116 loc) · 3.58 KB
/
queue.c
File metadata and controls
143 lines (116 loc) · 3.58 KB
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
// Copyright (C) 2024 Aleksei Rogov <alekzzzr@gmail.com>. All rights reserved.
#include <string.h>
#include <stdlib.h>
#include "queue.h"
int mt_queue_push(void **queue, void *object, size_t object_size) {
if(object == NULL || object_size == 0 || queue == NULL) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
struct MT_QUEUE *mt_queue = malloc(sizeof(struct MT_QUEUE) + object_size);
if(mt_queue == NULL) {
return QUEUE_ALLOC_ERROR;
}
mt_queue->last = mt_queue;
mt_queue->next = NULL;
mt_queue->object_size = object_size;
mt_queue->queue_length = 1;
memcpy((void *)mt_queue + sizeof(struct MT_QUEUE), object, object_size);
*queue = mt_queue;
return QUEUE_SUCCESS;
}
else {
struct MT_QUEUE *mt_queue = *queue;
mt_queue->last->next = malloc(sizeof(struct MT_QUEUE) + object_size);
if(mt_queue->last->next == NULL) {
return QUEUE_ALLOC_ERROR;
}
uint32_t length = mt_queue->last->queue_length + 1;
mt_queue->last->next->next = NULL;
mt_queue->last->next->object_size = object_size;
mt_queue->last->next->last = NULL;
mt_queue->last = mt_queue->last->next;
mt_queue->last->queue_length = length;
memcpy((void *)mt_queue->last + sizeof(struct MT_QUEUE), object, object_size);
return QUEUE_SUCCESS;
}
}
int mt_queue_pop(void **queue, void *object, size_t object_size) {
if(queue == NULL || object == NULL || object_size == 0) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
return QUEUE_IS_EMPTY;
}
struct MT_QUEUE *mt_queue = *queue;
if(mt_queue->object_size != object_size) {
return QUEUE_SIZE_ERROR;
}
uint32_t length = mt_queue->last->queue_length - 1;
memcpy(object, (*queue) + sizeof(struct MT_QUEUE), object_size);
struct MT_QUEUE *next = mt_queue->next;
struct MT_QUEUE *last = mt_queue->last;
free(*queue);
if(next == NULL) {
*queue = NULL;
return QUEUE_SUCCESS;
}
next->last = last;
*queue = next;
last->queue_length = length;
return QUEUE_SUCCESS;
}
int mt_queue_front(void **queue, void *object, size_t object_size) {
if(queue == NULL) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
return QUEUE_IS_EMPTY;
}
struct MT_QUEUE *mt_qeue = *queue;
if(mt_qeue->object_size != object_size) {
return QUEUE_SIZE_ERROR;
}
memcpy(object, (*queue) + sizeof(struct MT_QUEUE), object_size);
return QUEUE_SUCCESS;
}
int mt_queue_back(void **queue, void *object, size_t object_size) {
if(queue == NULL) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
return QUEUE_IS_EMPTY;
}
struct MT_QUEUE *mt_qeue = *queue;
if(mt_qeue->object_size != object_size) {
return QUEUE_SIZE_ERROR;
}
memcpy(object, ((void *)mt_qeue->last) + sizeof(struct MT_QUEUE), object_size);
return QUEUE_SUCCESS;
}
int mt_queue_length(void **queue) {
if(queue == NULL) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
return 0;
}
struct MT_QUEUE *mt_qeue = *queue;
return mt_qeue->last->queue_length;
}
int mt_qeue_destroy(void **queue) {
if(queue == NULL) {
return QUEUE_PARAM_ERROR;
}
if(*queue == NULL) {
return QUEUE_IS_EMPTY;
}
struct MT_QUEUE *mt_queue = *queue;
while(mt_queue) {
void *ptr = mt_queue->next;
free(mt_queue);
mt_queue = ptr;
}
*queue = NULL;
return QUEUE_SUCCESS;
}