-
Notifications
You must be signed in to change notification settings - Fork 0
/
myqueue.c
128 lines (124 loc) · 5.4 KB
/
myqueue.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
//------------------------------------------------------------------------
// File Name: myqueue.c
// Author: Dragon
// mail: [[email protected]](mailto:[email protected])
// Created Time: Wed 27 Sep 2023 09:06:03 CST
// Description:
//------------------------------------------------------------------------
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define Queue(T) \
typedef struct queue_##T { \
T *val; \
int head; \
int tail; \
int size; \
int (*maxsize)(struct queue_##T * queue); \
int (*getsize)(struct queue_##T * queue); \
int (*isempty)(struct queue_##T * queue); \
int (*isfull)(struct queue_##T * queue); \
void (*push)(struct queue_##T * queue, T val); \
void (*pop)(struct queue_##T * queue); \
T(*top) \
(struct queue_##T * queue); \
void (*clear)(struct queue_##T * queue); \
} queue_##T;
#define queue(T) queue_##T
#define Maxsize(T) \
int maxsize_##T(queue_##T *queue) { return queue->size; }
#define Getsize(T) \
int getsize_##T(queue_##T *queue) { \
int size = queue->tail - queue->head; \
while (size < 0) size += queue->size; \
while (size > queue->size) size -= queue->size; \
return size; \
}
#define Isempty(T) \
int isempty_##T(queue_##T *queue) { \
return queue->head == queue->tail; \
}
#define Isfull(T) \
int isfull_##T(queue_##T *queue) { \
return queue->tail == (queue->head - 1 >= 0 ? queue->head - 1 : queue->head - 1 + queue->size); \
}
#define Push(T) \
void push_##T(queue_##T *queue, T val) { \
if (queue->tail == (queue->head - 1 >= 0 ? queue->head - 1 : queue->head - 1 + queue->size)) return; \
queue->val[queue->tail] = val; \
queue->tail += 1; \
queue->tail %= queue->size; \
}
#define Pop(T) \
void pop_##T(queue_##T *queue) { \
if (queue->head == queue->tail) { \
printf("this queue is empty"); \
return; \
} \
queue->head++; \
queue->head %= queue->size; \
}
#define Top(T) \
T top_##T(queue_##T *queue) { \
if (queue->head == queue->tail) { \
printf("this queue is empty"); \
return (T) 0; \
} \
return queue->val[queue->head]; \
}
#define Clear(T) \
void clear_##T(queue_##T *queue) { \
queue->head = 0; \
queue->tail = 0; \
}
#define NewQueue(T) \
queue_##T *newqueue_##T(int size) { \
queue_##T *queue = (queue_##T *) malloc(sizeof(queue_##T)); \
queue->val = (T *) malloc(sizeof(T) * size); \
queue->size = size; \
queue->head = 0; \
queue->tail = 0; \
queue->maxsize = maxsize_##T; \
queue->getsize = getsize_##T; \
queue->isempty = isempty_##T; \
queue->isfull = isfull_##T; \
queue->push = push_##T; \
queue->pop = pop_##T; \
queue->top = top_##T; \
queue->clear = clear_##T; \
return queue; \
}
#define newqueue(T) newqueue_##T
#define DeleteQueue(T) \
void deletequeue_##T(queue_##T *queue) { \
free(queue->val); \
free(queue); \
}
#define deletequeue(T) deletequeue_##T
#define DEFINEQUEUE(T) Queue(T) \
Maxsize(T) \
Getsize(T) \
Isempty(T) \
Isfull(T) \
Push(T) \
Pop(T) \
Top(T) \
Clear(T) \
NewQueue(T) \
DeleteQueue(T)
DEFINEQUEUE(int)
DEFINEQUEUE(char)
DEFINEQUEUE(float)
DEFINEQUEUE(double)
int main() {
queue(int) *queue = newqueue(int)(10);
queue->push(queue, 1);
queue->push(queue, 2);
queue->push(queue, 3);
queue->push(queue, 4);
while (!queue->isempty(queue)) {
printf("%d\n", queue->top(queue));
queue->pop(queue);
}
deletequeue(int)(queue);
}