#include
using namespace std;
typedef struct queue {
int *data;
int head, tail, cnt, size;
} Queue;
Queue *init(int size) {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->data = (int *)malloc(sizeof(int) * size);
q->head = q->tail = q->cnt = 0;
q->size = size;
return q;
}
bool empty(Queue *q) {
return q->cnt == 0;
}
int front(Queue *q) {
return q->data[q->head];
}
bool expand(Queue *q) {
int extra_size = q->size;
int *old_data = q->data;
while (extra_size) {
q->data = (int *)malloc(sizeof(int) * (q->size + extra_size));
if (q->data) break;
extra_size /= 2;
}
if (!q->data) return false;
for (int i = 0, j = q->head; i < q->cnt; i++) {
q->data[i] = old_data[(i + j) % q->size];
}
q->head = 0, q->tail = q->cnt;
q->size += extra_size;
free(old_data);
return true;
}
bool push(Queue *q, int val) {
if (!q) return q;
if (q->cnt == q->size) {
if (expand(q)) printf("expand success!\n");
else {
printf("expand failed\n");
return false;
}
}
q->data[q->tail++] = val;
if (q->tail == q->size) q->tail = 0;
q->cnt++;
return true;
}
bool pop(Queue *q) {
if (!q || q->cnt == 0) return false;
q->head++;
if (q->head == q->size) q->head = 0;
q->cnt--;
return true;
}
void output(Queue *q) {
if (!q) return ;
printf("queue(%d) = [", q->cnt);
for (int i = 0, j = q->head; i < q->cnt; i++) {
if (i) printf(", ");
printf("%d", q->data[(i + j) % q->size]);
}
printf("]\n");
}
void clear(Queue *q) {
if (!q) return ;
free(q->data);
free(q);
}
int main() {
#define MAX_OP 20
srand(time(0));
Queue *q = init(1);
int val, op;
for (int i = 0; i < MAX_OP; i++) {
val = rand() % 100;
op = rand() % 4;
switch (op) {
case 0:
case 1:
case 2: {
printf("push %d into queue = %d\n", val, push(q, val));
break;
}
case 3: {
printf("pop %d from queue = ", front(q));
printf("%d\n", pop(q));
}
}
output(q);
printf("\n");
}
clear(q);
return 0;
}
/*结果输出:
push 29 into queue = 1
queue(1) = [29]
expand success!
push 97 into queue = 1
queue(2) = [29, 97]
expand success!
push 25 into queue = 1
queue(3) = [29, 97, 25]
push 83 into queue = 1
queue(4) = [29, 97, 25, 83]
expand success!
push 24 into queue = 1
queue(5) = [29, 97, 25, 83, 24]
push 97 into queue = 1
queue(6) = [29, 97, 25, 83, 24, 97]
push 48 into queue = 1
queue(7) = [29, 97, 25, 83, 24, 97, 48]
push 21 into queue = 1
queue(8) = [29, 97, 25, 83, 24, 97, 48, 21]
expand success!
push 57 into queue = 1
queue(9) = [29, 97, 25, 83, 24, 97, 48, 21, 57]
pop 29 from queue = 1
queue(8) = [97, 25, 83, 24, 97, 48, 21, 57]
pop 97 from queue = 1
queue(7) = [25, 83, 24, 97, 48, 21, 57]
pop 25 from queue = 1
queue(6) = [83, 24, 97, 48, 21, 57]
push 76 into queue = 1
queue(7) = [83, 24, 97, 48, 21, 57, 76]
push 35 into queue = 1
queue(8) = [83, 24, 97, 48, 21, 57, 76, 35]
push 9 into queue = 1
queue(9) = [83, 24, 97, 48, 21, 57, 76, 35, 9]
push 73 into queue = 1
queue(10) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73]
push 44 into queue = 1
queue(11) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44]
push 30 into queue = 1
queue(12) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30]
push 51 into queue = 1
queue(13) = [83, 24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30, 51]
pop 83 from queue = 1
queue(12) = [24, 97, 48, 21, 57, 76, 35, 9, 73, 44, 30, 51]
*/