-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
66 lines (54 loc) · 1.08 KB
/
queue.c
File metadata and controls
66 lines (54 loc) · 1.08 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
#include "main.h"
struct queue * new_queue()
{
struct queue *q = malloc(sizeof(struct queue));
q->head = NULL;
q->tail = NULL;
pthread_mutex_init(&q->mutex, NULL);
return q;
}
struct node * new_node(char *string)
{
struct node *n = malloc(sizeof(struct node));
n->next = n->prev = NULL;
n->data = malloc((strlen(string)+1) * sizeof(char));
strcpy(n->data, string);
return n;
}
void push(struct queue *q, struct node *n)
{
if(q->head == NULL)
{
q->head = n;
q->tail = n;
}
else
{
n->next = q->head;
q->head->prev = n;
q->head = n;
}
}
struct node * pop(struct queue *q)
{
if(q->tail == NULL)
return NULL;
else
{
struct node *n = q->tail;
q->tail = q->tail->prev;
if(q->tail != NULL)
q->tail->next = NULL;
else
q->head = NULL;
return n;
}
}
int lock_queue(struct queue *q)
{
return pthread_mutex_lock(&q->mutex);
}
int unlock_queue(struct queue *q)
{
return pthread_mutex_unlock(&q->mutex);
}