forked from ChenyuGao/Crawler-Parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
61 lines (54 loc) · 976 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
//存储节点的队列
Queue* que_init()
{
Queue* q;
if ((q = malloc(sizeof(Queue))) == NULL)
return NULL;
memset(q, 0, sizeof(Queue));
q->head = 0;
q->tail = 0;
return q;
}
//队列判空
int queue_empty(Queue* q)
{
if (q->head == q->tail)
return 1;
return 0;
}
//入队,成功返回1,否则返回0
int enqueue(Queue* q, char* node)
{
if ((q->head - q->tail + maxq) % maxq == 1)
return 0;
else
{
q->myqueue[q->tail] = node;
q->tail = (q->tail + 1) % maxq;
return 1;
}
}
//出队,返回出队节点或NULL(队空)
char* dequeue(Queue* q)
{
if (q->head == q->tail)
return NULL;
else
{
int head = q->head;
q->head = (head + 1) % maxq;
return q->myqueue[head];
}
}
// 返回队列长度
int queue_length(const Queue* const q)
{
if( q == NULL)//当传入参数为指针,必须判空
exit(-1);//进程异常结束
unsigned int length = q->tail - q->head;
return length;
}