-
Notifications
You must be signed in to change notification settings - Fork 30
/
queue.h
124 lines (110 loc) · 2.58 KB
/
queue.h
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
/**
* @file queue.h
*
* @author hutusi ([email protected])
*
* @brief Double-ended queue (Deque).
*
* @date 2019-07-20
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#ifndef RETHINK_C_QUEUE_H
#define RETHINK_C_QUEUE_H
/**
* @brief The type of a value to be stored in a @ref Queue.
* (void *) can be changed to int, long, or other types if needed.
*/
typedef void *QueueValue;
#define QUEUE_NULL ((void *)0)
/**
* @brief Definition of a @ref QueueNode.
*
*/
typedef struct _QueueNode {
QueueValue data;
struct _QueueNode *prev;
struct _QueueNode *next;
} QueueNode;
/**
* @brief Definition of a @ref Queue.
*
*/
typedef struct _Queue {
struct _QueueNode *head;
struct _QueueNode *tail;
unsigned int length;
} Queue;
/**
* @brief Allcate a new Queue.
*
* @return Queue* The new Queue if success, otherwise return NULL.
*/
Queue *queue_new();
/**
* @brief Delete a Queue and free back memory.
*
* @param queue The Queue to delete.
*/
void queue_free(Queue *queue);
/**
* @brief Push a value to the head of a Queue.
*
* @param queue The Queue.
* @param data The value to push.
* @return int 0 if success.
*/
int queue_push_head(Queue *queue, QueueValue data);
/**
* @brief Pop the head value of a Queue.
*
* The head QueueNode of Queue has been popped and freed back memory.
*
* @param queue The Queue.
* @return QueueValue The popped value.
*/
QueueValue queue_pop_head(Queue *queue);
/**
* @brief Peek the head value of a Queue.
*
* The head QueueNode of Queue still exists in the Queue!
*
* @param queue The Queue.
* @return QueueValue The peeked value.
*/
QueueValue queue_peek_head(const Queue *queue);
/**
* @brief Push a value to the tail of a Queue.
*
* @param queue The Queue.
* @param data The value to push.
* @return int 0 if success.
*/
int queue_push_tail(Queue *queue, QueueValue data);
/**
* @brief Pop the head value of a Queue.
*
* The head QueueNode of Queue has been popped and freed back memory.
*
* @param queue The Queue.
* @return QueueValue The popped value.
*/
QueueValue queue_pop_tail(Queue *queue);
/**
* @brief Peek the head value of a Queue.
*
* The head QueueNode of Queue still exists in the Queue!
*
* @param queue The Queue.
* @return QueueValue The peeked value.
*/
QueueValue queue_peek_tail(const Queue *queue);
/**
* @brief Check if a Queue is empty.
*
* @param queue The Queue.
* @return int 0 if not empty, 1 if empty.
*/
int queue_is_empty(const Queue *queue);
#endif /* #ifndef RETHINK_C_QUEUE_H */