-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
168 lines (137 loc) · 3.4 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "queue.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
node* create_node(void* data, size_t memsize) {
node* new_node = (node*)malloc(sizeof(node));
if (new_node == NULL)
return NULL;
new_node->data = malloc(memsize);
if (new_node->data == NULL) {
free(new_node);
return NULL;
}
memcpy(new_node->data, data, memsize);
new_node->next = NULL;
return new_node;
}
queue* create_queue(size_t memsize) {
queue* q = (queue*)malloc(sizeof(queue));
if (q == NULL)
return NULL;
q->memsize = memsize;
q->size = 0;
q->head = NULL;
q->tail = NULL;
return q;
}
queue* enqueue(queue* q, void* data) {
pthread_mutex_lock(q->mutex);
if (q == NULL)
return NULL;
node* new_node = create_node(data, q->memsize);
if (new_node == NULL) {
return NULL;
}
if (q->size == 0) {
q->head = q->tail = new_node;
} else {
q->tail->next = new_node;
q->tail = new_node;
}
q->size++;
pthread_mutex_unlock(q->mutex);
return q;
}
queue* dequeue(queue* q, void* data) {
pthread_mutex_lock(q->mutex);
if (q == NULL)
return NULL;
if (q->size == 0)
return NULL;
node* to_delete = q->head;
if (q->size == 1) {
memcpy(data, to_delete->data, q->memsize);
free(to_delete->data);
free(to_delete);
q->head = q->tail = NULL;
q->size--;
pthread_mutex_unlock(q->mutex);
return q;
}
q->head = q->head->next;
memcpy(data, to_delete->data, q->memsize);
free(to_delete->data);
free(to_delete);
q->size--;
pthread_mutex_unlock(q->mutex);
return q;
}
void front(queue* q, void* data) {
if (q == NULL)
return;
if (q->size == 0)
return;
memcpy(data, q->head->data, q->memsize);
}
queue* clear_queue(queue* q) {
pthread_mutex_lock(q->mutex);
if (q == NULL)
return NULL;
while (!is_empty(q)) {
node* temp = q->head;
q->head = q->head->next;
free(temp->data);
free(temp);
q->size--;
}
pthread_mutex_unlock(q->mutex);
return q;
}
size_t get_size(queue* q) {
pthread_mutex_lock(q->mutex);
if (q == NULL) {
return 0;
}
pthread_mutex_unlock(q->mutex);
return q->size;
}
bool is_empty(queue* q) {
return q->size == 0 ? true : false;
}
size_t get_memsize(queue* q) {
if (q == NULL) {
return 0;
}
return q->memsize;
}
queue* copy_queue(queue* src) {
pthread_mutex_lock(src->mutex);
if (src == NULL) {
return NULL;
}
queue* new_queue = create_queue(src->memsize);
if (new_queue == NULL) {
return NULL;
}
// Iterate through original queue and copy nodes
node* current = src->head;
node* prev_new_node = NULL;
while (current != NULL) {
enqueue(new_queue, current->data);
current = current->next;
}
pthread_mutex_unlock(src->mutex);
return new_queue;
}
void destroy_queue(queue* q) {
clear_queue(q);
free(q);
}
void display_queue(queue* q, displayer display) {
node* current = q->head;
while (current) {
display(current->data);
current = current->next;
}
}